Class: Settings::Hash

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

Defined Under Namespace

Classes: SettingNotFound

Instance Method Summary collapse

Constructor Details

#initialize(path, namespace = nil) ⇒ Hash

Creates a new Settings::Hash from a YAML file located at the given path.

Optionally loads only the settings within the given namespace (if a namespace is given)

Settings::Hash.new('/path/to/settings.yml')       => { 'foo' => { :bar => 'baz' }, :bam => 'bang' }
Settings::Hash.new('/path/to/settings.yml, 'foo') => { :bar => 'baz' }


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/settings.rb', line 26

def initialize(path, namespace=nil)
  begin
    settings = YAML.load_file(path)
  rescue Errno::ENOENT => e
    e.message << " (attempting to load settings file)"
    raise e
  end

  if namespace
    raise "No settings defined for #{namespace} in settings file: #{path}" unless settings[namespace]
    settings = settings[namespace]
  end

  update settings
  freeze
end

Instance Method Details

#[](key) ⇒ Object

Access the value at the given key, raises Settings::Hash::SettingNotFound if the key is not set.

Raises:



45
46
47
48
# File 'lib/settings.rb', line 45

def [](key)
  raise SettingNotFound.new("No setting found for #{key.inspect}") unless has_key?(key)
  super
end