Sha256: f19e7caec14e02b1ef0d3730936bf3cf2a4551c743123f68a5fbc836dc502ec5

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module TableSaw
  module DependencyGraph
    class Build
      attr_reader :manifest, :records

      def initialize(manifest)
        @manifest = manifest
        @records = {}
      end

      def call
        manifest.tables.values.sort_by { |t| t.partial? ? 1 : 0 }.each do |table|
          add TableSaw::DependencyGraph::AddDirective.new(table.name, ids: select_ids(table), partial: table.partial?)
        end

        records
      end

      private

      def add(directive)
        return [] unless directive.queryable?

        directives(directive).select(&:queryable?).each(&method(:add))
      end

      def directives(dir)
        record = records[dir.table_name]

        if record
          dir.partial? ? record.fetch_associations(dir) : []
        else
          TableSaw::DependencyGraph::DumpTable.new(context: context, name: dir.table_name, partial: dir.partial?)
            .tap { |table| records[dir.table_name] = table }.fetch_associations(dir)
        end
      end

      def context
        @context ||= TableSaw::DependencyGraph::Context.new(manifest)
      end

      def select_ids(table)
        return [] unless table.partial?

        context.perform_query(table.query).map { |row| row['id'] }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
table_saw-0.5.0 lib/table_saw/dependency_graph/build.rb
table_saw-0.4.0 lib/table_saw/dependency_graph/build.rb