lib/glimmer/data_binding/model_binding.rb in glimmer-1.1.0 vs lib/glimmer/data_binding/model_binding.rb in glimmer-1.1.1

- old
+ new

@@ -47,13 +47,11 @@ # e.g. person.address.state returns [person, person.address] def nested_models @nested_models = [base_model] model_property_names.reduce(base_model) do |reduced_model, nested_model_property_name| - if reduced_model.nil? - nil - else + if !reduced_model.nil? invoke_property_reader(reduced_model, nested_model_property_name).tap do |new_reduced_model| @nested_models << new_reduced_model end end end @@ -110,11 +108,14 @@ @nested_property_observers_collection[observer] = nested_property_names.reduce(Concurrent::Hash.new) do |output, property_name| output.merge( property_name => Observer.proc do |new_value| # Ensure reattaching observers when a higher level nested property is updated (e.g. person.address changes reattaches person.address.street observer) add_observer(observer) - observer.call(evaluate_property) + converted_value = evaluate_property + observer.call(converted_value).tap do + apply_processor(@binding_options[:after_read], converted_value) + end end ) end end @nested_property_observers_collection[observer] @@ -125,11 +126,14 @@ add_computed_observers(observer) elsif nested_property? add_nested_observers(observer) else model_binding_observer = Observer.proc do |new_value| - observer.call(evaluate_property) + converted_value = evaluate_property + observer.call(converted_value).tap do + apply_processor(@binding_options[:after_read], converted_value) + end end observer_registration = model_binding_observer.observe(model, property_name) my_registration = observer.registration_for(self) observer.add_dependent(my_registration => observer_registration) end @@ -150,11 +154,14 @@ def computed_observer_for(observer) @computed_observer_collection ||= Concurrent::Hash.new unless @computed_observer_collection.has_key?(observer) @computed_observer_collection[observer] = Observer.proc do |new_value| - observer.call(evaluate_property) + converted_value = evaluate_property + observer.call(converted_value).tap do + apply_processor(@binding_options[:after_read], converted_value) + end end end @computed_observer_collection[observer] end @@ -199,13 +206,11 @@ def evaluate_property value = nil value = invoke_property_reader(model, property_name) unless model.nil? apply_processor(@binding_options[:before_read], value) - converted_value = convert_on_read(value) - apply_processor(@binding_options[:after_read], converted_value) - converted_value + convert_on_read(value) end def evaluate_options_property model.send(options_property_name) unless model.nil? end @@ -254,18 +259,18 @@ 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] 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, converted_value) else object.send(property_expression, converted_value) end + apply_processor(@binding_options[:after_write], converted_value) end end end end