Class: ShortCircuIt::MemoizationStore
- Inherits:
-
Object
- Object
- ShortCircuIt::MemoizationStore
- Defined in:
- lib/short_circu_it/memoization_store.rb
Instance Attribute Summary collapse
-
#owner ⇒ Object
readonly
private
Returns the value of attribute owner.
Instance Method Summary collapse
-
#clear_all_memoization ⇒ Object
Clears all memoized values on the object.
-
#clear_memoization(method_name) ⇒ Boolean
Clears all cached values for the given method.
-
#current_memoization_for_method(method_name) ⇒ Hash
private
A hash of memoized values for the current state of the observed objects for the given method.
-
#current_memoization_for_method?(method_name) ⇒ Boolean
private
True if there are any memoized values for the current state of the observed objects.
-
#initialize(owner) ⇒ MemoizationStore
constructor
A new instance of MemoizationStore.
- #inspect ⇒ Object
- #memoization_for_method(method_name) ⇒ Object private
-
#memoize(method_name, argument_hash) { ... } ⇒ *
The value returned either from the memoization cache if present, or yielded block if not.
-
#memoized?(method_name, argument_hash) ⇒ Boolean
private
True if the method has a current memoized value with the given arguments.
- #memoized_hash ⇒ Object private
-
#memoized_value(method_name, argument_hash) ⇒ *
private
The value that has been memoized for the method/argument combination.
-
#state_hash(method_name) ⇒ Integer
private
The hash value of all observed objects for the given method.
Constructor Details
#initialize(owner) ⇒ MemoizationStore
Returns a new instance of MemoizationStore.
10 11 12 |
# File 'lib/short_circu_it/memoization_store.rb', line 10 def initialize(owner) @owner = owner end |
Instance Attribute Details
#owner ⇒ Object (readonly, private)
Returns the value of attribute owner.
89 90 91 |
# File 'lib/short_circu_it/memoization_store.rb', line 89 def owner @owner end |
Instance Method Details
#clear_all_memoization ⇒ Object
Clears all memoized values on the object
37 38 39 |
# File 'lib/short_circu_it/memoization_store.rb', line 37 def clear_all_memoization memoized_hash.clear end |
#clear_memoization(method_name) ⇒ Boolean
Clears all cached values for the given method
32 33 34 |
# File 'lib/short_circu_it/memoization_store.rb', line 32 def clear_memoization(method_name) !!memoized_hash.delete(method_name) end |
#current_memoization_for_method(method_name) ⇒ Hash (private)
Returns A hash of memoized values for the current state of the observed objects for the given method.
73 74 75 |
# File 'lib/short_circu_it/memoization_store.rb', line 73 def current_memoization_for_method(method_name) memoization_for_method(method_name)[state_hash(method_name)] ||= {} end |
#current_memoization_for_method?(method_name) ⇒ Boolean (private)
Returns True if there are any memoized values for the current state of the observed objects.
79 80 81 |
# File 'lib/short_circu_it/memoization_store.rb', line 79 def current_memoization_for_method?(method_name) memoization_for_method(method_name).key?(state_hash(method_name)) end |
#inspect ⇒ Object
41 42 43 |
# File 'lib/short_circu_it/memoization_store.rb', line 41 def inspect "#<#{self.class} memoized: #{memoized_hash.keys.inspect}>" end |
#memoization_for_method(method_name) ⇒ Object (private)
51 52 53 |
# File 'lib/short_circu_it/memoization_store.rb', line 51 def memoization_for_method(method_name) memoized_hash[method_name] ||= {} end |
#memoize(method_name, argument_hash) { ... } ⇒ *
Returns The value returned either from the memoization cache if present, or yielded block if not.
18 19 20 21 22 23 24 25 26 |
# File 'lib/short_circu_it/memoization_store.rb', line 18 def memoize(method_name, argument_hash) return memoized_value(method_name, argument_hash) if memoized?(method_name, argument_hash) clear_memoization(method_name) unless current_memoization_for_method?(method_name) yield.tap do |returned_value| current_memoization_for_method(method_name)[argument_hash] = returned_value end end |
#memoized?(method_name, argument_hash) ⇒ Boolean (private)
Returns True if the method has a current memoized value with the given arguments.
58 59 60 |
# File 'lib/short_circu_it/memoization_store.rb', line 58 def memoized?(method_name, argument_hash) current_memoization_for_method?(method_name) && current_memoization_for_method(method_name).key?(argument_hash) end |
#memoized_hash ⇒ Object (private)
47 48 49 |
# File 'lib/short_circu_it/memoization_store.rb', line 47 def memoized_hash @memoized_hash ||= {} end |
#memoized_value(method_name, argument_hash) ⇒ * (private)
Returns The value that has been memoized for the method/argument combination.
65 66 67 68 69 |
# File 'lib/short_circu_it/memoization_store.rb', line 65 def memoized_value(method_name, argument_hash) raise NotMemoizedError unless memoized?(method_name, argument_hash) current_memoization_for_method(method_name)[argument_hash] end |
#state_hash(method_name) ⇒ Integer (private)
Returns The hash value of all observed objects for the given method.
85 86 87 |
# File 'lib/short_circu_it/memoization_store.rb', line 85 def state_hash(method_name) memoization_observers[method_name].map { |observed_method| owner.public_send(observed_method) }.hash end |