Class: TableSaw::BuildDependencyGraph
- Inherits:
-
Object
- Object
- TableSaw::BuildDependencyGraph
- Defined in:
- lib/table_saw/build_dependency_graph.rb
Instance Attribute Summary collapse
-
#manifest ⇒ Object
readonly
Returns the value of attribute manifest.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
Instance Method Summary collapse
- #add(table_name, ids) ⇒ Object
- #call ⇒ Object
-
#fetch_belongs_to_associations(table_name, ids) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#fetch_has_many_associations(table_name, ids) ⇒ Object
rubocop:enable Metrics/AbcSize.
-
#initialize(manifest) ⇒ BuildDependencyGraph
constructor
A new instance of BuildDependencyGraph.
Constructor Details
#initialize(manifest) ⇒ BuildDependencyGraph
Returns a new instance of BuildDependencyGraph.
11 12 13 14 |
# File 'lib/table_saw/build_dependency_graph.rb', line 11 def initialize(manifest) @manifest = manifest @records = Hash.new { |h, k| h[k] = [] } end |
Instance Attribute Details
#manifest ⇒ Object (readonly)
Returns the value of attribute manifest.
9 10 11 |
# File 'lib/table_saw/build_dependency_graph.rb', line 9 def manifest @manifest end |
#records ⇒ Object (readonly)
Returns the value of attribute records.
9 10 11 |
# File 'lib/table_saw/build_dependency_graph.rb', line 9 def records @records end |
Instance Method Details
#add(table_name, ids) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/table_saw/build_dependency_graph.rb', line 24 def add(table_name, ids) return if ids.empty? ids_to_add = ids - records[table_name] return if ids_to_add.empty? records[table_name].concat ids_to_add fetch_belongs_to_associations(table_name, ids_to_add) fetch_has_many_associations(table_name, ids_to_add) end |
#call ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/table_saw/build_dependency_graph.rb', line 16 def call manifest.tables.each_value do |table| add(table.name, perform_query(table.query).map { |row| row['id'] }) end records end |
#fetch_belongs_to_associations(table_name, ids) ⇒ Object
rubocop:disable Metrics/AbcSize
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/table_saw/build_dependency_graph.rb', line 36 def fetch_belongs_to_associations(table_name, ids) associations = belongs_to[table_name] return if associations.empty? rows = perform_query( format('select %{columns} from %{table_name} where id in (%{ids})', columns: associations.keys.join(','), table_name: table_name, ids: ids.join(',')) ) values = rows.each_with_object(Hash.new { |h, k| h[k] = Set.new }) do |row, memo| associations.each_key { |key| memo[key].add row[key] unless row[key].nil? } end associations.each { |from_column, to_table| add to_table, values[from_column].to_a } end |
#fetch_has_many_associations(table_name, ids) ⇒ Object
rubocop:enable Metrics/AbcSize
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/table_saw/build_dependency_graph.rb', line 53 def fetch_has_many_associations(table_name, ids) has_many.fetch(table_name, []).each do |table, column| next if tables_with_no_ids.include?(table) next unless has_many_mapping.fetch(table_name, []).include?(table) rows = perform_query( format('select id from %{table} where %{column} in (%{ids})', table: table, column: column, ids: ids.join(',')) ) add(table, rows.map { |row| row['id'] }) end end |