module Tins::Delegate

This module can be included into modules/classes to make the delegate method available.

Public Instance Methods

delegate(method_name, obj, other_method_name = method_name) click to toggle source

A method to easily delegate methods to an object, stored in an instance variable or returned by a method call.

It's used like this:

class A
  delegate :method_here, :@obj, :method_there
end

or:

class A
  delegate :method_here, :method_call, :method_there
end

other_method_name defaults to method_name, if it wasn't given.

# File lib/tins/dslkit.rb, line 481
    def delegate(method_name, obj, other_method_name = method_name)
      raise ArgumentError, "obj wasn't defined" unless obj
=begin
1.9 only:
      define_method(method_name) do |*args, &block|
        instance_variable_get(obj).__send__(other_method_name, *args, &block)
      end
=end
      obj = obj.to_s
      if obj[0] == ?@
        class_eval <<-EOS
          def #{method_name}(*args, &block)
            instance_variable_get('#{obj}').__send__(
              '#{other_method_name}', *args, &block)
          end
        EOS
      else
        class_eval <<-EOS
          def #{method_name}(*args, &block)
            __send__('#{obj}').__send__(
              '#{other_method_name}', *args, &block)
          end
        EOS
      end
    end