Class: HotCocoa::DelegateBuilder

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(control, required_methods) ⇒ DelegateBuilder

Returns a new instance of DelegateBuilder

Parameters:

  • control

    the object which needs a delegate

  • required_methods (Array<SEL,String>)


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

#controlObject (readonly)

The object that needs a delegate

Returns:



13
14
15
# File 'lib/hotcocoa/delegate_builder.rb', line 13

def control
  @control
end

#delegateObject (readonly)

The delegate object

Returns:



26
27
28
# File 'lib/hotcocoa/delegate_builder.rb', line 26

def delegate
  @delegate
end

#required_methodsArray<SEL,String> (readonly)

Delegate methods which are assumed to be implemented and therefore MUST be given at least a stub

Returns:

  • (Array<SEL,String>)


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

Add a delegated method for #control to #delegate



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