module DelegateMissingTo::ClassMethods

Public Instance Methods

delegate_missing_to(*object_names) click to toggle source
# File lib/delegate_missing_to.rb, line 30
def delegate_missing_to(*object_names)
  object_names.reverse_each do |object_name|
    define_method "method_missing_with_delegation_to_#{object_name}" do |method, *args, &block|
      object = send(object_name)

      if object.respond_to?(method)
        object.public_send(method, *args, &block)
      else
        send("method_missing_without_delegation_to_#{object_name}", method, *args, &block)
      end
    end

    alias_method_chain :method_missing, "delegation_to_#{object_name}"
  end
end