Class: HotCocoa::DelegateBuilder
- Defined in:
- lib/hotcocoa/delegate_builder.rb
Overview
Builds a delegate for a control at runtime by creating a generic object and adding singleton methods for each given delegate method; it then tells the control to delegate to that created object.
Instance Attribute Summary collapse
-
#control ⇒ Object
readonly
The object that needs a delegate.
-
#delegate ⇒ Object
readonly
The delegate object.
-
#required_methods ⇒ Array<SEL,String>
readonly
Delegate methods which are assumed to be implemented and therefore MUST be given at least a stub.
Instance Method Summary collapse
- #add_delegated_method(block, selector_name, *parameters) ⇒ Object
- #delegate_to(object, *method_names) ⇒ Object
-
#initialize(control, required_methods) ⇒ DelegateBuilder
constructor
A new instance of DelegateBuilder.
Constructor Details
#initialize(control, required_methods) ⇒ DelegateBuilder
Returns a new instance of DelegateBuilder.
30 31 32 33 34 |
# File 'lib/hotcocoa/delegate_builder.rb', line 30 def initialize control, required_methods @control = control @required_methods = required_methods @delegate = Object.new end |
Instance Attribute Details
#control ⇒ Object (readonly)
The object that needs a delegate
13 14 15 |
# File 'lib/hotcocoa/delegate_builder.rb', line 13 def control @control end |
#delegate ⇒ Object (readonly)
The delegate object
26 27 28 |
# File 'lib/hotcocoa/delegate_builder.rb', line 26 def delegate @delegate end |
#required_methods ⇒ Array<SEL,String> (readonly)
Delegate methods which are assumed to be implemented and therefore MUST be given at least a stub
20 21 22 |
# File 'lib/hotcocoa/delegate_builder.rb', line 20 def required_methods @required_methods end |
Instance Method Details
#add_delegated_method(block, selector_name, *parameters) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/hotcocoa/delegate_builder.rb', line 38 def add_delegated_method block, selector_name, *parameters clear_delegate if required_methods.empty? delegate_method_builder.add_delegated_method block, selector_name, *parameters required_methods.delete(selector_name) set_delegate if required_methods.empty? end |
#delegate_to(object, *method_names) ⇒ Object
47 48 49 50 51 |
# File 'lib/hotcocoa/delegate_builder.rb', line 47 def delegate_to object, *method_names method_names.each do |method_name| control.send(method_name, &object.method(method_name)) if object.respond_to?(method_name) end end |