Module: AttrMemoizer::ClassMethods

Defined in:
lib/attr_memoizer.rb

Instance Method Summary collapse

Instance Method Details

#attr_memoizer(*attrs) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/attr_memoizer.rb', line 9

def attr_memoizer(*attrs)
  attrs_to_memoize.concat(attrs.collect { |ea| ea.to_sym }).uniq!
  # OMG 1.8.7 RETURNS STRINGS
  existing_methods = self.instance_methods(true).collect { |ea| ea.to_sym }
  (existing_methods & attrs_to_memoize).each do |name|
    attrs_to_memoize.delete(name)
    class_eval <<-RUBY
      alias_method :_#{name}, :#{name}
      def #{name}
        defined?(@#{name}) ? @#{name} : @#{name} = _#{name}()
      end
    RUBY
  end
end

#attrs_to_memoizeObject



24
25
26
# File 'lib/attr_memoizer.rb', line 24

def attrs_to_memoize
  @attrs_to_memoize ||= []
end

#method_added(method_name) ⇒ Object



28
29
30
31
# File 'lib/attr_memoizer.rb', line 28

def method_added(method_name)
  attr_memoizer(method_name) if attrs_to_memoize.include?(method_name)
  super
end