Module: FastForwarding::InstanceMethods

Defined in:
lib/fast_forward.rb

Instance Method Summary collapse

Instance Method Details

#fast_forward_to(object) ⇒ Object



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

def fast_forward_to(object)
  class_eval(<<-END_OF_METHOD_MISSING, "(__FAST_FORWARD__)", 1)
    def method_missing_with_fast_forwarding(meth, *args, &block)
      raise FastForwarding::DelegationToNilError, "Cannot delegate to nil: #{object}" if #{object}.nil?
      result = #{object}.__send__(meth, *args, &block)
      (class << self; self; end).module_eval(<<-EOS, "(__FAST_FORWARD_GENERATE_METHOD__)", 1)
        def \#{meth}(*args, &block)
          raise FastForwarding::DelegationToNilError, "Cannot delegate to nil: #{object}" if #{object}.nil?
          #{object}.__send__(:\#{meth}, *args, &block)
        end
      EOS
      result
    end
    alias_method :method_missing_without_fast_forwarding, :method_missing
    alias_method :method_missing, :method_missing_with_fast_forwarding
  END_OF_METHOD_MISSING
end