Module: AdminWidgets::Memoization

Included in:
BaseWidget
Defined in:
lib/admin_widgets/memoization.rb

Instance Method Summary collapse

Instance Method Details

#memoize(attribute, val_or_callback) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/admin_widgets/memoization.rb', line 3

def memoize(attribute, val_or_callback)
  ivar = "@#{attribute}"

  define_method attribute do
    if (current_val = instance_variable_get(ivar)).nil?
      value = val_or_callback.respond_to?(:call) ? instance_eval(&val_or_callback) : val_or_callback
      current_val = instance_variable_set(ivar, value)
    end
    current_val
  end
end