Module: WillingStone::AttributeCache::ClassMethods

Defined in:
lib/willing_stone/attribute_cache.rb

Instance Method Summary collapse

Instance Method Details

#attr_cache(name, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/willing_stone/attribute_cache.rb', line 24

def attr_cache(name, &block)
  define_method(:"#{name}!", &block)
  class_eval <<-RUBY
    def #{name}(force_update = false)
      @attribute_cache ||= {}
      if force_update
        @attribute_cache[:#{name}] = #{name}!
      else
        @attribute_cache[:#{name}] ||= #{name}!
      end
    end
    def #{name}?
      @attribute_cache ||= {}
      @attribute_cache.key?(:#{name})
    end
  RUBY
end