Sha256: d9e0aceaf248c1e1c797a8772370ffc390ea01b38602410f4d17b33274301b9d

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

module Tdc
  #
  # Knows how to read data definitions from YAML files.
  #
  class DataDefinitionFileReader < Tdc::DataDefinition
    EMPTY_DEFINITIONS = []

    def initialize(catalog_root_directory)
      super()

      @catalog_root_directory = catalog_root_directory
    end

    def read(*path_elements)
      data_definition_from(definitions_file(path_elements))
    end

    private

    def definitions_file(path_elements)
      fully_qualified_path_elements = [@catalog_root_directory].concat(path_elements.map(&:to_s))

      fully_qualified_path_elements.last.concat(".yml")

      File.join(*fully_qualified_path_elements)
    end

    def data_definition_from(definitions_file)
      if File.exist?(definitions_file)
        load_yaml(definitions_file) || EMPTY_DEFINITIONS
      else
        missing_data_definition(definitions_file)
      end
    end

    def load_yaml(definitions_file)
      YAML.load(expand_erb(definitions_file)) # rubocop:disable Security/YAMLLoad
    rescue => e
      raise Tdc::FatalError, <<~MSG
        Unable to load YAML from #{definitions_file}
        Cause: #{e.message}"
      MSG
    end

    def expand_erb(definitions_file)
      ERB.new(File.read(definitions_file)).result
    end

    def missing_data_definition(definitions_file)
      return EMPTY_DEFINITIONS if ENV.key?("TDC_IGNORE_MISSING_TEST_DATA_DEFINITION_ERROR")

      raise Tdc::FatalError, "Missing test definitions file: '#{definitions_file}'"
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tdc-0.3.9 lib/tdc/data_definition_file_reader.rb
tdc-0.3.8 lib/tdc/data_definition_file_reader.rb
tdc-0.3.7 lib/tdc/data_definition_file_reader.rb
tdc-0.3.6.1 lib/tdc/data_definition_file_reader.rb