Class: Mongoid::Relations::Embedded::Atomic::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/relations/embedded/atomic/operation.rb

Direct Known Subclasses

Pull, PushAll, Set, Unset

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#documentsObject

Returns the value of attribute documents.



8
9
10
# File 'lib/mongoid/relations/embedded/atomic/operation.rb', line 8

def documents
  @documents
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/mongoid/relations/embedded/atomic/operation.rb', line 8

def options
  @options
end

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/mongoid/relations/embedded/atomic/operation.rb', line 8

def path
  @path
end

#selectorObject

Returns the value of attribute selector.



8
9
10
# File 'lib/mongoid/relations/embedded/atomic/operation.rb', line 8

def selector
  @selector
end

Instance Method Details

#consume(selector, operations, options = {}) ⇒ Object

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

Examples:

Consume the operation.

set.consume(
  { "_id" => BSON::ObjectId.new },
  { "$push" => { "addresses" => { "_id" => "street" } } },
  { :multi => false, :safe => true }
)

Parameters:

  • selector (Hash)

    The document selector.

  • operations (Hash)

    The ops to set in the db.

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

    The persistence options.

Options Hash (options):

  • :multi (true, false)

    Persist multiple at once.

  • :safe (true, false)

    Persist in safe mode.

Since:

  • 2.0.0



28
29
30
31
32
# File 'lib/mongoid/relations/embedded/atomic/operation.rb', line 28

def consume(selector, operations, options = {})
  @consumed, @selector, @options = true, selector, options
  @documents ||= []
  parse(operations)
end

#consumed?true, false

Has this operation consumed any executions?

Examples:

Is this consumed?

unset.consumed?

Returns:

  • (true, false)

    If the operation has consumed anything.

Since:

  • 2.0.0



42
43
44
# File 'lib/mongoid/relations/embedded/atomic/operation.rb', line 42

def consumed?
  !!@consumed
end

#execute(collection) ⇒ Object

Execute the $pushAll operation on the collection.

Examples:

Execute the operation.

unset.execute(collection)

Parameters:

  • collection (Collection)

    The root collection.

Since:

  • 2.0.0



54
55
56
57
58
# File 'lib/mongoid/relations/embedded/atomic/operation.rb', line 54

def execute(collection)
  if collection && consumed?
    collection.update(selector, operations, options)
  end
end