模块:Mongoid::Interceptable
Overview
该模块包含 Mongoid 的所有回调钩子。
常量摘要折叠
- CALLBACKS =
%i[ after_build after_create after_destroy after_find after_initialize after_save after_touch after_update after_upsert after_validation around_create around_destroy around_save around_update around_upsert before_create before_destroy before_save before_update before_upsert before_validation ].冻结
实例方法摘要折叠
-
# _mongoid_run_child_after_callbacks (callback_list: []) ⇒ 对象
执行 after 回调。
-
# _mongoid_run_child_before_callbacks (Kind,children:[],callback_list:[]) ⇒ 对象
private
对嵌入式文档执行给定类型的 before 回调。
-
# _mongoid_run_child_callbacks (kind,children: nil, &block) ⇒ 对象
private
为嵌入式文档运行回调。
-
# _mongoid_run_child_callbacks_with_round (kind,children: nil, &block) ⇒ 对象
为嵌入式文档执行给定类型的回调,包括周围回调。
-
# _mongoid_run_child_callbacks_Without_round (Kind,children: nil, &block) ⇒ 对象
private
为没有周围回调的嵌入式文档执行给定类型的回调。
-
# callback_executable? (种类)→ true | false
所提供的回调类型是否可由此文档执行?
-
# in_callback_state? (种类)→ true | false
文档当前是否处于可能需要执行回调的状态?
-
#pending_callbacks ⇒ Array<Symbol>
private
返回存储的回调以供稍后执行。
-
# pending_callbacks= (value) ⇒ Array<Symbol>
private
存储稍后执行的回调。
-
# run_after_callbacks (*kinds) ⇒ 对象
仅为特定事件运行后续回调。
-
# run_before_callbacks (*kinds) ⇒ 对象
仅为特定事件运行 before 回调。
-
# run_callbacks (Kind, with_children: true, skip_if: nil, &block) ⇒ 对象
为文档运行回调。
-
# run_pending_callbacks ⇒ 对象
private
运行待处理的回调。
实例方法详细信息
# _mongoid_run_child_after_callbacks (callback_list: []) ⇒对象
执行 after 回调。
247 248 249 250 251 252 |
# File 'lib/mongoid/interceptable.rb', line 247 def _mongoid_run_child_after_callbacks(callback_list: []) callback_list.reverse_each do |next_sequence, env| next_sequence.invoke_after(env) return false if env.已停止 end end |
# _mongoid_run_child_before_callbacks (Kind,children:[],callback_list:[]) ⇒对象
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
对嵌入式文档执行给定类型的 before 回调。
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/mongoid/interceptable.rb', line 225 def _mongoid_run_child_before_callbacks(kind, 子项: [], callback_list: []) 子项.每 do |子项| 链 = 子项.__callbacks[child_callback_type(kind, 子项)] env = ActiveSupport::回调::筛选器::environment.new(子项, false, nil) next_sequence = compile_callbacks(链) 除非 next_sequence.final? Mongoid.记录器.WARN(" 对于嵌入式文档,禁用了周围回调。跳过 #{ child. class.name} 的 周围回调 。 ") Mongoid.记录器.WARN(' 要为嵌入式文档启用周围回调,请将 Mongoid::Config.round_callbacks_for_embeds设立为 true。 ') end next_sequence.invoke_before(env) return false if env.已停止 env.值 = !env.已停止 callback_list << [ next_sequence, env ] end callback_list end |
# _mongoid_run_child_callbacks (kind,children: nil, &block) ⇒对象
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
为嵌入式文档运行回调。
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/mongoid/interceptable.rb', line 148 def _mongoid_run_child_callbacks(kind, 子项: nil, 和块) if Mongoid::配置. _mongoid_run_child_callbacks_with_round(kind, 子项: 子项, 和块) else _mongoid_run_child_callbacks_Without_round(kind, 子项: 子项, 和块) end end |
# _mongoid_run_child_callbacks_with_round (kind,children: nil, &block) ⇒对象
为嵌入式文档执行给定类型的回调,包括周围回调。
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/mongoid/interceptable.rb', line 169 def _mongoid_run_child_callbacks_with_round(kind, 子项: nil, 和块) 子项 ||= cascadable_children(kind) with_children = !Mongoid::配置. return 块&。调用 if 子项.空? 纤维 = 子项.map do |子项| 光纤.new do 子项.run_callbacks(child_callback_type(kind, 子项), with_children: with_children) do 光纤.产量 end end end 纤维.每 do |纤维| 纤维.resume 提高 Mongoid::Errors::InvalidAroundCallback 除非 纤维.alive? end 块&。调用 纤维.reverse.每(和:resume) end |
# _mongoid_run_child_callbacks_Without_round (Kind,children: nil, &block) ⇒对象
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
为没有周围回调的嵌入式文档执行给定类型的回调。
202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/mongoid/interceptable.rb', line 202 def _mongoid_run_child_callbacks_Without_round(kind, 子项: nil, 和块) 子项 ||= cascadable_children(kind) callback_list = _mongoid_run_child_before_callbacks(kind, 子项: 子项) return false if callback_list == false 值 = 块&。调用 callback_list.每 do |_next_sequence, env| env.值 &&= 值 end return false if _mongoid_run_child_after_callbacks(callback_list: callback_list) == false 值 end |
# callback_executable? (种类)→ true | false
本文档是否可执行所提供的回调类型?
60 61 62 |
# File 'lib/mongoid/interceptable.rb', line 60 def callback_executable?(kind) respond_to?(" _ #{ Kind} _callbacks ") end |
# in_callback_state? (种类)→ true | false
文档当前是否处于可能需要执行回调的状态?
73 74 75 |
# File 'lib/mongoid/interceptable.rb', line 73 def in_callback_state?(kind) %i[创建 销毁].包括?(kind) || new_record? || flagged_for_destroy? || 改变? end |
#pending_callbacks ⇒ Array<Symbol>
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
返回存储的回调以供稍后执行。
259 260 261 |
# File 'lib/mongoid/interceptable.rb', line 259 def pending_callbacks @pending_callbacks ||= [].to_set end |
# pending_callbacks= (value) ⇒ Array<Symbol>
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
存储稍后执行的回调。 一个很好的使用案例是延迟 after_find 和 after_initialize 回调,直到在文档上设立关联。 这也可用于延迟对文档应用默认值。
273 274 275 |
# File 'lib/mongoid/interceptable.rb', line 273 def ped_callbacks=(值) @pending_callbacks = 值 end |
# run_after_callbacks (*kinds) ⇒对象
ActiveSupport 默认不允许这种行为,因此 Mongoid 必须绕过它并自行实现。
仅为特定事件运行后续回调。
88 89 90 91 92 |
# File 'lib/mongoid/interceptable.rb', line 88 def run_after_callbacks(*kinds) kinds.每 do |kind| run_targeted_callbacks(:after, kind) end end |
# run_before_callbacks (*kinds) ⇒对象
ActiveSupport 默认不允许这种行为,因此 Mongoid 必须绕过它并自行实现。
仅为特定事件运行 before 回调。
105 106 107 108 109 |
# File 'lib/mongoid/interceptable.rb', line 105 def run_before_callbacks(*kinds) kinds.每 do |kind| run_targeted_callbacks(:before, kind) end end |
# run_callbacks (Kind, with_children: true, skip_if: nil, &block) ⇒对象
为 document 运行回调。这会覆盖主动支持的功能,以将回调级联到已标记为此类的嵌入式文档。
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/mongoid/interceptable.rb', line 125 def run_callbacks(kind, with_children: true, skip_if: nil, 和块) return 块&。调用 if skip_if&。调用 if with_children cascadable_children(kind).每 do |子项| return false if 子项.run_callbacks(child_callback_type(kind, 子项), with_children: with_children) == false end end if callback_executable?(kind) 超(kind, 和块) else true end end |
# run_pending_callbacks ⇒对象
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
运行待处理的回调。 如果回调为 :apply_defaults,我们将应用此文档的默认值。 否则,回调将传递给 run_callbacks 函数。
282 283 284 285 286 287 288 289 290 291 |
# File 'lib/mongoid/interceptable.rb', line 282 def run_pending_callbacks pending_callbacks.每 do |cb| if %i[apply_defaults apply_post_processed_defaults].包括?(cb) 发送(cb) else run_callbacks(cb, with_children: false) end end pending_callbacks.清除 end |