Module: DeferedDelegator
- Included in:
- Binder::AR
- Defined in:
- lib/defered_delegator.rb
Overview
Private: A simple module that delegates classes methods when needed, keeping the calls in memory.
Examples
class Foo
def self.foo arg
"World of #{arg}"
end
#
def self.
"Hello"
end
end
class Baz
extend DeferedDelegator
register_delegators :bar, :foo
#
def initialize obj
@obj = obj
end
#
def
delegate_to @obj
end
end
class Bar < Baz
foo "Foo"
end
Bar.new Foo
Bar.
# => "World of Foo"
# => "Hello"
Instance Method Summary collapse
-
#delegate_to(klass_or_object) ⇒ Object
Public: Triggers the delegation and run the delegated methods.
-
#register_delegators(*args) ⇒ Object
Public: Allows to register delegators.
Instance Method Details
#delegate_to(klass_or_object) ⇒ Object
Public: Triggers the delegation and run the delegated methods
klass_or_object - A Class or an Object to delegate to.
Returns Nothing.
60 61 62 63 64 65 66 67 68 |
# File 'lib/defered_delegator.rb', line 60 def delegate_to klass_or_object @delegators.each do |data| unless data.empty? name = data[:name] args = data[:params] klass_or_object.send(name, *args) end end end |
#register_delegators(*args) ⇒ Object
Public: Allows to register delegators
args - List of Symbols, the method name that will be delegated.
Returns Nothing.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/defered_delegator.rb', line 43 def register_delegators *args args.each do |delegator| delegator = delegator.to_s module_eval %Q{ def self.#{delegator} *parameters @delegators ||= [] @delegators << { name: :#{delegator}, params: parameters } end } end end |