Module: ActiveSupport::Memoizable

Defined in:
lib/active_support/memoizable.rb

Defined Under Namespace

Modules: Freezable

Constant Summary collapse

MEMOIZED_IVAR =
Proc.new do |symbol|
  "@_memoized_#{symbol.to_s.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')}".to_sym
end

Instance Method Summary collapse

Instance Method Details

#memoize(*symbols) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/active_support/memoizable.rb', line 44

def memoize(*symbols)
  symbols.each do |symbol|
    original_method = :"_unmemoized_#{symbol}"
    memoized_ivar = MEMOIZED_IVAR.call(symbol)

    class_eval "include Freezable\n\nraise \"Already memoized \#{symbol}\" if method_defined?(:\#{original_method})\nalias \#{original_method} \#{symbol}\n\nif instance_method(:\#{symbol}).arity == 0\ndef \#{symbol}(reload = false)\nif reload || !defined?(\#{memoized_ivar}) || \#{memoized_ivar}.empty?\n\#{memoized_ivar} = [\#{original_method}.freeze]\nend\n\#{memoized_ivar}[0]\nend\nelse\ndef \#{symbol}(*args)\n\#{memoized_ivar} ||= {} unless frozen?\nreload = args.pop if args.last == true || args.last == :reload\n\nif defined?(\#{memoized_ivar}) && \#{memoized_ivar}\nif !reload && \#{memoized_ivar}.has_key?(args)\n\#{memoized_ivar}[args]\nelsif \#{memoized_ivar}\n\#{memoized_ivar}[args] = \#{original_method}(*args).freeze\nend\nelse\n\#{original_method}(*args)\nend\nend\nend\n", __FILE__, __LINE__
  end
end