Sha256: 083bacfbfbb91a8cfed869edcbac9992440ca7a4bb8670725e1bbd997baf3fe1

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  class Identity #:nodoc:
    # 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
      identify!; type!
    end

    # Create the new identity generator - this will be expanded in the future
    # to support pk generators.
    #
    # Options:
    #
    # document: The document to generate an id for.
    def initialize(document)
      @document = document
    end

    protected
    # Return the proper id for the document.
    def generate_id
      id = BSON::ObjectID.new
      @document.using_object_ids? ? id : id.to_s
    end

    # Set the id for the document.
    def identify!
      @document.id = compose.join(" ").identify if @document.primary_key
      @document.id = generate_id if @document.id.blank?
    end

    # Set the _type field on the @document.
    def type!
      @document._type = @document.class.name if @document.hereditary? || @document.class.descendants.any?
    end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongoid-2.0.0.beta.18 lib/mongoid/identity.rb
mongoid-2.0.0.beta.17 lib/mongoid/identity.rb
mongoid-2.0.0.beta.16 lib/mongoid/identity.rb