Class: YAMLSettings

Inherits:
Hash
  • Object
show all
Defined in:
lib/yaml_settings.rb

Defined Under Namespace

Modules: AttrReader

Constant Summary collapse

VERSION =
'1.0.6'

Instance Method Summary collapse

Constructor Details

#initialize(*filenames, key) ⇒ YAMLSettings

Settings are merged in two stages. First, all filenames are recursively merged in reverse order. Settings in succeeding filenames overwrite all duplicate settings in preceding filenames. Second, settings specified by key are then recursively merged with the default settings.

filenames

one or more YAML data files

key

selects the top-level stanza from filenames to be merged with the default stanza


25
26
27
28
29
30
31
32
33
# File 'lib/yaml_settings.rb', line 25

def initialize(*filenames, key)
  all_settings = filenames.collect {|f| YAML.load_file(f) }
  settings = all_settings.reduce {|i, j| deep_merge(i, j) }
  @settings = deep_merge(settings.fetch('default'),
                         settings.fetch(key.to_s))
  @settings.extend(AttrReader)
  propagate_attr_reader(@settings)
  super(@settings)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(setting, *args, &blk) ⇒ Object (private)


40
41
42
# File 'lib/yaml_settings.rb', line 40

def method_missing(setting, *args, &blk)
  super rescue @settings.send(setting)
end

Instance Method Details

#inspectObject Also known as: to_s


35
# File 'lib/yaml_settings.rb', line 35

def inspect() @settings.inspect end