lib/glimmer/data_binding/model_binding.rb in glimmer-2.4.1 vs lib/glimmer/data_binding/model_binding.rb in glimmer-2.5.0
- old
+ new
@@ -28,13 +28,13 @@
include Observable
include Observer
attr_reader :binding_options, :property_name_expression
- def initialize(base_model, property_name_expression, binding_options = nil)
- @base_model = base_model
- @property_name_expression = property_name_expression
+ def initialize(*args)
+ binding_options = args.pop if args.size > 1 && args.last.is_a?(Hash)
+ @base_model, @property_name_expression = args
@binding_options = binding_options || Concurrent::Hash.new
if computed?
@computed_model_bindings = Concurrent::Array.new(computed_by.map do |computed_by_property_expression|
self.class.new(base_model, computed_by_property_expression)
end)
@@ -89,11 +89,11 @@
def model_property_names
Concurrent::Array.new(nested_property_names[0...-1])
end
def nested_property?
- property_name_expression.match(/[.\[]/)
+ property_name_expression.to_s.match(/[.\[]/)
end
def computed?
!computed_by.empty?
end
@@ -135,11 +135,11 @@
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, observation_options)
+ observer_registration = model_binding_observer.observe(*[model, property_name, observation_options].compact)
my_registration = observer.registration_for(self)
observer.add_dependent(my_registration => observer_registration)
end
end
@@ -204,11 +204,11 @@
end
def call(value, *extra_args)
return if model.nil?
converted_value = value
- invoke_property_writer(model, "#{property_name}=", converted_value) unless converted_value == evaluate_property
+ invoke_property_writer(model, model.is_a?(Hash) ? property_name : "#{property_name}=", converted_value) unless converted_value == evaluate_property || property_name.nil?
end
def evaluate_property
value = nil
value = invoke_property_reader(model, property_name) unless model.nil?
@@ -261,24 +261,35 @@
property_method = '[]'
property_argument = property_expression[1...-1]
property_argument = property_argument.to_i if property_argument.match(/\d+/)
object.send(property_method, property_argument)
else
- object.send(property_expression)
+ if property_expression.nil?
+ object
+ elsif object.is_a?(Hash)
+ object[property_expression]
+ else
+ object.send(property_expression)
+ end
end
end
def invoke_property_writer(object, property_expression, value)
+ return if property_expression.nil?
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)
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)
+ if object.is_a?(Hash)
+ object[property_expression] = converted_value
+ else
+ object.send(property_expression, converted_value)
+ end
end
apply_processor(@binding_options[:after_write], converted_value)
end
end
end