lib/glimmer/data_binding/model_binding.rb in glimmer-2.6.0 vs lib/glimmer/data_binding/model_binding.rb in glimmer-2.7.0

- old
+ new

@@ -25,10 +25,15 @@ module Glimmer module DataBinding class ModelBinding include Observable include Observer + + ARRAY_INDEXED_PROPERTY_ARGUMENT_REGEX = /\d+/ + HASH_SYMBOL_INDEXED_PROPERTY_ARGUMENT_REGEX = /:[^:]+/ + HASH_SINGLE_QUOTE_INDEXED_PROPERTY_ARGUMENT_REGEX = /'[^']+'/ + HASH_DOUBLE_QUOTE_INDEXED_PROPERTY_ARGUMENT_REGEX = /"[^"]+"/ attr_reader :binding_options, :property_name_expression def initialize(*args) binding_options = args.pop if args.size > 1 && args.last.is_a?(Hash) @@ -204,11 +209,11 @@ end def call(value, *extra_args) return if model.nil? converted_value = value - invoke_property_writer(model, model.is_a?(Hash) ? property_name : "#{property_name}=", converted_value) unless converted_value == evaluate_property || property_name.nil? + invoke_property_writer(model, model.is_a?(Hash) && !property_indexed?(property_name) ? 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? @@ -258,11 +263,11 @@ 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+/) + property_argument = indexed_property_argument(property_argument) object.send(property_method, property_argument) else if property_expression.nil? object elsif object.is_a?(Hash) @@ -279,19 +284,33 @@ 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+/) + property_argument = indexed_property_argument(property_argument) object.send(property_method, property_argument, converted_value) else 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 + + def indexed_property_argument(property_argument) + if property_argument.match(ARRAY_INDEXED_PROPERTY_ARGUMENT_REGEX) + property_argument.to_i + elsif property_argument.match(HASH_SYMBOL_INDEXED_PROPERTY_ARGUMENT_REGEX) + property_argument.sub(':', '').to_sym + elsif property_argument.match(HASH_SINGLE_QUOTE_INDEXED_PROPERTY_ARGUMENT_REGEX) + property_argument.gsub("'", '') + elsif property_argument.match(HASH_DOUBLE_QUOTE_INDEXED_PROPERTY_ARGUMENT_REGEX) + property_argument.gsub('"', '') + else + property_argument + end end end end end