Module: Mongoid::Persistable
- Extended by:
- ActiveSupport::Concern
- Includes:
- Creatable, Deletable, Destroyable, Incrementable, Logical, Maxable, Minable, Multipliable, 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/maxable.rb,
lib/mongoid/persistable/minable.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/multipliable.rb,
lib/mongoid/persistable/incrementable.rb
Overview
Contains general behavior for persistence operations.
Defined Under Namespace
Modules: Creatable, Deletable, Destroyable, Incrementable, Logical, Maxable, Minable, Multipliable, 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.
[ "$addToSet", "$push", "$pull", "$pullAll" ].freeze
Instance Method Summary collapse
-
#atomically(join_context: nil) ⇒ true | false
Execute operations atomically (in a single database call) for everything that would happen inside the block.
-
#fail_due_to_callback!(method) ⇒ Object
Raise an error if a callback failed.
-
#fail_due_to_validation! ⇒ Object
Raise an error if validation failed.
Methods included from Unsettable
Methods included from Upsertable
Methods included from Updatable
#update, #update!, #update_attribute
Methods included from Settable
Methods included from Savable
Methods included from Renamable
Methods included from Pushable
Methods included from Pullable
Methods included from Positional
Methods included from Poppable
Methods included from Multipliable
Methods included from Minable
Methods included from Maxable
Methods included from Logical
Methods included from Incrementable
Methods included from Destroyable
Methods included from Deletable
Methods included from Creatable
Instance Method Details
#atomically(join_context: nil) ⇒ true | false
Execute operations atomically (in a single database call) for everything that would happen inside the block. This method supports nesting further calls to atomically, which will behave according to the options described below.
An option join_context can be given which, when true, will merge the operations declared by the given block with the atomically block wrapping the current invocation for the same document, if one exists. If this block or any other block sharing the same context raises before persisting, then all the operations of that context will not be persisted, and will also be reset in memory.
When join_context is false, the given block of operations will be persisted independently of other contexts. Failures in other contexts will not affect this one, so long as this block was able to run and persist changes.
The default value of join_context is set by the global configuration option join_contexts, whose own default is false.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/mongoid/persistable.rb', line 94 def atomically(join_context: nil) join_context = Mongoid.join_contexts if join_context.nil? call_depth = @atomic_depth ||= 0 has_own_context = call_depth.zero? || !join_context @atomic_updates_to_execute_stack ||= [] _mongoid_push_atomic_context if has_own_context if block_given? @atomic_depth += 1 yield(self) @atomic_depth -= 1 end if has_own_context persist_atomic_operations @atomic_context _mongoid_remove_atomic_context_changes end true rescue StandardError => e _mongoid_reset_atomic_context_changes! if has_own_context raise e ensure _mongoid_pop_atomic_context if has_own_context if call_depth.zero? @atomic_depth = nil @atomic_updates_to_execute_stack = nil end end |
#fail_due_to_callback!(method) ⇒ Object
Raise an error if a callback failed.
143 144 145 |
# File 'lib/mongoid/persistable.rb', line 143 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.
131 132 133 |
# File 'lib/mongoid/persistable.rb', line 131 def fail_due_to_validation! raise Errors::Validations.new(self) end |