Sha256: 9304820f372c615162a916d6e7e1c1033ca2d941f35577bbba0426f0c02f6b53

Contents?: true

Size: 868 Bytes

Versions: 1

Compression:

Stored size: 868 Bytes

Contents

require 'yaml'
require 'erb'

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

      def initialize(path, evaluate_erb: Config.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 and File.exist?(@path)
          file_contents = IO.read(@path)
          file_contents = ERB.new(file_contents).result if evaluate_erb
          result = 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

1 entries across 1 versions & 1 rubygems

Version Path
config-3.1.0 lib/config/sources/yaml_source.rb