Class: Mongoid::Relations::Referenced::Batch::Insert

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/relations/referenced/batch/insert.rb

Overview

Handles all the batch insert collection.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#documentsObject

Returns the value of attribute documents.



9
10
11
# File 'lib/mongoid/relations/referenced/batch/insert.rb', line 9

def documents
  @documents
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/mongoid/relations/referenced/batch/insert.rb', line 9

def options
  @options
end

Instance Method Details

#consume(document, options = {}) ⇒ Object

Consumes an execution that was supposed to hit the database, but is now being deferred to later in favor of a single batch insert.

Examples:

Consume the operation.

set.consume({ "field" => "value" }, { :safe => true })

Parameters:

  • document (Hash)

    The document to collect.

  • options (Hash) (defaults to: {})

    The persistence options.

Options Hash (options):

  • :safe (true, false)

    Persist in safe mode.

Since:

  • 2.0.2, batch-relational-insert



23
24
25
26
# File 'lib/mongoid/relations/referenced/batch/insert.rb', line 23

def consume(document, options = {})
  @consumed, @options = true, options
  (@documents ||= []).push(document)
end

#consumed?true, false

Has this operation consumed any executions?

Examples:

Is this consumed?

insert.consumed?

Returns:

  • (true, false)

    If the operation has consumed anything.

Since:

  • 2.0.2, batch-relational-insert



36
37
38
# File 'lib/mongoid/relations/referenced/batch/insert.rb', line 36

def consumed?
  !!@consumed
end

#execute(collection) ⇒ Object

Execute the batch insert operation on the collection.

Examples:

Execute the operation.

insert.execute(collection)

Parameters:

  • collection (Collection)

    The root collection.

Since:

  • 2.0.2, batch-relational-insert



48
49
50
51
52
# File 'lib/mongoid/relations/referenced/batch/insert.rb', line 48

def execute(collection)
  if collection && consumed?
    collection.insert(documents, options)
  end
end