Sha256: 8702efe572e13dc27005c744adea726c984d3507f3a4ce378c7c1c07b0b6d1da

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

module DataMapper
  module Mongo
    class Property
      # Each object (document) stored in Mongo DB has an _id field as its
      # first attribute.  This is an object id.  It must be unique for each
      # member of a collection (this is enforced if the collection has an _id
      # index, which is the case by default).
      #
      # The database will assign an _id if an object being inserted into a
      # collection does not have one.
      #
      # The _id may be of any type as long as it is a unique value.
      #
      # @see http://www.mongodb.org/display/DOCS/Object+IDs
      #
      # @api public
      class ObjectId < DataMapper::Property::Object
        include DataMapper::Property::PassThroughLoadDump

        primitive ::BSON::ObjectId
        key true
        field "_id"
        required false

        # Returns the ObjectId as a string
        #
        # @return [String]
        #
        # @api semipublic
        def typecast_to_primitive(value)
          case value
          when ::String
            ::BSON::ObjectId.from_string(value)
          else
            raise ArgumentError.new('+value+ must String')
          end
        end

        # @api semipublic
        def valid?(value, negated = false)
          value.nil? || primitive?(value)
        end

      end # ObjectId
    end # Property
  end # Mongo
end # DataMapper

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dm-mongo-adapter-0.6.0 lib/mongo_adapter/property/object_id.rb