模块:Mongoid::Association::Referenced::Syncable

包含在:
Mongoid::Association
定义于:
lib/mongoid/association/referenced/syncable.rb

Overview

此模块处理在多对多关联的双方之间同步外键的行为。

在命名空间下定义

模块: 类方法

实例方法摘要折叠

实例方法详细信息

# _syncable? (association) ⇒ true | false

文档能否反向同步? 仅当键已更改且关联绑定尚未运行才会出现这种情况。

例子:

外键是否可同步?

document._syncable?(association)

参数:

返回:

  • ( true | false )

    如果我们可以同步的话。



18
19
20
# File 'lib/mongoid/association/referenced/syncable.rb', line 18

def _syncable?(关联)
  !_synced?(关联.foreign_key) && 发送(关联.foreign_key_check)
end

# _synced哈希

获取同步的外键。

例子:

获取同步的外键。

document._synced

返回:

  • (哈希)

    同步的外键。



28
29
30
# File 'lib/mongoid/association/referenced/syncable.rb', line 28

def _synced
  @_synced ||= {}
end

# _synced? (foreign_key) ⇒ true | false

文档是否已针对外键进行同步?

例子:

文档是否已同步?

document._synced?

参数:

  • foreign_key ( string )

    外键。

返回:

  • ( true | false )

    如果我们可以同步的话。



40
41
42
# File 'lib/mongoid/association/referenced/syncable.rb', line 40

def _synced?(foreign_key)
  !!_synced[foreign_key]
end

# remove_inverse_keys (association) ⇒对象

在销毁时更新反向键。

例子:

更新反向键。

document.remove_inverse_keys(association)

参数:

返回:

  • ( Object )

    更新后的值。



52
53
54
55
56
57
# File 'lib/mongoid/association/referenced/syncable.rb', line 52

def remove_inverse_keys(关联)
  foreign_keys = 发送(关联.foreign_key)
  return if foreign_keys.nil? || foreign_keys.空?

  关联.条件(self, foreign_keys).pull(关联.inverse_foreign_key => _id)
end

# update_inverse_keys (association) ⇒对象

更新关联的反向键。

例子:

更新反向键

document.update_inverse_keys(association)

参数:

返回:

  • ( Object )

    更新后的值。



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mongoid/association/referenced/syncable.rb', line 67

def update_inverse_keys(关联)
  return 除非 previous_changes.has_key?(关联.foreign_key)

  , new = previous_changes[关联.foreign_key]
  增加, subs = new - ( || []), ( || []) - new

  # 如果是自动保存,则不希望添加重复项 - the
  # $addToSet 会先运行,然后 $push 和 $each
  # inverse on the autosave 会导致此问题。 我们删除每个ID
  # 如果同时使用 ID 加法和对象加法,内存中的内容是什么
  发生了 # 次。
  if 关联.autosave?
    发送(关联.名称).in_memory. do |doc|
      增加.delete_one(doc._id)
    end
  end

  除非 增加.空?
    关联.条件(self, 增加).without_options.add_to_set(关联.inverse_foreign_key => _id)
  end
  return if subs.空?

  关联.条件(self, subs).without_options.pull(关联.inverse_foreign_key => _id)
end