Sha256: 1702c0e656aa90d6894875fb8c3411a160ce1295b0238630cb3475063efb8b1d

Contents?: true

Size: 959 Bytes

Versions: 2

Compression:

Stored size: 959 Bytes

Contents

# frozen_string_literal: true

require "yaml"
require "erb"

module Confset
  module Sources
    class YAMLSource
      attr_accessor :path
      attr_reader :evaluate_erb

      def initialize(path, evaluate_erb: Confset.evaluate_erb_in_yaml)
        @path = path.to_s
        @evaluate_erb = !!evaluate_erb
      end

      # returns a config hash from the YML file
      def load
        if @path && File.exist?(@path)
          file_contents = IO.read(@path)
          file_contents = ERB.new(file_contents).result if evaluate_erb
          result = YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(file_contents) : YAML.load(file_contents)
        end

        result || {}

      rescue Psych::SyntaxError => e
        raise "YAML syntax error occurred while parsing #{@path}. " \
              "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
              "Error: #{e.message}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
confset-1.1.0 lib/confset/sources/yaml_source.rb
confset-1.0.3 lib/confset/sources/yaml_source.rb