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
if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/
raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
end
prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_"
file, line = caller.first.split(':', 2)
line = line.to_i
methods.each do |method|
on_nil =
if options[:allow_nil]
'return'
else
%(raise "#{prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
end
module_eval("def \#{prefix}\#{method}(*args, &block) # def customer_name(*args, &block)\n\#{to}.__send__(\#{method.inspect}, *args, &block) # client.__send__(:name, *args, &block)\nrescue NoMethodError # rescue NoMethodError\nif \#{to}.nil? # if client.nil?\n\#{on_nil}\nelse # else\nraise # raise\nend # end\nend # end\n", file, line)
end
end