模块:Mongoid::Changeable
Overview
定义脏跟踪的行为。
在命名空间下定义
实例方法摘要折叠
-
#attribute_before_last_save (attr) ⇒ 对象
返回属性在上次保存之前的原始值。
-
# Changed ⇒ Array<String>
获取文档的已更改属性。
-
#已更改? ⇒ true | false
文档是否已更改?
-
# Changed_attributes ⇒ Hash< string , Object>
获取属性更改。
-
#changes ⇒ Hash< string , Array<Object, Object> ] 更改。
获取文档的所有更改。
-
#children_changed? ⇒ true | false
此文档的任何子文档(嵌入式文档)是否已更改?
-
#children_may_have_changed! ⇒ 对象
private
表示此文档的子文档可能已更改,应在验证文档时进行检查。
-
# move_changes ⇒ 对象
保存后调用此方法,以便可以正确切换更改。
-
# post_persist ⇒ 对象
在持久化文档后需要执行的操作。
-
# previous_changes ⇒ Hash< string , Array<Object, Object> ] 以前的更改。
获取文档的先前更改。
-
# remove_change (name) ⇒ 对象
从脏属性哈希中删除更改。
-
#saved_change_to_attribute(attr) ⇒ Array<Object> | nil
返回上次保存期间对属性的更改。
-
# Saved_change_to_attribute? (attr, from: Utils::PLACEHOLDER, to: Utils::PLACEHOLDER) ⇒ true | false
返回此属性在上次保存期间是否已更改。
-
# setter ⇒ 哈希
获取每个已更改字段的所有新值,并将其传递给MongoDB $ 设立修饰符。
-
#will_save_change_to_attribute?(attr, **kwargs) ⇒ true | false
返回此属性在下次保存时是否会更改。
实例方法详细信息
#attribute_before_last_save (attr) ⇒对象
返回属性在上次保存之前的原始值。
此方法在回调后非常有用,可获取 的原始值
an attribute before the save that triggered the callbacks to run.
150 151 152 153 |
# File 'lib/mongoid/changeable.rb', line 150 def attribute_before_last_save(attr) attr = database_field_name(attr) properties_before_last_save[attr] end |
# Changed ⇒ Array<String>
获取文档的已更改属性。
14 15 16 |
# File 'lib/mongoid/changeable.rb', line 14 def 已更改 Changed_attributes.密钥.SELECT { |attr| attribute_change(attr) } end |
#已更改? ⇒ true | false
文档是否已更改?
32 33 34 |
# File 'lib/mongoid/changeable.rb', line 32 def 改变? 更改.values.任何? { |val| val } || children_changed? end |
# Changed_attributes ⇒ Hash< string , Object>
获取属性更改。
51 52 53 |
# File 'lib/mongoid/changeable.rb', line 51 def Changed_attributes @changed_attributes ||= {} end |
#changes ⇒ Hash< string , Array<Object, Object> ]更改。
获取文档的所有更改。
61 62 63 64 65 66 |
# File 'lib/mongoid/changeable.rb', line 61 def 更改 已更改.each_with_object({}) do |attr, 更改| 更改 = attribute_change(attr) 更改[attr] = 更改 if 更改 end.with_indifference_access end |
#children_changed? ⇒ true | false
注意:
这是有意只考虑子节点而不是后代。
此文档的任何子文档(嵌入式文档)是否已更改?
41 42 43 |
# File 'lib/mongoid/changeable.rb', line 41 def children_changed? @children_may_have_changed || _children.任何?(和:changed?) end |
#children_may_have_changed! ⇒ 对象
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
表示此文档的子文档可能已更改,应在验证文档时进行检查。
22 23 24 |
# File 'lib/mongoid/changeable.rb', line 22 def children_may_have_changed! @children_may_have_changed = true end |
# move_changes ⇒对象
保存后调用此方法,以便可以正确切换更改。
这将取消设置已记忆的子数组,将新记录标志设置为 false,将文档设置为已验证,并移动脏更改。
75 76 77 78 79 80 81 82 83 |
# File 'lib/mongoid/changeable.rb', line 75 def move_changes @changes_before_last_save = @previous_changes @previous_changes = 更改 @attributes_before_last_save = @previous_attributes @previous_attributes = 属性.dup @children_may_have_changed = false reset_atomic_updates! Changed_attributes.清除 end |
# post_persist ⇒对象
在持久化文档后需要执行的操作。
89 90 91 92 93 |
# File 'lib/mongoid/changeable.rb', line 89 def post_persist reset_persisted_descendants reset_attributes_before_type_cast move_changes end |
# previous_changes ⇒ Hash< string , Array<Object, Object> ] 以前的更改。
获取文档的先前更改。
101 102 103 |
# File 'lib/mongoid/changeable.rb', line 101 def previous_changes @previous_changes ||= {} end |
# remove_change (name) ⇒对象
从脏属性哈希中删除更改。 由单字段原子更新器使用。
112 113 114 |
# File 'lib/mongoid/changeable.rb', line 112 def remove_change(名称) Changed_attributes.删除(名称.to_s) end |
#saved_change_to_attribute(attr) ⇒ Array<Object> | nil
返回上次保存期间对属性的更改。
161 162 163 164 |
# File 'lib/mongoid/changeable.rb', line 161 def saved_change_to_attribute(attr) attr = database_field_name(attr) previous_changes[attr] end |
# Saved_change_to_attribute? (attr, from: Utils::PLACEHOLDER, to: Utils::PLACEHOLDER) ⇒ true | false
返回此属性在上次保存期间是否已更改。
此方法在回调后非常有用,可以查看更改
in an attribute during the save that triggered the callbacks to run.
176 177 178 179 180 181 182 183 184 185 |
# File 'lib/mongoid/changeable.rb', line 176 def Saved_change_to_attribute?(attr, 来自: Utils::占位符, 至: Utils::占位符) 更改 = saved_change_to_attribute(attr) return false 除非 更改.is_a?(阵列) return true if Utils.placeholder?(from) && Utils.placeholder?(to) return 更改.first == from if Utils.placeholder?(to) return 更改.last == to if Utils.placeholder?(from) 更改.first == from && 更改.last == to end |
# setter ⇒哈希
获取每个已更改字段的所有新值,并将其传递给MongoDB $ 设立修饰符。
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/mongoid/changeable.rb', line 125 def setter mods = {} 更改.each_pair do |名称, 更改| 来年 除非 更改 旧, new = 更改 字段 = 字段[名称] key = atomic_attribute_name(名称) if 字段&。可调整大小? 字段.add_atomic_changes(self, 名称, key, mods, new, 旧) else mods[key] = new 除非 atomic_unsets.包括?(key) end end mods end |
#will_save_change_to_attribute?(attr, **kwargs) ⇒ true | false
返回此属性在下次保存时是否会更改。
此方法在验证中和回调之前很有用,以确定
if the next call to save will change a particular attribute.
199 200 201 |
# File 'lib/mongoid/changeable.rb', line 199 def will_save_change_to_attribute?(attr, **kwargs) attribute_changed?(attr, **kwargs) end |