Class: Databender::TableOrder

Inherits:
Object
  • Object
show all
Defined in:
lib/databender/table_order.rb

Class Method Summary collapse

Class Method Details

.order_by_foreign_key_dependency(source, db_name, tables_with_rank) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/databender/table_order.rb', line 6

def order_by_foreign_key_dependency(source, db_name, tables_with_rank)
  @tables_with_rank = unique_group_by(tables_with_rank, :name).with_indifferent_access
  @dependency_map = source.foreign_key_dependency_map_for(db_name).with_indifferent_access
  tables_with_no_dependencies = @tables_with_rank.keys - @dependency_map.keys
  tables_with_no_dependencies.each { |table_name| @tables_with_rank[table_name].rank = 0 }
  @dependency_map.keys.each do |table|
    rank_for(table)
  end
  @tables_with_rank.values.flatten.sort {|x,y| x.rank <=> y.rank}
end

.parent_tables_for(table_name) ⇒ Object



29
30
31
# File 'lib/databender/table_order.rb', line 29

def parent_tables_for(table_name)
  @dependency_map[table_name]
end

.rank_for(table) ⇒ Object



17
18
19
20
21
# File 'lib/databender/table_order.rb', line 17

def rank_for(table)
  @tables_with_rank[table].rank ||= @dependency_map[table].collect do |parent|
                                        @tables_with_rank[parent.ref_table_name].rank ||= rank_for(parent.ref_table_name)
                                    end.max + 1
end

.unique_group_by(array, key) ⇒ Object



23
24
25
26
27
# File 'lib/databender/table_order.rb', line 23

def unique_group_by(array, key)
  array.each_with_object({}) do |element, hash|
    hash[element.send(key)] = element
  end
end