lib/phlex/attribute_cache.rb

Lines

100%

56 of 56 covered

Branches

100%

44 of 44 covered

Methods

100%

6 of 6 covered

1# frozen_string_literal: true
2
3# @api private
4#
5# Caches serialised attributes. Hash#eql? ignores order, a String's class and
6# whether it's a safe object, any of which can change the output, so a hit
7# must match the frozen snapshot taken when it was cached, in order and class
8# for class. The snapshot interns its Strings, so a frozen literal matches by
9# identity, and a safe object can't, since it can't be frozen before it's
10# extended. Only plain Hashes, Arrays, Strings, SafeValues, Symbols, Integers,
11# true, false and nil are cached; anything else is serialised every time.
121class Phlex::AttributeCache < Phlex::FIFO
131 Pairs = Data.define(:items)
14
151 def fetch(attributes)
161864 digest = attributes.hash
17
181864 if (entry = @store[digest])
191022 snapshot, value = entry
201022 return matches?(snapshot, attributes) ? value : yield
21 end
22
23842 value = yield
24545 return value if value.bytesize.zero? || value.bytesize > @max_value_bytesize
25
26642 snapshot = catch(:uncacheable) { snapshot(attributes) }
27321 store(digest, snapshot, value) unless :uncacheable == snapshot
28321 value
29 end
30
311 def [](attributes)
3227 snapshot, value = @store[attributes.hash]
3327 value if snapshot && matches?(snapshot, attributes)
34 end
35
361 def []=(attributes, value)
374 snapshot = catch(:uncacheable) { snapshot(attributes) }
382 store(attributes.hash, snapshot, value) unless :uncacheable == snapshot
39 end
40
411 private def snapshot(value)
421636 case value
43 when Symbol, Integer, true, false, nil
44685 value
45 when String
46300 throw :uncacheable, :uncacheable unless String.equal?(value.class) && !(Phlex::SGML::SafeObject === value)
47297 -value
48 when Phlex::SGML::SafeValue
4924 throw :uncacheable, :uncacheable unless Phlex::SGML::SafeValue.equal?(value.class) && String.equal?(value.to_s.class)
5023 Phlex::SGML::SafeValue.new(-value.to_s)
51 when Hash
52446 throw :uncacheable, :uncacheable unless Hash.equal?(value.class)
531008 Pairs.new(value.flat_map { |k, v| [snapshot_key(k), snapshot(v)] }.freeze)
54 when Array
5592 throw :uncacheable, :uncacheable unless Array.equal?(value.class)
56260 value.map { |item| snapshot(item) }.freeze
57 else
5889 throw :uncacheable, :uncacheable
59 end
60 end
61
62 # A pair with a nil or false value is skipped when serialising, so its key
63 # can be anything, even a cyclic Array.
641 private def snapshot_key(key)
65563 case key
66561 when Symbol, String then snapshot(key)
672 else throw :uncacheable, :uncacheable
68 end
69 end
70
711 private def matches?(snapshot, value)
721378 case snapshot
73 when Pairs
741287 items = snapshot.items
751287 return false unless Hash.equal?(value.class) && value.size * 2 == items.size
76
771276 i = 0
781276 value.each do |k, v|
792783 key = items[i]
802783 item = items[i + 1]
812783 return false unless key.equal?(k) || matches?(key, k)
822777 return false unless item.equal?(v) || matches?(item, v)
832735 i += 2
84 end
85
861228 true
87 when Array
8829 return false unless Array.equal?(value.class) && value.size == snapshot.size
89
9021 i = 0
9121 while i < snapshot.size
9237 item = snapshot[i]
9337 return false unless item.equal?(value[i]) || matches?(item, value[i])
9433 i += 1
95 end
96
9717 true
98 when String
9933 String.equal?(value.class) && snapshot.eql?(value) && snapshot.encoding == value.encoding && !(Phlex::SGML::SafeObject === value)
100 when Phlex::SGML::SafeValue
10115 Phlex::SGML::SafeValue.equal?(value.class) && String.equal?(value.to_s.class) && snapshot.to_s.eql?(value.to_s) && snapshot.to_s.encoding == value.to_s.encoding
102 else
10314 snapshot.eql?(value)
104 end
105 end
106end