Module: Mongoid::Persistable

Extended by:
ActiveSupport::Concern
Includes:
Creatable, Deletable, Destroyable, Incrementable, Logical, Poppable, Pullable, Pushable, Renamable, Savable, Settable, Unsettable, Updatable, Upsertable
Included in:
Composable
Defined in:
lib/mongoid/persistable.rb,
lib/mongoid/persistable/logical.rb,
lib/mongoid/persistable/savable.rb,
lib/mongoid/persistable/poppable.rb,
lib/mongoid/persistable/pullable.rb,
lib/mongoid/persistable/pushable.rb,
lib/mongoid/persistable/settable.rb,
lib/mongoid/persistable/creatable.rb,
lib/mongoid/persistable/deletable.rb,
lib/mongoid/persistable/renamable.rb,
lib/mongoid/persistable/updatable.rb,
lib/mongoid/persistable/unsettable.rb,
lib/mongoid/persistable/upsertable.rb,
lib/mongoid/persistable/destroyable.rb,
lib/mongoid/persistable/incrementable.rb

Overview

Contains general behaviour for persistence operations.

Since:

  • 2.0.0

Defined Under Namespace

Modules: Creatable, Deletable, Destroyable, Incrementable, Logical, Poppable, Pullable, Pushable, Renamable, Savable, Settable, Unsettable, Updatable, Upsertable

Constant Summary collapse

LIST_OPERATIONS =

The atomic operations that deal with arrays or sets in the db.

Since:

  • 4.0.0

[ "$addToSet", "$push", "$pull", "$pullAll" ].freeze

Instance Method Summary collapse

Methods included from Unsettable

#unset

Methods included from Upsertable

#upsert

Methods included from Updatable

#update, #update!, #update_attribute

Methods included from Settable

#set

Methods included from Savable

#save, #save!

Methods included from Renamable

#rename

Methods included from Pushable

#add_to_set, #push

Methods included from Pullable

#pull, #pull_all

Methods included from Poppable

#pop

Methods included from Logical

#bit

Methods included from Incrementable

#inc

Methods included from Destroyable

#destroy, #destroy!

Methods included from Deletable

#delete

Methods included from Creatable

#insert

Instance Method Details

#atomicallytrue, false

Execute operations atomically (in a single database call) for everything that would happen inside the block.

Examples:

Execute the operations atomically.

document.atomically do
  document.set(name: "Tool").inc(likes: 10)
end

Returns:

  • (true, false)

    If the operation succeeded.

Since:

  • 4.0.0


55
56
57
58
59
60
61
62
63
64
# File 'lib/mongoid/persistable.rb', line 55

def atomically
  begin
    @atomic_updates_to_execute = {}
    yield(self) if block_given?
    persist_atomic_operations(@atomic_updates_to_execute)
    true
  ensure
    @atomic_updates_to_execute = nil
  end
end

#fail_due_to_callback!(method) ⇒ Object

Raise an error if a callback failed.

Examples:

Raise the callback error.

Person.fail_due_to_callback!(person, :create!)

Parameters:

  • document (Document)

    The document to fail.

  • method (Symbol)

    The method being called.

Raises:

Since:

  • 4.0.0


91
92
93
# File 'lib/mongoid/persistable.rb', line 91

def fail_due_to_callback!(method)
  raise Errors::Callback.new(self.class, method)
end

#fail_due_to_validation!Object

Raise an error if validation failed.

Examples:

Raise the validation error.

Person.fail_due_to_validation!(person)

Parameters:

  • document (Document)

    The document to fail.

Raises:

Since:

  • 4.0.0


76
77
78
# File 'lib/mongoid/persistable.rb', line 76

def fail_due_to_validation!
  raise Errors::Validations.new(self)
end