Class: Memor::PrivateMemoizeInstance

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

Instance Method Summary collapse

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_valuesObject



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

#calleeObject



11
12
13
# File 'lib/memor.rb', line 11

def callee
  @the_context_binding.eval '__callee__'
end

#explicit_dependents_valuesObject



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_keyObject



33
34
35
# File 'lib/memor.rb', line 33

def memoize_lookup_key
  args_values + explicit_dependents_values
end

#memoize_with_dependentsObject



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_dependentObject



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_wrapObject



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_methodObject



20
21
22
# File 'lib/memor.rb', line 20

def memor_method
  @the_caller.method callee
end

#memor_nameObject



15
16
17
18
# File 'lib/memor.rb', line 15

def memor_name
 "@____memor_#{callee}".gsub('?', '_question_mark')
                       .gsub('!', '_bang')
end