Sha256: 146f2d63abbe1c2c061fabbe69cbe4cbc4b813f01213b909d2e62a4246c9ee5b

Contents?: true

Size: 935 Bytes

Versions: 14

Compression:

Stored size: 935 Bytes

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  class Identity #:nodoc:
    class << self
      # Create the identity for the +Document+.
      #
      # The id will be set in either in the form of a Mongo
      # +ObjectID+ or a composite key set up by defining a key on the document.
      #
      # The _type will be set to the document's class name.
      def create(doc)
        identify(doc); type(doc); doc
      end

      protected
      # Set the id for the document.
      def identify(doc)
        doc.id = compose(doc).join(" ").identify if doc.primary_key
        doc.id = Mongo::ObjectID.new.to_s unless doc.id
      end

      # Set the _type field on the document.
      def type(doc)
        doc._type = doc.class.name
      end

      # Generates the composite key for a document.
      def compose(doc)
        doc.primary_key.collect { |key| doc.attributes[key] }.reject { |val| val.nil? }
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
mongoid-pre-2.0.0.pre lib/mongoid/identity.rb
mongoid-1.2.6 lib/mongoid/identity.rb
mongoid-1.2.5 lib/mongoid/identity.rb
mongoid-1.2.4 lib/mongoid/identity.rb
mongoid-1.2.3 lib/mongoid/identity.rb
mongoid-1.2.2 lib/mongoid/identity.rb
mongoid-1.2.1 lib/mongoid/identity.rb
mongoid-1.2.0 lib/mongoid/identity.rb
mongoid-1.1.4 lib/mongoid/identity.rb
mongoid-1.1.3 lib/mongoid/identity.rb
mongoid-1.1.2 lib/mongoid/identity.rb
mongoid-1.1.1 lib/mongoid/identity.rb
mongoid-1.1.0 lib/mongoid/identity.rb
mongoid-1.0.6 lib/mongoid/identity.rb