module MongoModel::AttributeMethods::MultiParameterAssignment

Private Instance Methods

extract_multiparameter_attributes(attrs) click to toggle source
# File lib/mongomodel/concerns/attribute_methods/multi_parameter_assignment.rb, line 19
def extract_multiparameter_attributes(attrs)
  multiparameter_attributes = Hash.new { |h, k| h[k] = [] }

  attrs.each do |k, v|
    if k.to_s =~ /(.*)\((\d+)([if])?\)/
      multiparameter_attributes[$1][$2.to_i - 1] = type_cast_attribute_value($3, v)
      attrs.delete(k)
    end
  end

  multiparameter_attributes
end
transform_multiparameter_attributes(attrs) click to toggle source

Converts multiparameter attributes into array format. For example, the parameters

{ "start_date(1i)" => "2010", "start_date(2i)" => "9", "start_date(3i)" => "4" }

will be converted to:

{ "start_date" => [2010, 9, 4] }
# File lib/mongomodel/concerns/attribute_methods/multi_parameter_assignment.rb, line 15
def transform_multiparameter_attributes(attrs)
  attrs.merge(extract_multiparameter_attributes(attrs))
end
type_cast_attribute_value(type, value) click to toggle source
# File lib/mongomodel/concerns/attribute_methods/multi_parameter_assignment.rb, line 32
def type_cast_attribute_value(type, value)
  case type
  when 'i', 'f'
    value.send("to_#{type}")
  else
    value
  end
end