Class: GollyUtils::Delegator
Overview
An object that delegates method calls to eligible delegate objects.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#delegate_to ⇒ Object
readonly
Returns the value of attribute delegate_to.
Instance Method Summary collapse
- #clone ⇒ Object
- #dup ⇒ Object
-
#initialize(*delegates, options = {}) ⇒ Delegator
constructor
A new instance of Delegator.
- #method_missing(method, *args) ⇒ Object
- #respond_to?(method) ⇒ Boolean
Constructor Details
#initialize(*delegates, options = {}) ⇒ Delegator
Returns a new instance of Delegator.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/golly-utils/delegator.rb', line 20 def initialize(*args) = args.last.kind_of?(Hash) ? args.pop.clone : {} = @delegates= args @delegate_to= [:delegate_to] || :first @cache= {} unless .has_key?(:cache) && ![:cache] @allow_protected= [:allow_protected] parse_method_delegation_option , :method_whitelist parse_method_delegation_option , :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_to ⇒ Object (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
#clone ⇒ Object
33 |
# File 'lib/golly-utils/delegator.rb', line 33 def clone; Delegator.new @delegates.map(&:clone), end |
#dup ⇒ Object
32 |
# File 'lib/golly-utils/delegator.rb', line 32 def dup; Delegator.new @delegates.map(&:dup), end |
#respond_to?(method) ⇒ 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 |