Class: GollyUtils::Delegator

Inherits:
Object
  • Object
show all
Defined in:
lib/golly-utils/delegator.rb

Overview

An object that delegates method calls to eligible delegate objects.

Direct Known Subclasses

MultiIO

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*delegates, options = {}) ⇒ Delegator

Returns a new instance of Delegator.

Parameters:

  • delegates (Object)

    Objects that method calls may be delegated to.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :allow_protected (true, false) — default: false

    Whether or not to allow calls to protected methods in delegates.

  • :cache (true, false) — default: true

    Whether or not to maintain a cache of which delegate objects can respond to each method call.

  • :delegate_to (:first, :all) — default: :first

    When multiple delegates can respond to a method call, this setting determines which object(s) are delegated to.

  • :method_whitelist (String, Symbol, Regexp, Array)

    Method name matcher(s) that specify which methods are allowed to be delegated.

  • :method_blacklist (String, Symbol, Regexp, Array)

    Method name matcher(s) that specify methods that are not allowed to be delegated.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/golly-utils/delegator.rb', line 20

def initialize(*args)
  options= args.last.kind_of?(Hash) ? args.pop.clone : {}
  @original_options= options

  @delegates= args
  @delegate_to= options[:delegate_to] || :first
  @cache= {} unless options.has_key?(:cache) && !options[:cache]
  @allow_protected= options[:allow_protected]
  parse_method_delegation_option options, :method_whitelist
  parse_method_delegation_option options, :method_blacklist
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/golly-utils/delegator.rb', line 35

def method_missing(method, *args)
  matches= delegates_that_respond_to(method)
  return super(method,*args) if matches.empty?

  case delegate_to
  when :first
    delegate_call matches[0], method, args
  when :all
    matches.map{|m| delegate_call m, method, args }
  else
    raise "Don't know how to respond to :delegate_to value of #{delegate_to.inspect}"
  end
end

Instance Attribute Details

#delegate_toObject (readonly)

Returns the value of attribute delegate_to.



5
6
7
# File 'lib/golly-utils/delegator.rb', line 5

def delegate_to
  @delegate_to
end

Instance Method Details

#cloneObject



33
# File 'lib/golly-utils/delegator.rb', line 33

def clone; Delegator.new @delegates.map(&:clone), @original_options end

#dupObject



32
# File 'lib/golly-utils/delegator.rb', line 32

def dup;   Delegator.new @delegates.map(&:dup),   @original_options end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/golly-utils/delegator.rb', line 49

def respond_to?(method)
  super(method) or !delegates_that_respond_to(method).empty?
end