Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/switchboard/ext/delegate.rb

Instance Method Summary collapse

Instance Method Details

#delegate(*methods) ⇒ Object

Modified version from ActiveSupport to support :with (for additional arguments)



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/switchboard/ext/delegate.rb', line 3

def delegate(*methods)
  options = methods.pop
  unless options.is_a?(Hash) && to = options[:to]
    raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
  end

  prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_"
  with = options[:with] || []

  methods.each do |method|
    module_eval(<<-EOS, "(__DELEGATION__)", 1)
      def #{prefix}#{method}(*args, &block)
        args += [#{with * ", "}]
        #{to}.__send__(#{method.inspect}, *args, &block)
      end
    EOS
  end
end