Class: DatabaseCleaner::Spanner::TableDependency

Inherits:
Hash
  • Object
show all
Includes:
TSort
Defined in:
lib/database_cleaner/spanner/table_dependency.rb

Instance Method Summary collapse

Constructor Details

#initializeTableDependency

Returns a new instance of TableDependency.



9
10
11
# File 'lib/database_cleaner/spanner/table_dependency.rb', line 9

def initialize
  @parents = Hash.new { |h, k| h[k] = Set.new }
end

Instance Method Details

#add_child(parent, child) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/database_cleaner/spanner/table_dependency.rb', line 13

def add_child(parent, child)
  store(parent, Set.new) unless key?(parent)

  return if child.nil?

  store(child, Set.new) unless key?(child)

  fetch(parent).add(child)

  @parents[child].add(parent)
end

#add_children(parent, children) ⇒ Object



25
26
27
28
# File 'lib/database_cleaner/spanner/table_dependency.rb', line 25

def add_children(parent, children)
  store(parent, children || Set.new)
  children.each { |child| @parents[child].add(parent) }
end

#divideObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/database_cleaner/spanner/table_dependency.rb', line 30

def divide
  groups = []
  visited = Set.new

  each_table do |table|
    next if visited.include?(table)

    group = TableDependency.new
    dfs(table, group, visited)
    groups << group
  end

  groups
end

#tsort_each_child(node, &block) ⇒ Object



47
48
49
# File 'lib/database_cleaner/spanner/table_dependency.rb', line 47

def tsort_each_child(node, &block)
  fetch(node).each(&block)
end