Module: AttributeMemoization

Defined in:
lib/attribute_memoization.rb,
lib/attribute_memoization/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#attr_accessor(*names, &block) ⇒ Object



6
7
8
9
10
11
# File 'lib/attribute_memoization.rb', line 6

def attr_accessor(*names, &block)
  return super(*names) unless block_given?

  attr_reader(*names, &block)
  attr_writer(*names)
end

#attr_reader(*names, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/attribute_memoization.rb', line 13

def attr_reader(*names, &block)
  return super(*names) unless block_given?

  names.each do |name|
    private define_method("_calculate_#{name}", &block)

    class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
      def #{name}
        return @#{name} if defined? @#{name}
        @#{name} = _calculate_#{name}
      end
    RUBY
  end
end