Module: ForwardableX

Included in:
Object
Defined in:
lib/forwardablex.rb,
lib/forwardablex/version.rb

Overview

ForwardableX is a module for providing extra Forwardable functions. Benefits to use this library are the following:

  • you can use easy keyword “forward”, instead of “def_instance_delegator” or “def_delegator”

  • you can specify message receiver as instance variable name, Proc object or plain object

  • no need to declare “extend Forwardable”

  • forwardable.rb compatible API

Examples:

Forward to Instance Variable

Receiver = Struct(:m)
class Forwarder
  forward :@receiver, :m
  def initialize
    @recevier = Receiver.new("forwarded")
  end
end
Forwarder.new.m #=> "forwarded"

Forward to Proc Receiver

class Forwarder
  forward lambda{Struct(:m).new("forwarded")}, :m
end
Forwarder.new.m #=> "forwarded"

Forward to Instance

Receiver = Struct(:m)
class Forwarder
  forward Receiver.new("forwarded"), :m
end
Forwarder.new.m #=> "forwarded"

Class Method Accessor

Receiver = Struct(:name)
class Forwarder
  class << self
    def m
      "forwarded"
    end
  end
  forward :class, :m
end
Forwarder.new.m #=> "forwarded"

Constant Summary collapse

VERSION =

version of ForwardableX

"0.1.4"

Instance Method Summary collapse

Instance Method Details

#delegate(hash) ⇒ void

This method returns an undefined value.

Same as Forwardable#delegate, but you can specify receivers as instance variable name, Proc object, and plain object.

Parameters:

  • hash (Hash)

    the hash table contains keys as methods and values as recevier



98
99
100
101
102
# File 'lib/forwardablex.rb', line 98

def delegate(hash)
  hash.each do |methods, receiver|
    forward!(receiver, *methods)
  end
end

#forward(receiver, method, name = method) ⇒ void Also known as: def_instance_delegator, def_singleton_delegator, def_delegator

This method returns an undefined value.

Define a method that forwards the message to the receiver. You can specify receiver as instance variable name, Proc object, and plain object.

Parameters:

  • receiver (Symbol, String, Proc, or Object)

    message receiver

  • method (Symbol)

    method name that we forward to

  • name (Symbol) (defaults to: method)

    method name that we forward from



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/forwardablex.rb', line 55

def forward(receiver, method, name=method)
  context = self.kind_of?(Module) ? self : self.singleton_class
  context.instance_eval do
    case receiver
    when :class
      forward_class_receiver(method, name)
    when :identity
      forward_identity_receiver(name)
    when Symbol, String
      forward_named_receiver(receiver, method, name)
    when Proc
      forward_proc_receiver(receiver, method, name)
    else
      forward_object_receiver(receiver, method, name)
    end
  end
end

#forward!(receiver, *methods) ⇒ void Also known as: def_instance_delegators, def_singleton_delegators, def_delegators

This method returns an undefined value.

Define each method that calls the receiver’s method.

Parameters:

  • receiver (Symbol, String, Proc, or Object)

    message receiver

  • methods (Array<Symbol>)

    method names that we forward to



83
84
85
86
87
# File 'lib/forwardablex.rb', line 83

def forward!(receiver, *methods)
  methods.delete("__send__")
  methods.delete("__id__")
  methods.each {|method| forward(receiver, method)}
end

#forward_as_key(receiver, key, name = key) ⇒ void

This method returns an undefined value.

Define a method that forwards the key to the receiver. You can specify receiver as instance variable name, Proc object, and plain object.

Parameters:

  • receiver (Symbol, String, Proc, or Object)

    message receiver that have method #[]

  • key (Symbol)

    key that we forward to the receiver

  • name (Symbol) (defaults to: key)

    method name that we forward from



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/forwardablex.rb', line 114

def forward_as_key(receiver, key, name=key)
  context = self.kind_of?(Module) ? self : self.singleton_class
  context.instance_eval do
    case receiver
    when :class
      forward_class_receiver(:[], name, key)
    when :identity
      forward_identity_receiver(name)
    when Symbol, String
      forward_named_receiver(receiver, :[], name, key)
    when Proc
      forward_proc_receiver(receiver, :[], name, key)
    else
      forward_object_receiver(receiver, :[], name, key)
    end
  end
end

#forward_as_key!(receiver, *keys) ⇒ void

This method returns an undefined value.

Define each method that forwards to the receiver as key.

Parameters:

  • receiver (Symbol, String, Proc, or Object)

    message receiver that have method #[]

  • key (Array<Symbol>)

    key that we forward to the receiver



139
140
141
# File 'lib/forwardablex.rb', line 139

def forward_as_key!(receiver, *keys)
  keys.each {|key| forward_as_key(receiver, key)}
end