Sha256: bfece01958f0b9cef13f9ab6eb5997909ec635b2a9acc8ec39384abe1e2dad98

Contents?: true

Size: 1.22 KB

Versions: 6

Compression:

Stored size: 1.22 KB

Contents

module MongoModel
  module AttributeMethods
    module MultiParameterAssignment
      extend ActiveSupport::Concern
      
      def assign_attributes(attrs, options={})#:nodoc:
        super(transform_multiparameter_attributes(attrs))
      end
    
    private
      # 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] }
      def transform_multiparameter_attributes(attrs)
        attrs.merge(extract_multiparameter_attributes(attrs))
      end
      
      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
      
      def type_cast_attribute_value(type, value)
        case type
        when 'i', 'f'
          value.send("to_#{type}")
        else
          value
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mongomodel-0.5.5 lib/mongomodel/concerns/attribute_methods/multi_parameter_assignment.rb
mongomodel-0.5.4 lib/mongomodel/concerns/attribute_methods/multi_parameter_assignment.rb
mongomodel-0.5.3 lib/mongomodel/concerns/attribute_methods/multi_parameter_assignment.rb
mongomodel-0.5.2 lib/mongomodel/concerns/attribute_methods/multi_parameter_assignment.rb
mongomodel-0.5.1 lib/mongomodel/concerns/attribute_methods/multi_parameter_assignment.rb
mongomodel-0.5.0 lib/mongomodel/concerns/attribute_methods/multi_parameter_assignment.rb