Class: SettingsHash
- Inherits:
-
ReadonlyHash
- Object
- Hash
- ReadonlyHash
- SettingsHash
- Defined in:
- lib/settings_hash.rb
Defined Under Namespace
Classes: SettingNotFound
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access the value at the given key, raises SettingsHash::SettingNotFound if the key is not set.
-
#initialize(path, namespace = nil) ⇒ SettingsHash
constructor
Creates a new SettingsHash from a YAML file located at the given path.
Methods inherited from ReadonlyHash
Methods inherited from Hash
Constructor Details
#initialize(path, namespace = nil) ⇒ SettingsHash
Creates a new SettingsHash from a YAML file located at the given path.
Optionally loads only the settings within the given namespace (if a namespace is given)
SettingsHash.new('/path/to/settings.yml') => { :foo => { :bar => 'baz' }, :bam => 'bang' }
SettingsHash.new('/path/to/settings.yml, 'foo') => { :bar => 'baz' }
Note that hash keys are symbolized (as seen in example above)
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/settings_hash.rb', line 42 def initialize(path, namespace=nil) raise "No settings file found: #{path}" unless File.exists?(path) settings = YAML.load_file(path) if namespace raise "No settings defined for #{namespace} in settings file: #{path}" unless settings[namespace] settings = settings[namespace] end super(settings.symbolize_keys) end |
Instance Method Details
#[](key) ⇒ Object
Access the value at the given key, raises SettingsHash::SettingNotFound if the key is not set.
56 57 58 59 |
# File 'lib/settings_hash.rb', line 56 def [](key) raise SettingNotFound.new("No setting found for #{key}") unless has_key?(key) super end |