Module: Mongoid::Persistable

Extended by:
ActiveSupport::Concern
Includes:
Creatable, Deletable, Destroyable, Incrementable, Logical, Poppable, Pullable, Pushable, Renamable, Savable, Settable, Unsettable, Updatable, Upsertable, Positional
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

Instance Method Summary collapse

Methods included from Unsettable

#unset

Methods included from Upsertable

#upsert

Methods included from Updatable

#update, #update!, #update_attribute, #update_document

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 Positional

#positionally

Methods included from Poppable

#pop

Methods included from Logical

#bit

Methods included from Incrementable

#inc

Methods included from Destroyable

#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



51
52
53
54
55
56
57
58
59
60
# File 'lib/mongoid/persistable.rb', line 51

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



87
88
89
# File 'lib/mongoid/persistable.rb', line 87

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



72
73
74
# File 'lib/mongoid/persistable.rb', line 72

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