module Forwarder

Constants

VERSION

Public Instance Methods

forward(message, opts={}) click to toggle source

delegates (forwards) a message to an object (indicated by :to)

# File lib/forwarder.rb, line 8
  def forward message, opts={}, &blk
    opts = parse_opts opts, blk
#    p opts: opts
    forward_without_parsing message, opts
  end
forward_all(*messages, &blk) click to toggle source
# File lib/forwarder.rb, line 14
def forward_all *messages, &blk
  opts = messages.pop
  raise ArgumentError, "need a Hash as last arg" unless Hash === opts
  opts = parse_opts opts, blk
  messages.each do | msg |
    forward_without_parsing msg, opts
  end
end
forward_to_self(message, opts={}) click to toggle source
# File lib/forwarder.rb, line 23
def forward_to_self message, opts={}
  forwarding_with message, opts.merge( to: lambda{ |*args| self } )
end

Private Instance Methods

forward_with_chain(message, opts) click to toggle source

Triggered by the presence of :to_chain in forward's parameters

# File lib/forwarder.rb, line 46
def forward_with_chain message, opts
  return false unless opts[:to_chain]
  forwarding_with message, opts
end
forward_with_forwardable(message, opts) click to toggle source

Handles the cases, where Forwardable can be used behind the scenes as a matter of fact :to was indicating a method or instance variable and :as was passed in (or defaults to the original message).

# File lib/forwarder.rb, line 38
def forward_with_forwardable message, opts
  to = opts.fetch :to
  extend Forwardable
  as = opts.fetch( :as, message )
  def_delegator to, as, message
end
forward_with_meta(message, opts) click to toggle source

Transform blk into a normal parameter call the metaprogramming stuff if needed and return nil iff we can do it with Forwardable

# File lib/forwarder.rb, line 52
  def forward_with_meta message, opts
#    p [:forward_with_meta, opts]
    if opts[:applying] || opts[:with]
      forwarding_with message, opts
      true
    elsif opts[:to_object]
      forwarding_to_object message, opts
      true
    end
  end
forward_without_parsing(message, opts) click to toggle source
# File lib/forwarder.rb, line 74
  def forward_without_parsing message, opts
#    p [:forward_without_parsing, opts]
    return if forward_with_meta message, opts
    return if forward_with_chain message, opts
    forward_with_forwardable message, opts
  end
forwarding_to_object(message, opts) click to toggle source

Triggered by the presence of :to_object in forward's parameters

# File lib/forwarder.rb, line 30
def forwarding_to_object message, opts
  target = opts[ :to_object ]
  forwarding_with message, opts.merge( to: Meta::ObjectContainer.new(target), with: opts.fetch( :with, [] ) )
end
forwarding_with(message, opts) click to toggle source

Whenever the forward(s) cannot be implemented by def_delegator(s) eventually this method is called

# File lib/forwarder.rb, line 65
  def forwarding_with message, opts
#    p opts
    application, as, to, with = opts.values_at( :applying, :as, :to, :with )
    as ||= message

   # define_method( :__eval_receiver__, &Meta.eval_receiver_body )
    define_method( message, &Meta.eval_body( application, as, to, with ) )
  end
parse_opts(opts, blk) click to toggle source
# File lib/forwarder.rb, line 81
def parse_opts opts, blk
  params = opts.values_at :to_chain, :to, :to_object
  params.compact.tap do | pms |
    if pms.size != 1
      raise ArgumentError, 
        "must passin exactly one of these kwd arguments: :to, :to_object or :to_chain, but found #{pms.join(", ")}"
    end
  end
  opts.update applying: blk if blk
  if params.first
    opts.merge to: params.first
  else
    opts
  end
end