Module: DelegateIt

Defined in:
lib/delegate_it.rb,
lib/delegate_it/main.rb,
lib/delegate_it/version.rb

Constant Summary collapse

VERSION =
"1.1.1"

Instance Method Summary collapse

Instance Method Details

#delegate(*methods, options) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/delegate_it/main.rb', line 3

def delegate *methods, options
  methods.each do |method|
    method_name = if options[:prefix] && options[:prefix].is_a?(Symbol)
      "#{options[:prefix]}_#{method}"
    elsif options[:prefix]
      "#{options[:to]}_#{method}"
    else
      method
    end

    define_method method_name do |*args|
      receiver = self.send(options[:to])
      if options[:allow_nil] && !receiver
        nil
      else
        receiver.send(method, *args)
      end
    end
  end
end