Sha256: 782a39bf533baadc914c96d1e759478f9d913133302ccf7dc32be7468c7b2301

Contents?: true

Size: 937 Bytes

Versions: 1

Compression:

Stored size: 937 Bytes

Contents

require 'yaml'
require 'symbolizer'
require 'erb'

# Helps to load conf files in salemove ecosystem
#
class ConfLoader
  EnvironmentNotFoundError = Class.new(KeyError)

  # Load given conf file
  #
  # @path [String] path
  #   full path to conf
  # @path [String] env
  #   environment to load
  #
  # @return [Hash]
  #   hash with symbolized keys
  #
  # @api public
  def self.load(path, env)
    template = ERB.new File.new(path).read
    environments = YAML.load template.result(binding)

    if environments.has_key?(env)
      hash = environments[env]
      guarantee_key_presence(Symbolizer.symbolize(hash))
    else
      raise EnvironmentNotFoundError,
        "Configuration for `#{env}` not found at path #{path}"
    end
  end

  def self.guarantee_key_presence(hash)
    hash.default_proc = proc do |h, k|
      raise KeyError, "#{k} not defined"
    end
    hash
  end

  private_class_method :guarantee_key_presence
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
conf_loader-1.0.0 lib/conf_loader.rb