Module: Delegation
- Included in:
- AppUpdate, Application, Runner
- Defined in:
- lib/delegation.rb
Instance Method Summary collapse
-
#delegate(*arguments) ⇒ Object
Defines a delegation to another object.
Instance Method Details
#delegate(*arguments) ⇒ Object
Defines a delegation to another object. Use this as follows:
delegate_to :a, :b, to: :foobar
This will delegate local methods ‘a’ and ‘b’ to the object returned by the accessor :foobar. Calling
self.a('test')
will now really call
self..a('test')
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/delegation.rb', line 16 def delegate(*arguments) opts = arguments.pop raise ArgumentError, "Missing options hash at end of delegate arguments." \ unless opts && opts[:to] to_ref = opts[:to] silent = opts[:silent] arguments.each do |name| define_method(name) do |*args, &block| obj = self.send(to_ref) fail "Delegation to nil object. (#{to_ref} - #{name})." \ if !obj && !silent obj.send(name, *args, &block) if obj end end end |