Module: LazyMethods::InstanceMethods

Defined in:
lib/lazy_methods/lazy_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
# File 'lib/lazy_methods/lazy_methods.rb', line 7

def self.included (base)
  base.send :alias_method, :method_missing_without_lazy, :method_missing
  base.send :alias_method, :method_missing, :method_missing_with_lazy
end

Instance Method Details

#method_missing_with_lazy(method, *args, &block) ⇒ Object

Override missing method to add the lazy method handling



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lazy_methods/lazy_methods.rb', line 13

def method_missing_with_lazy (method, *args, &block)
  method = method.to_s
  if method[0, 5] == 'lazy_'
    method = method.to_s
    return Proxy.new(self, method[5, method.length], args, &block)
  else
    # Keep track of the current missing method calls to keep out of an infinite loop
    stack = Thread.current[:lazy_method_missing_methods] ||= []
    sig = MethodSignature.new(self, method)
    raise NoMethodError.new("undefined method `#{method}' for #{self}") if stack.include?(sig)
    begin
      stack.push(sig)
      return method_missing_without_lazy(method, *args, &block)
    ensure
      stack.pop
    end
  end
end