Sha256: ac94039598b4a70cde31d8a2484ec63d8b4f1756bfacfa396cc3127fa43ccced

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require "direction/version"
# 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.
module Direction
  def command(options)
    method_defs = []
    options.each_pair do |key, value|
      Array(key).map do |command_name|
        method_defs.unshift %{
          def #{command_name}(*args, &block)
            #{value}.__send__(:#{command_name}, *args, &block)
            self
          end
        }
      end
    end
    self.class_eval method_defs.join(' '), __FILE__, __LINE__
  end

  def query(options)
    method_defs = []
    options.each_pair do |key, value|
      Array(key).map do |command_name|
        method_defs.unshift %{
          def #{command_name}(*args, &block)
            #{value}.__send__(:#{command_name}, *args, &block)
          end
        }
      end
    end
    self.class_eval method_defs.join(' '), __FILE__, __LINE__
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
direction-0.0.3 lib/direction.rb