Class: DataMapper::YS::CachedAccessor::Define

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-ys/cached_accessor.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, &block) ⇒ Define

Returns a new instance of Define.



18
19
20
21
# File 'lib/dm-ys/cached_accessor.rb', line 18

def initialize(klass, &block)
  @klass = klass
  instance_eval(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, &block) ⇒ Object



23
24
25
26
27
# File 'lib/dm-ys/cached_accessor.rb', line 23

def method_missing(symbol, &block)
  cached = "__cached__#{symbol}"
  @klass.send(:define_method, cached, &block)
  @klass.class_eval("def #{symbol}; @#{cached} ||= #{cached}; end", "(__CACHED_ACCESSOR__)", 1)
end