Class: ShortCircuIt::MemoizationStore

Inherits:
Object
  • Object
show all
Defined in:
lib/short_circu_it/memoization_store.rb

Constant Summary collapse

NOT_MEMOIZED =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner) ⇒ MemoizationStore

Returns a new instance of MemoizationStore.

Parameters:

  • owner (*)

    The object being memoized



13
14
15
# File 'lib/short_circu_it/memoization_store.rb', line 13

def initialize(owner)
  @owner = owner
end

Instance Attribute Details

#ownerObject (readonly, private)

Returns the value of attribute owner.



95
96
97
# File 'lib/short_circu_it/memoization_store.rb', line 95

def owner
  @owner
end

Instance Method Details

#clear_all_memoizationObject

Clears all memoized values on the object



47
48
49
# File 'lib/short_circu_it/memoization_store.rb', line 47

def clear_all_memoization
  memoized_hash.clear
end

#clear_memoization(*method_names) ⇒ Boolean

Clears all cached values for the given method

Parameters:

  • *method_names (Symbol)

    The name of a memoized method

Returns:

  • (Boolean)

    True if a value was cleared, false if not



42
43
44
# File 'lib/short_circu_it/memoization_store.rb', line 42

def clear_memoization(*method_names)
  method_names.all? { |method_name| memoized_hash.delete(method_name) }
end

#current_memoization_for_method(method_name, state_hash = nil) ⇒ Hash (private)

Returns A hash of memoized values for the current state of the observed objects for the given method.

Parameters:

  • method_name (Symbol)

    The name of a memoized method

Returns:

  • (Hash)

    A hash of memoized values for the current state of the observed objects for the given method.



74
75
76
77
78
79
# File 'lib/short_circu_it/memoization_store.rb', line 74

def current_memoization_for_method(method_name, state_hash = nil)
  state_hash ||= state_hash(method_name)

  # Memoize values inside a hash with default value of NOT_MEMOIZED
  memoization_for_method(method_name)[state_hash] ||= Hash.new(NOT_MEMOIZED)
end

#current_memoization_for_method?(method_name, state_hash = nil) ⇒ Boolean (private)

Returns True if there are any memoized values for the current state of the observed objects.

Parameters:

  • method_name (Symbol)

    The name of a memoized method

Returns:

  • (Boolean)

    True if there are any memoized values for the current state of the observed objects.



83
84
85
86
87
# File 'lib/short_circu_it/memoization_store.rb', line 83

def current_memoization_for_method?(method_name, state_hash = nil)
  state_hash ||= state_hash(method_name)

  memoization_for_method(method_name).key?(state_hash)
end

#inspectObject



51
52
53
# File 'lib/short_circu_it/memoization_store.rb', line 51

def inspect
  "#<#{self.class} memoized: #{memoized_hash.keys.inspect}>"
end

#memoization_for_method(method_name) ⇒ Object (private)



61
62
63
# File 'lib/short_circu_it/memoization_store.rb', line 61

def memoization_for_method(method_name)
  memoized_hash[method_name] ||= {}
end

#memoize(method_name, args, kwargs) { ... } ⇒ *

Returns The value returned either from the memoization cache if present, or yielded block if not.

Parameters:

  • method_name (Symbol)

    The name of the method being memoized.

  • args (Array<*>)

    Arguments passed to the method being memoized.

  • kwargs (Hash)

    Keyword arguments passed to the method being memoized.

Yields:

  • Yields to a given block with no arguments. Memoizes the value returned by the block.

Returns:

  • (*)

    The value returned either from the memoization cache if present, or yielded block if not.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/short_circu_it/memoization_store.rb', line 22

def memoize(method_name, args, kwargs)
  argument_hash = [ *args, kwargs ].hash

  value = memoized_value(method_name, argument_hash)

  return value unless value == NOT_MEMOIZED

  state_hash = state_hash(method_name)

  clear_memoization(method_name) unless current_memoization_for_method?(method_name, state_hash)

  yield.tap do |returned_value|
    current_memoization_for_method(method_name, state_hash)[argument_hash] = returned_value
  end
end

#memoized_hashObject (private)



57
58
59
# File 'lib/short_circu_it/memoization_store.rb', line 57

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.

Parameters:

  • method_name (String)

    The name of the method to memoize

  • argument_hash (Integer)

    The hash value of the arguments passed to the method

Returns:

  • (*)

    The value that has been memoized for the method/argument combination



68
69
70
# File 'lib/short_circu_it/memoization_store.rb', line 68

def memoized_value(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.

Parameters:

  • method_name (Symbol)

    The name of a memoized method

Returns:

  • (Integer)

    The hash value of all observed objects for the given method.



91
92
93
# File 'lib/short_circu_it/memoization_store.rb', line 91

def state_hash(method_name)
  memoization_observers[method_name].map { |observed_method| owner.public_send(observed_method) }.hash
end