Sha256: 254cb666f9891c1b71345fcd2e0737f2422af85e3be79bfccffafb566aa206e0

Contents?: true

Size: 1.07 KB

Versions: 14

Compression:

Stored size: 1.07 KB

Contents

module MongoModel
  module Attributes
    module Mongo
      def to_mongo
        inject({}) do |result, (k, v)|
          property = properties[k]
          
          if property
            result[property.as] = property.to_mongo(v)
          else
            converter = Types.converter_for(v.class)
            result[k.to_s] = converter.to_mongo(v)
          end
          
          result
        end
      end
      
      def load!(hash)
        from_mongo!(hash)
        changed.clear
      end
      
      def from_mongo!(hash)
        hash.each do |k, v|
          property = properties_as[k.to_s]
          
          if property
            child = self[property.name] = property.from_mongo(v)
            child.parent_document = instance if child.respond_to?(:parent_document=)
          else
            self[k.to_sym] = v
          end
        end
      end
    
    private
      def properties_as
        @properties_as ||= properties.inject({}) do |result, (name, property)|
          result[property.as] = property
          result
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
mongomodel-0.2.17 lib/mongomodel/attributes/mongo.rb
mongomodel-0.2.16 lib/mongomodel/attributes/mongo.rb
mongomodel-0.2.15 lib/mongomodel/attributes/mongo.rb
mongomodel-0.2.14 lib/mongomodel/attributes/mongo.rb
mongomodel-0.2.13 lib/mongomodel/attributes/mongo.rb
mongomodel-0.2.12 lib/mongomodel/attributes/mongo.rb
mongomodel-0.2.11 lib/mongomodel/attributes/mongo.rb
mongomodel-0.2.10 lib/mongomodel/attributes/mongo.rb
mongomodel-0.2.9 lib/mongomodel/attributes/mongo.rb
mongomodel-0.2.8 lib/mongomodel/attributes/mongo.rb
mongomodel-0.2.7 lib/mongomodel/attributes/mongo.rb
mongomodel-0.2.6 lib/mongomodel/attributes/mongo.rb
mongomodel-0.2.5 lib/mongomodel/attributes/mongo.rb
mongomodel-0.2.4 lib/mongomodel/attributes/mongo.rb