Class: YAMLConfig::Parser
- Inherits:
-
Object
- Object
- YAMLConfig::Parser
- Defined in:
- lib/yaml-config-parser.rb
Instance Attribute Summary collapse
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#extra ⇒ Object
Returns the value of attribute extra.
-
#main ⇒ Object
Returns the value of attribute main.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
-
#find_config_files ⇒ Object
Gets the path for all the configuration files.
-
#initialize(path, args = {}) ⇒ Parser
constructor
Instantiate a new configuration parser.
-
#load ⇒ Object
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.
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
#environment ⇒ Object
Returns the value of attribute environment.
9 10 11 |
# File 'lib/yaml-config-parser.rb', line 9 def environment @environment end |
#extra ⇒ Object
Returns the value of attribute extra.
10 11 12 |
# File 'lib/yaml-config-parser.rb', line 10 def extra @extra end |
#main ⇒ Object
Returns the value of attribute main.
10 11 12 |
# File 'lib/yaml-config-parser.rb', line 10 def main @main end |
#path ⇒ Object
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_files ⇒ Object
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.
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 |
#load ⇒ Object
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 |