Module: Lite::Memoize::Mixin

Included in:
Instance, Klass
Defined in:
lib/lite/memoize/mixin.rb

Instance Method Summary collapse

Instance Method Details

#caller_key(block, as: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/lite/memoize/mixin.rb', line 11

def caller_key(block, as: nil)
  name = as ? [as] : block.source_location
  return name.concat(block) if block.is_a?(Array)

  block.binding.local_variables.each_with_object(name) do |local_name, array|
    array << local_name
    array << block.binding.local_variable_get(local_name)
  end
end

#memoize(method_name, args: nil, reload: false) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/lite/memoize/mixin.rb', line 21

def memoize(method_name, args: nil, reload: false)
  key = "#{method_name}:#{args}"

  if reload
    store[key] = yield
  else
    store[key] ||= yield
  end
end

#storeObject



7
8
9
# File 'lib/lite/memoize/mixin.rb', line 7

def store
  @_memoized_methods ||= {}
end