Sha256: fc5bdb8a140dc764f287b73da8f5c39334b055fae4eaec5c0322a3a82f339382

Contents?: true

Size: 1.19 KB

Versions: 5

Compression:

Stored size: 1.19 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
        EMPTY_DEFINITIONS
      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
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tdc-0.4.3.1 lib/tdc/data_definition_file_reader.rb
tdc-0.4.3 lib/tdc/data_definition_file_reader.rb
tdc-0.4.2 lib/tdc/data_definition_file_reader.rb
tdc-0.4.1 lib/tdc/data_definition_file_reader.rb
tdc-0.4.0 lib/tdc/data_definition_file_reader.rb