Class: Memor::PrivateMemoizeInstance
- Inherits:
-
Object
- Object
- Memor::PrivateMemoizeInstance
- Defined in:
- lib/memor.rb
Instance Method Summary collapse
- #args_values ⇒ Object
- #callee ⇒ Object
- #explicit_dependents_values ⇒ Object
-
#initialize(the_caller, the_context_binding, explicit_dependents) ⇒ PrivateMemoizeInstance
constructor
A new instance of PrivateMemoizeInstance.
- #memoize_lookup_key ⇒ Object
- #memoize_with_dependents ⇒ Object
- #memoize_without_dependent ⇒ Object
- #memoize_wrap ⇒ Object
- #memor_method ⇒ Object
- #memor_name ⇒ Object
Constructor Details
#initialize(the_caller, the_context_binding, explicit_dependents) ⇒ PrivateMemoizeInstance
Returns a new instance of PrivateMemoizeInstance.
5 6 7 8 9 |
# File 'lib/memor.rb', line 5 def initialize(the_caller, the_context_binding, explicit_dependents) @the_caller = the_caller @the_context_binding = the_context_binding @explicit_dependents = explicit_dependents end |
Instance Method Details
#args_values ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/memor.rb', line 43 def args_values # parameters is like [[:req, :a], [:opt, :b], [:rest, :c], [:block, :d]] value_parameters = memor_method.parameters.map do |pp| if [:req, :rest, :opt].include? pp[0] pp[1] end end.compact.map do |arg_name| @the_context_binding.eval arg_name.to_s end end |
#callee ⇒ Object
11 12 13 |
# File 'lib/memor.rb', line 11 def callee @the_context_binding.eval '__callee__' end |
#explicit_dependents_values ⇒ Object
37 38 39 40 41 |
# File 'lib/memor.rb', line 37 def explicit_dependents_values @explicit_dependents.map do |ed_name| @the_context_binding.eval ed_name.to_s end end |
#memoize_lookup_key ⇒ Object
33 34 35 |
# File 'lib/memor.rb', line 33 def memoize_lookup_key args_values + explicit_dependents_values end |
#memoize_with_dependents ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/memor.rb', line 62 def memoize_with_dependents unless @the_caller.instance_variable_defined? memor_name @the_caller.instance_variable_set memor_name, {} end buckets = @the_caller.instance_variable_get memor_name unless buckets.has_key?(memoize_lookup_key) buckets[memoize_lookup_key] = yield end buckets[memoize_lookup_key] end |
#memoize_without_dependent ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/memor.rb', line 54 def memoize_without_dependent unless @the_caller.instance_variable_defined? memor_name @the_caller.instance_variable_set memor_name, yield end @the_caller.instance_variable_get memor_name end |
#memoize_wrap ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/memor.rb', line 24 def memoize_wrap if memoize_lookup_key.empty? memoize_without_dependent { yield } else memoize_with_dependents { yield } end end |
#memor_method ⇒ Object
20 21 22 |
# File 'lib/memor.rb', line 20 def memor_method @the_caller.method callee end |
#memor_name ⇒ Object
15 16 17 18 |
# File 'lib/memor.rb', line 15 def memor_name "@____memor_#{callee}".gsub('?', '_question_mark') .gsub('!', '_bang') end |