Class: YAMLConfig::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/yaml-config-parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, args = {}) ⇒ Parser

Instantiate a new configuration parser.

path - The directory where the configuration files are stored :env - (optional) The list of environments used to choose the

right keys. If there is only one environment it can be passed as
a String. Default is 'development'


18
19
20
21
22
23
24
# File 'lib/yaml-config-parser.rb', line 18

def initialize(path, args = {})
  @path = path
  # TODO: make main and extra files configurable
  @main = 'config.yml'
  @extra = 'config.*.yml'
  self.environment = args[:env] || 'development'
end

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



9
10
11
# File 'lib/yaml-config-parser.rb', line 9

def environment
  @environment
end

#extraObject

Returns the value of attribute extra.



10
11
12
# File 'lib/yaml-config-parser.rb', line 10

def extra
  @extra
end

#mainObject

Returns the value of attribute main.



10
11
12
# File 'lib/yaml-config-parser.rb', line 10

def main
  @main
end

#pathObject

Returns the value of attribute path.



10
11
12
# File 'lib/yaml-config-parser.rb', line 10

def path
  @path
end

Instance Method Details

#find_config_filesObject

Gets the path for all the configuration files. The main file is put first

Returns an Array of Strings, each one being a path to a configuration file.

Raises:

  • (ArgumentError)


47
48
49
50
51
52
# File 'lib/yaml-config-parser.rb', line 47

def find_config_files
  main = File.join(@path, @main)
  raise ArgumentError.new("Main config file #{main} does not exist") unless File.exists? main
  extra = Dir.glob(File.join(@path, @extra))
  ([main] + extra).uniq
end

#loadObject

Loads all the config files that can be found under the given path, chooses the right environments, and merges them into a new OpenStruct object

Returns an OpenStruct with the configuration keys



31
32
33
# File 'lib/yaml-config-parser.rb', line 31

def load
  OpenStruct.new merge_config_files
end