Sha256: 857a59af8e6939bf0a633e878739f9e72cff71f6ab735e580ea572413d8e4234

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require 'yaml'
require 'table_saw/associations'

module TableSaw
  class Manifest
    class Table
      attr_reader :variables, :config

      def initialize(variables, config)
        @variables = variables
        @config = config
      end

      def table
        config['table']
      end

      alias name table

      def query
        return unless partial?

        format(config['query'], variables.transform_keys(&:to_sym))
      end

      def partial?
        config.key?('query')
      end

      def has_many
        config.fetch('has_many', [])
      end
    end

    def self.instance
      raise ArgumentError, 'Could not find manifest file' unless File.exist?(TableSaw.configuration.manifest)

      new(YAML.safe_load(File.read(TableSaw.configuration.manifest)))
    end

    attr_reader :config

    def initialize(config)
      @config = config
    end

    def variables
      vars = config.fetch('variables', {})
      vars.merge(TableSaw.configuration.variables.slice(*vars.keys))
    end

    def tables
      @tables ||= config['tables'].map { |entry| Table.new(variables, entry) }.each_with_object({}) do |t, memo|
        memo[t.name] = t
      end
    end

    def has_many
      @has_many ||= config.fetch('has_many', {})
    end

    def foreign_keys
      @foreign_keys ||= config.fetch('foreign_keys', [])
    end

    def associations
      @associations ||= TableSaw::Associations.new(self)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
table_saw-2.9.0 lib/table_saw/manifest.rb