Class: HotCocoa::DelegateMethodBuilder
- Inherits:
-
Object
- Object
- HotCocoa::DelegateMethodBuilder
- Defined in:
- lib/hotcocoa/delegate_builder.rb
Instance Method Summary (collapse)
- - (Object) add_delegated_method(block, selector_name, *parameters)
- - (Object) bind_block_to_delegate_instance_variable(selector_name, block)
-
- (String) block_instance_variable_for(selector_name)
Returns an instance variable name to be used for the delegate method currently being built.
- - (Object) create_delegate_method(selector_name, parameters)
-
- (DelegateMethodBuilder) initialize(target)
constructor
A new instance of DelegateMethodBuilder.
- - (Object) parameter_values_for_mapping(selector_name, parameters)
-
- (String) parameterize_selector_name(selector_name)
Take an Objective-C selector and create a parameter list to be used in creating method's using #eval.
Constructor Details
- (DelegateMethodBuilder) initialize(target)
A new instance of DelegateMethodBuilder
77 78 79 |
# File 'lib/hotcocoa/delegate_builder.rb', line 77 def initialize target @target = target end |
Instance Method Details
- (Object) add_delegated_method(block, selector_name, *parameters)
81 82 83 84 |
# File 'lib/hotcocoa/delegate_builder.rb', line 81 def add_delegated_method block, selector_name, *parameters bind_block_to_delegate_instance_variable(selector_name, block) create_delegate_method(selector_name, parameters) end |
- (Object) bind_block_to_delegate_instance_variable(selector_name, block)
86 87 88 |
# File 'lib/hotcocoa/delegate_builder.rb', line 86 def bind_block_to_delegate_instance_variable selector_name, block @target.instance_variable_set(block_instance_variable_for(selector_name), block) end |
- (String) block_instance_variable_for(selector_name)
Returns an instance variable name to be used for the delegate method currently being built.
105 106 107 |
# File 'lib/hotcocoa/delegate_builder.rb', line 105 def block_instance_variable_for selector_name "@block_#{selector_name.gsub(':', "_")}" end |
- (Object) create_delegate_method(selector_name, parameters)
92 93 94 95 96 97 98 |
# File 'lib/hotcocoa/delegate_builder.rb', line 92 def create_delegate_method selector_name, parameters eval %{ def @target.#{parameterize_selector_name(selector_name)} #{block_instance_variable_for(selector_name)}.call(#{parameter_values_for_mapping(selector_name, parameters)}) end } end |
- (Object) parameter_values_for_mapping(selector_name, parameters)
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/hotcocoa/delegate_builder.rb', line 130 def parameter_values_for_mapping selector_name, parameters return if parameters.empty? result = [] selector_params = selector_name.split(':') parameters.each do |parameter| if (dot = parameter.index('.')) result << "p#{selector_params.index(parameter[0...dot]) + 1}#{parameter[dot..-1]}" else parameter = parameter.to_s raise "Error in delegate mapping: '#{parameter}' is not a valid parameter of method '#{selector_name}'" if selector_params.index(parameter).nil? result << "p#{selector_params.index(parameter) + 1}" end end result.join(', ') end |
- (String) parameterize_selector_name(selector_name)
Take an Objective-C selector and create a parameter list to be used in creating method's using #eval
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/hotcocoa/delegate_builder.rb', line 119 def parameterize_selector_name selector_name return selector_name unless selector_name.include?(':') params = selector_name.split(':') result = "#{params.shift} p1" params.each_with_index do |param, i| result << ",#{param}:p#{i + 2}" end result end |