Module: Humanoid::Memoization

Defined in:
lib/humanoid/memoization.rb

Instance Method Summary collapse

Instance Method Details

#memoized(name, &block) ⇒ Object

Handles cases when accessing an association that should be memoized in the Humanoid specific manner.



6
7
8
9
10
11
12
13
# File 'lib/humanoid/memoization.rb', line 6

def memoized(name, &block)
  var = "@#{name}"
  if instance_variable_defined?(var)
    return instance_variable_get(var)
  end
  value = yield
  instance_variable_set(var, value)
end

#reset(name, &block) ⇒ Object

Humanoid specific behavior is to remove the memoized object when setting the association, or if it wasn’t previously memoized it will get set.



17
18
19
20
21
22
23
24
25
# File 'lib/humanoid/memoization.rb', line 17

def reset(name, &block)
  var = "@#{name}"
  value = yield
  if instance_variable_defined?(var)
    remove_instance_variable(var)
  else
    instance_variable_set(var, value)
  end
end