Sha256: 1f2b8a099e6fed495b68ac5385d405d8ecb6acb4512c1bff8041079858dfd369

Contents?: true

Size: 836 Bytes

Versions: 1

Compression:

Stored size: 836 Bytes

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 [:name, :id] => :collaborator, [:color, :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})
            self
          end
        }
      end
    end
    self.class_eval method_defs.join(' ')
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
direction-0.0.1 lib/direction.rb