lib/couchrest/model/properties.rb in couchrest_model-1.1.2 vs lib/couchrest/model/properties.rb in couchrest_model-1.2.0.beta

- old
+ new

@@ -104,17 +104,26 @@ # Set all the attributes and return a hash with the attributes # that have not been accepted. def directly_set_attributes(hash, mass_assign = false) return if hash.nil? + + multi_parameter_attributes = [] + hash.reject do |key, value| - if self.respond_to?("#{key}=") - self.send("#{key}=", value) + if key.to_s.include?("(") + multi_parameter_attributes << [ key, value ] + false + elsif self.respond_to?("#{key}=") + self.send("#{key}=", value) elsif mass_assign || mass_assign_any_attribute + couchrest_attribute_will_change!(key) if use_dirty? && self[key] != value self[key] = value end end + + assign_multiparameter_attributes(multi_parameter_attributes, hash) unless multi_parameter_attributes.empty? end def directly_set_read_only_attributes(hash) property_list = self.properties.map{|p| p.name} hash.each do |attribute_name, attribute_value| @@ -123,10 +132,42 @@ write_attribute(attribute_name, hash.delete(attribute_name)) end end end + def assign_multiparameter_attributes(pairs, hash) + execute_callstack_for_multiparameter_attributes( + extract_callstack_for_multiparameter_attributes(pairs), hash + ) + end + def execute_callstack_for_multiparameter_attributes(callstack, hash) + callstack.each do |name, values_with_empty_parameters| + if self.respond_to?("#{name}=") + casted_attrib = send("#{name}=", values_with_empty_parameters) + unless casted_attrib.is_a?(Hash) + hash.reject { |key, value| key.include?(name.to_s)} + end + end + end + hash + end + def extract_callstack_for_multiparameter_attributes(pairs) + attributes = { } + + pairs.each do |pair| + multiparameter_name, value = pair + attribute_name = multiparameter_name.split("(").first + attributes[attribute_name] = {} unless attributes.include?(attribute_name) + attributes[attribute_name][find_parameter_name(multiparameter_name)] ||= value + end + attributes + end + + def find_parameter_name(multiparameter_name) + position = multiparameter_name.scan(/\(([0-9]*).*\)/).first.first.to_i + {1 => :year, 2 => :month, 3 => :day, 4 => :hour, 5 => :min, 6 => :sec}[position] + end module ClassMethods def property(name, *options, &block) raise "Invalid property definition, '#{name}' already used for CouchRest Model type field" if name.to_s == model_type_key.to_s && CouchRest::Model::Base >= self