module Mongo::Operation::Write::Idable
This module provides functionality to ensure that documents contain an id field. Used by insert operations (Bulk, legacy, write command inserts).
@since 2.1.0
Constants
- ID_GENERATOR
The option for a custom id generator.
@since 2.2.0
Public Instance Methods
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/write/idable.rb, line 38 def id_generator @id_generator ||= (spec[ID_GENERATOR] || ObjectIdGenerator.new) end
Private Instance Methods
ensure_ids(documents)
click to toggle source
# File lib/mongo/operation/write/idable.rb, line 52 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/write/idable.rb, line 48 def has_id?(doc) !!id(doc) end
id(doc)
click to toggle source
# File lib/mongo/operation/write/idable.rb, line 44 def id(doc) doc.respond_to?(:id) ? doc.id : (doc['_id'] || doc[:_id]) end