模块:Mongoid::Persistable

扩展方式:
ActiveSupport::Concern
包括:
Creatable Deletable Destroyable Incrementable 、 Logical Maxable Minable Multipliable Poppable Pullable Pushable Renameable Saveable Settable Unsettable Updatable 、 Upsertable 、 Positional
包含在:
可组合
定义于:
lib/mongoid/persistable.rb ,
lib/mongoid/persistable/logic.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/renameable.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

包含持久性操作的一般行为。

在命名空间下定义

模块: Creatable Deletable Destroyable Incrementable Logical Maxable Minable Multipliable Poppable Pullable Pushable Renameable Saveable Settable 、 Unsettable 、 Updateable Upsertable

常量摘要折叠

LIST_OPERATIONS =

处理数据库中数组或集的原子操作。

[ "$addToSet", " $push ", " $pull ", " $pullAll " ].冻结

实例方法摘要折叠

Unsettable 中包含的方法

#unset

Upsertable 中包含的方法

#upsert

Updateable中包含的方法

#update, #update!, #update_attribute

Settable中包含的方法

#set

Saveable中包含的方法

#save, #save!

Renameable 中包含的方法

#rename

Pushable 中包含的方法

#add_to_set#push

Pullable 中包含的方法

#pull , #pull_all

位置包含的方法

#positionally

Poppable 中包含的方法

#pop

Multipliable中包含的方法

#mul

Minable 中包含的方法

#set_min

Maxable中包含的方法

#set_max

包含在以下方法中的方法

#bit

Incrementable中包含的方法

#inc

Destroyable中包含的方法

#destroy , #destroy!

Deletable包含的方法

# 删除

Createtable中包含的方法

#insert

实例方法详细信息

#自动(join_context: nil) ⇒ true | false

对区块内可能发生的所有事情以原子方式(在单个数据库调用中)执行操作。 此方法支持以原子方式嵌套进一步调用,这将根据下述选项运行。

可以指定 join_context 选项,为 true 时,将把给定块声明的操作与包装同一文档的当前调用的原子块(如果存在)合并。 如果此区块或共享相同上下文的任何其他区块在持久化之前引发,则该上下文的所有操作都将不会被持久化,并且也将在内存中重置。

当 join_context 为 false 时,给定的操作块将独立于其他上下文而持久保存。 只要此区块能够运行并持久化更改,其他上下文中的故障就不会影响此上下文。

join_context 的默认值由全局配置选项 join_contexts设立,该选项自身的默认值为 false。

例子:

以原子方式执行操作。

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

以原子方式执行某些内部操作,但独立于外部操作。


document.atomically do
  document.inc likes: 10
  document.atomically join_context: false do
    # The following is persisted to the database independently.
    document.unset :origin
  end
  document.atomically join_context: true do
    # The following is persisted along with the other outer operations.
    document.inc member_count: 3
  end
  document.set name: "Tool"
end

参数:

  • join_context ( true | false ) (默认为: nil

    加入上下文(即 合并声明的原子操作)的原子性区块,为同一文档包装此操作(如果存在)。

返回:

  • ( true | 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 原子地(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
    产量(self)
    @atomic_depth -= 1
  end

  if has_own_context
    persist_atomic_operations @atomic_context
    _mongoid_remove_atomic_context_changes
  end

  true
救援 StandardError => e
  _mongoid_reset_atomic_context_changes! if has_own_context
  提高 e
确保
  _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! (方法)→对象

如果回调失败,则引发错误。

例子:

引发回调错误。

Person.fail_due_to_callback!(person, :create!)

参数:

  • 方法 (符号)

    正在调用的方法。

引发:



143
144
145
# File 'lib/mongoid/persistable.rb', line 143

def fail_due_to_callback!(方法)
  提高 Errors::Callback.new(self.class, 方法)
end

# fail_due_to_validation!对象

如果验证失败,则引发错误。

例子:

引发验证错误。

Person.fail_due_to_validation!(person)

引发:



131
132
133
# File 'lib/mongoid/persistable.rb', line 131

def fail_due_to_validation!
  提高 Errors::验证.new(self)
end