module AdventureRL::Helpers::PipeMethods

Public Class Methods

pipe_methods_from(object_origin, pipe_args = {}) click to toggle source

This method pipes or forwards any methods called on the object to the target object. It defines the method_missing method on the origin object which calls the wanted method on the target object, if it exists.

# File lib/AdventureRL/Helpers/PipeMethods.rb, line 8
def self.pipe_methods_from object_origin, pipe_args = {}
  object_target = pipe_args[:to]
  Error.error(
    "AdventureRL::Helpers::PipeMethods#pipe_methods_from requires a hash with the key :to and the value of the target object, where the methods should be piped to."
  )  unless (object_target)

  object_origin.define_singleton_method :method_missing do |method_name, *args|
    if    (object_target.methods.include?(method_name))
      return object_target.method(method_name).call(*args)
    elsif (object_target.methods.include?(:method_missing))
      return object_target.method(:method_missing).call(method_name, *args)
    else
      raise NoMethodError, "undefined method `#{method_name}' for #{self} (PIPED)"
    end
  end
end