123456789_123456789_123456789_123456789_123456789_

Module: Mongo::Operation::Idable Private

Do not use. This module is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/mongo/operation/shared/idable.rb

Overview

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

Since:

  • 2.5.2

Constant Summary

Instance Method Summary

Instance Method Details

#documents

Since:

  • 2.5.2

[ GitHub ]

  
# File 'lib/mongo/operation/shared/idable.rb', line 27

def documents
  @documents ||= ensure_ids(super)
end

#ensure_ids(documents) (private)

Since:

  • 2.5.2

[ GitHub ]

  
# File 'lib/mongo/operation/shared/idable.rb', line 58

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) ⇒ Boolean (private)

Since:

  • 2.5.2

[ GitHub ]

  
# File 'lib/mongo/operation/shared/idable.rb', line 54

def has_id?(doc)
  !!id(doc)
end

#id(doc) (private)

Since:

  • 2.5.2

[ GitHub ]

  
# File 'lib/mongo/operation/shared/idable.rb', line 50

def id(doc)
  doc.respond_to?(:id) ? doc.id : (doc['_id'] || doc[:_id])
end

#id_generatorIdGenerator (private)

Get the id generator.

Examples:

Get the id generator.

idable.id_generator

Returns:

  • (IdGenerator)

    The default or custom id generator.

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/operation/shared/idable.rb', line 46

def id_generator
  @id_generator ||= (spec[ID_GENERATOR] || Operation::ObjectIdGenerator.new)
end