Module: Walruz::Memoization::ClassMethods

Defined in:
lib/walruz/core_ext/memoization.rb

Instance Method Summary collapse

Instance Method Details

#walruz_memoize(*methods) ⇒ Object

Avoiding name clashes with Rails



11
12
13
14
15
# File 'lib/walruz/core_ext/memoization.rb', line 11

def walruz_memoize(*methods)
  methods.each do |method|
    self.walruz_memoize_method(method)
  end
end

#walruz_memoize_method(method) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/walruz/core_ext/memoization.rb', line 17

def walruz_memoize_method(method)
  memoized = {}
  original_method = self.instance_method(method)
  self.send(:define_method, method) do |*params|
    bound_original_method = original_method.bind(self).to_proc
    if params.last.kind_of?(Symbol) && params.last == :reload
      params.pop
      memoized[[self, params]] = bound_original_method.call(*params)
    else
      memoized[[self, params]] ||= bound_original_method.call(*params)
    end
  end
end