Sha256: ce20f7350c515635b1ada5331e296a1ad4020642bf8cb026a11610b5b48771dd
Contents?: true
Size: 1.39 KB
Versions: 1
Compression:
Stored size: 1.39 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 # Forward messages and return self, protecting the encapsulation of the object def command(options) Direction.define_methods(self, options) do |command, accessor| %{ def #{command}(*args, &block) #{accessor}.__send__(:#{command}, *args, &block) self end } end end # Forward messages and return the result of the forwarded message def query(options) Direction.define_methods(self, options) do |query, accessor| %{ def #{query}(*args, &block) #{accessor}.__send__(:#{query}, *args, &block) end } end end 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 end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
direction-0.0.5 | lib/direction.rb |