Sha256: 2aca9412a74cbc69c862c199c6444d5c94bc7b6dd2615407db8f0711335b40b5

Contents?: true

Size: 891 Bytes

Versions: 1

Compression:

Stored size: 891 Bytes

Contents

require 'yaml'
require 'erb'

module Config
  module Sources
    class YAMLSource
      attr_accessor :path, :namespace

      def initialize(path, namespace=nil)
        @path = path
        @namespace = namespace
      end

      # returns a config hash from the YML file
      def load
        if @path and File.exist?(@path.to_s)
          result = YAML.load(ERB.new(IO.read(@path.to_s)).result)
        end
        if self.namespace
          namespaces = self.namespace.split("/")
          result = namespaces.reverse.inject(result || {}) { |acc, space| { space => acc } }
        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
ef-config-1.4.1 lib/config/sources/yaml_source.rb