Class: Mongo::Protocol::Insert::Upconverter

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/protocol/insert.rb

Overview

Converts legacy insert messages to the appropriare OP_COMMAND style message.

Since:

  • 2.1.0

Constant Summary

INSERT =

Insert field constant.

Since:

  • 2.1.0

'insert'.freeze
DOCUMENTS =

Documents field constant.

Since:

  • 2.1.0

'documents'.freeze
WRITE_CONCERN =

Write concern field constant.

Since:

  • 2.1.0

'writeConcern'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, documents, options) ⇒ Upconverter

Instantiate the upconverter.

Examples:

Instantiate the upconverter.

Upconverter.new('users', documents)

Parameters:

  • collection (String)

    The name of the collection.

  • documents (Array<BSON::Document>)

    The documents.

  • options (Hash)

    The options.

Since:

  • 2.1.0



149
150
151
152
153
# File 'lib/mongo/protocol/insert.rb', line 149

def initialize(collection, documents, options)
  @collection = collection
  @documents = documents
  @options = options
end

Instance Attribute Details

#collectionString (readonly)

Returns collection The name of the collection.

Returns:

  • (String)

    collection The name of the collection.

Since:

  • 2.1.0



131
132
133
# File 'lib/mongo/protocol/insert.rb', line 131

def collection
  @collection
end

#documentsArray<BSON::Document> (readonly)

Returns documents The documents to insert.

Returns:

  • (Array<BSON::Document>)

    documents The documents to insert.

Since:

  • 2.1.0



134
135
136
# File 'lib/mongo/protocol/insert.rb', line 134

def documents
  @documents
end

#optionsHash (readonly)

Returns options The options.

Returns:

  • (Hash)

    options The options.

Since:

  • 2.1.0



137
138
139
# File 'lib/mongo/protocol/insert.rb', line 137

def options
  @options
end

Instance Method Details

#commandBSON::Document

Get the upconverted command.

Examples:

Get the command.

upconverter.command

Returns:

  • (BSON::Document)

    The upconverted command.

Since:

  • 2.1.0



163
164
165
166
167
168
169
170
# File 'lib/mongo/protocol/insert.rb', line 163

def command
  document = BSON::Document.new
  document.store(INSERT, collection)
  document.store(DOCUMENTS, documents)
  document.store(Message::ORDERED, options.fetch(:ordered, true))
  document.merge!(WRITE_CONCERN => options[:write_concern].options) if options[:write_concern]
  document
end