module Direction

Provide a feature like the Forwardable library, but set the return value to self. It provides a class level “command” method to do message forwarding.

class SomeClass

extend Direction

command [:print_details, :setup_things] => :collaborator
query [:name, :id] => :collaborator, :type => :@partner

end

This will define methods on instances that forward to the provided receiver while enforcing encapsulation of the relationship between objects.

Constants

VERSION

Public Class Methods

define_methods(mod, options) { |message, accessor| ... } click to toggle source
# File lib/direction.rb, line 42
def self.define_methods(mod, options)
  method_defs = []
  options.each_pair do |method_names, accessor|
    Array(method_names).map do |message|
      method_defs.push yield(message, accessor)
    end
  end
  mod.class_eval method_defs.join("\n"), __FILE__, __LINE__
end

Public Instance Methods

command(options) click to toggle source

Forward messages and return self, protecting the encapsulation of the object

# File lib/direction.rb, line 20
def command(options)
  Direction.define_methods(self, options) do |command, accessor|
    %{
    def #{command}(*args, &block)
      #{accessor}.__send__(:#{command}, *args, &block)
      self
    end
    }
  end
end
query(options) click to toggle source

Forward messages and return the result of the forwarded message

# File lib/direction.rb, line 32
def query(options)
  Direction.define_methods(self, options) do |query, accessor|
    %{
    def #{query}(*args, &block)
      #{accessor}.__send__(:#{query}, *args, &block)
    end
    }
  end
end