module Ratch::ConfigUtils

Utility extensions for working with configuration files.

Public Class Methods

extended(base) click to toggle source
# File lib/ratch/utils/config.rb, line 14
def self.extended(base)
  included(base)
end
included(base) click to toggle source
# File lib/ratch/utils/config.rb, line 10
def self.included(base)
  require 'yaml'
end

Public Instance Methods

configuration(file) click to toggle source

Load configuration data from a file. Results are cached and an empty Hash is returned if the file is not found.

Since they are YAML files, they can optionally end with ‘.yaml’ or ‘.yml’.

# File lib/ratch/utils/config.rb, line 22
def configuration(file)
  @configuration ||= {}
  @configuration[file] ||= (
    begin
      configuration!(file)
    rescue LoadError
      Hash.new{ |h,k| h[k] = {} }
    end
  )
end
configuration!(file) click to toggle source

Load configuration data from a file. The “bang” version will raise an error if file is not found. It also does not cache the results.

Since they are YAML files, they can optionally end with ‘.yaml’ or ‘.yml’.

# File lib/ratch/utils/config.rb, line 37
def configuration!(file)
  @configuration ||= {}
  patt = file + "{.yml,.yaml,}"
  path = Dir.glob(patt, File::FNM_CASEFOLD).find{ |f| File.file?(f) }
  if path
    # The || {} is in case the file is empty.
    data = YAML::load(File.open(path)) || {}
    @configuration[file] = data
  else
    raise LoadError, "Missing file -- #{path}"
  end
end