Class: HotCocoa::DelegateBuilder
- Inherits:
-
Object
- Object
- 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)
-
- (Object) control
readonly
The object that needs a delegate.
-
- (Object) delegate
readonly
The delegate object.
-
- (Array<SEL,String>) required_methods
readonly
Delegate methods which are assumed to be implemented and therefore MUST be given at least a stub.
Instance Method Summary (collapse)
- - (Object) add_delegated_method(block, selector_name, *parameters)
- - (Object) delegate_to(object, *method_names)
-
- (DelegateBuilder) initialize(control, required_methods)
constructor
A new instance of DelegateBuilder.
Constructor Details
- (DelegateBuilder) initialize(control, required_methods)
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
- (Object) control (readonly)
The object that needs a delegate
13 14 15 |
# File 'lib/hotcocoa/delegate_builder.rb', line 13 def control @control end |
- (Object) delegate (readonly)
The delegate object
26 27 28 |
# File 'lib/hotcocoa/delegate_builder.rb', line 26 def delegate @delegate end |
- (Array<SEL,String>) required_methods (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
- (Object) add_delegated_method(block, selector_name, *parameters)
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 |
- (Object) delegate_to(object, *method_names)
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 |