Module: Ratch::ConfigUtils

Defined in:
lib/ratch/utils/config.rb

Overview

Utility extensions for working with configuration files. – TODO: Perhaps utilize confectionary gem in future? ++

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



14
15
16
# File 'lib/ratch/utils/config.rb', line 14

def self.extended(base)
  included(base)
end

.included(base) ⇒ Object



10
11
12
# File 'lib/ratch/utils/config.rb', line 10

def self.included(base)
  require 'yaml'
end

Instance Method Details

#configuration(file) ⇒ Object

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’.



22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Object

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’.



37
38
39
40
41
42
43
44
45
46
47
48
# 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