Sha256: 0ed253506bc993a2b31020610d0e418715fa0483c9c110da0123e6558ed0885c

Contents?: true

Size: 1.08 KB

Versions: 9

Compression:

Stored size: 1.08 KB

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
      # Return the proper id for the document.
      def generate_id
        id = Mongo::ObjectID.new
        Mongoid.use_object_ids ? id : id.to_s
      end

      # Set the id for the document.
      def identify(doc)
        doc.id = compose(doc).join(" ").identify if doc.primary_key
        doc.id = generate_id unless doc.id
      end

      # Set the _type field on the document.
      def type(doc)
        doc._type = doc.class.name if Mongoid.persist_types
      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

9 entries across 9 versions & 2 rubygems

Version Path
mongoid-pre-2.0.0.beta1 lib/mongoid/identity.rb
mongoid-2.0.0.alpha lib/mongoid/identity.rb
mongoid-1.2.14 lib/mongoid/identity.rb
mongoid-1.2.13 lib/mongoid/identity.rb
mongoid-1.2.12 lib/mongoid/identity.rb
mongoid-1.2.11 lib/mongoid/identity.rb
mongoid-1.2.10 lib/mongoid/identity.rb
mongoid-1.2.9 lib/mongoid/identity.rb
mongoid-1.2.8 lib/mongoid/identity.rb