Sha256: 43b6f8926424bbd2dfd3204587394ed8eea4d4d5afbfc8bf5420f91724931eec

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

module Tdc
  #
  # Knows how to read test data definitions from YAML files.
  #
  class TestDataDefinitionReader
    EMPTY_DEFINITIONS = []

    def initialize(catalog_root_directory)
      @catalog_root_directory = catalog_root_directory
    end

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

    def with_indifferent_access
      self.extend(Tdc::WithIndifferentAccessDecorator) # rubocop:disable Style/RedundantSelf
    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::YamlLoadError, <<~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 MissingTestDataDefinitionError, definitions_file
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tdc-0.1.2 lib/tdc/test_data_definition_reader.rb