lib/glimmer/data_binding/model_binding.rb in glimmer-1.0.11 vs lib/glimmer/data_binding/model_binding.rb in glimmer-1.1.0
- old
+ new
@@ -70,25 +70,10 @@
def property_name
nested_property? ? nested_property_name : property_name_expression
end
- def convert_on_read(value)
- apply_converter(@binding_options[:on_read], value)
- end
-
- def convert_on_write(value)
- apply_converter(@binding_options[:on_write], value)
- end
-
- def apply_converter(converter, value)
- return value if converter.nil?
- return value.send(converter) if (converter.is_a?(String) || converter.is_a?(Symbol)) && value.respond_to?(converter)
- return converter.call(value) if converter.respond_to?(:call, value)
- raise Glimmer::Error, "Unsupported bind converter: #{converter.inspect}"
- end
-
# All nested property names
# e.g. property name expression "address.state" gives ['address', 'state']
# If there are any indexed property names, this returns indexes as properties.
# e.g. property name expression "addresses[1].state" gives ['addresses', '[1]', 'state']
def nested_property_names
@@ -213,13 +198,16 @@
end
def evaluate_property
value = nil
value = invoke_property_reader(model, property_name) unless model.nil?
- convert_on_read(value)
+ apply_processor(@binding_options[:before_read], value)
+ converted_value = convert_on_read(value)
+ apply_processor(@binding_options[:after_read], converted_value)
+ converted_value
end
-
+
def evaluate_options_property
model.send(options_property_name) unless model.nil?
end
def options_property_name
@@ -228,10 +216,33 @@
def property_indexed?(property_expression)
property_expression.to_s.start_with?('[')
end
+ private
+
+ def convert_on_read(value)
+ apply_processor(@binding_options[:on_read], value)
+ end
+
+ def convert_on_write(value)
+ apply_processor(@binding_options[:on_write], value)
+ end
+
+ def apply_processor(processor, value)
+ return value if processor.nil?
+ return value.send(processor) if (processor.is_a?(String) || processor.is_a?(Symbol)) && value.respond_to?(processor)
+ return invoke_proc_with_exact_parameters(processor, value) if processor.respond_to?(:call)
+ raise Glimmer::Error, "Unsupported bind processor: #{processor.inspect}"
+ end
+
+ def invoke_proc_with_exact_parameters(proc_object, *args)
+ return if proc_object.nil?
+ args = args[0...proc_object.parameters.size]
+ proc_object.call(*args)
+ end
+
def invoke_property_reader(object, property_expression)
if property_indexed?(property_expression)
property_method = '[]'
property_argument = property_expression[1...-1]
property_argument = property_argument.to_i if property_argument.match(/\d+/)
@@ -241,17 +252,19 @@
end
end
def invoke_property_writer(object, property_expression, value)
raise "Cannot invoke `#{property_expression}` because ModelBinding#binding_options[:read_only]=true" if @binding_options[:read_only]
- value = convert_on_write(value)
+ apply_processor(@binding_options[:before_write], value)
+ converted_value = convert_on_write(value)
+ apply_processor(@binding_options[:after_write], converted_value)
if property_indexed?(property_expression)
property_method = '[]='
property_argument = property_expression[1...-2]
property_argument = property_argument.to_i if property_argument.match(/\d+/)
- object.send(property_method, property_argument, value)
+ object.send(property_method, property_argument, converted_value)
else
- object.send(property_expression, value)
+ object.send(property_expression, converted_value)
end
end
end
end
end