module Mongo::Operation::Idable

Shared behavior of operations that require its documents to each have an id.

@since 2.5.2

Constants

ID_GENERATOR

The option for a custom id generator.

@since 2.2.0

Public Instance Methods

documents() click to toggle source
Calls superclass method
# File lib/mongo/operation/shared/idable.rb, line 23
def documents
  @documents ||= ensure_ids(super)
end

Private Instance Methods

ensure_ids(documents) click to toggle source
# File lib/mongo/operation/shared/idable.rb, line 54
def ensure_ids(documents)
  @ids = []
  documents.collect do |doc|
    doc_with_id = has_id?(doc) ? doc : doc.merge(_id: id_generator.generate)
    @ids << id(doc_with_id)
    doc_with_id
  end
end
has_id?(doc) click to toggle source
# File lib/mongo/operation/shared/idable.rb, line 50
def has_id?(doc)
  !!id(doc)
end
id(doc) click to toggle source
# File lib/mongo/operation/shared/idable.rb, line 46
def id(doc)
  doc.respond_to?(:id) ? doc.id : (doc['_id'] || doc[:_id])
end
id_generator() click to toggle source

Get the id generator.

@example Get the id generator.

idable.id_generator

@return [ IdGenerator ] The default or custom id generator.

@since 2.2.0

# File lib/mongo/operation/shared/idable.rb, line 42
def id_generator
  @id_generator ||= (spec[ID_GENERATOR] || Operation::ObjectIdGenerator.new)
end