Module: DSLKit::Delegate
- Included in:
- Module
- Defined in:
- lib/dslkit/polite.rb
Overview
This module can be included into modules/classes to make the delegate method available.
Instance Method Summary collapse
-
#delegate(method_name, obj, other_method_name = method_name) ⇒ Object
A method to easily delegate methods to an object, stored in an instance variable or returned by a method call.
Instance Method Details
#delegate(method_name, obj, other_method_name = method_name) ⇒ Object
A method to easily delegate methods to an object, stored in an instance variable or returned by a method call.
It's used like this:
class A
delegate :method_here, :@obj, :method_there
end
or:
class A
delegate :method_here, :method_call, :method_there
end
other_method_name defaults to method_name, if it wasn't given.
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 |
# File 'lib/dslkit/polite.rb', line 531 def delegate(method_name, obj, other_method_name = method_name) raise ArgumentError, "obj wasn't defined" unless obj =begin 1.9 only: define_method(method_name) do |*args, &block| instance_variable_get(obj).__send__(other_method_name, *args, &block) end =end obj = obj.to_s if obj[0] == ?@ class_eval " def \#{method_name}(*args, &block)\n instance_variable_get('\#{obj}').__send__(\n '\#{other_method_name}', *args, &block)\n end\n EOS\n else\n class_eval <<-EOS\n def \#{method_name}(*args, &block)\n __send__('\#{obj}').__send__(\n '\#{other_method_name}', *args, &block)\n end\n EOS\n end\nend\n" |