Class: HotCocoa::DelegateMethodBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/hotcocoa/delegate_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ DelegateMethodBuilder

Returns 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

#add_delegated_method(block, selector_name, *parameters) ⇒ Object



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

#bind_block_to_delegate_instance_variable(selector_name, block) ⇒ Object



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

#block_instance_variable_for(selector_name) ⇒ String

Returns an instance variable name to be used for the delegate method currently being built.

Returns:

  • (String)


105
106
107
# File 'lib/hotcocoa/delegate_builder.rb', line 105

def block_instance_variable_for selector_name
  "@block_#{selector_name.gsub(':', "_")}"
end

#create_delegate_method(selector_name, parameters) ⇒ Object

TODO:

GET RID OF EVAL



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

#parameter_values_for_mapping(selector_name, parameters) ⇒ Object



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

#parameterize_selector_name(selector_name) ⇒ String

Take an Objective-C selector and create a parameter list to be used in creating method's using #eval

Examples:

parameterize_selector_name('myDelegateMethod') # => 'myDeletageMethod'
parameterize_selector_name('myDelegateMethod:withArgument:') # => 'myDeletageMethod p1, withArgument:p2'

Parameters:

  • selector_name (String)

Returns:

  • (String)


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