Class: Reality::Config
- Inherits:
-
Object
- Object
- Reality::Config
- Defined in:
- lib/reality/config.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
Instance Method Summary collapse
- #fetch(*path) ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #load(str) ⇒ Object
- #register(*path, **opts) ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
7 8 9 10 |
# File 'lib/reality/config.rb', line 7 def initialize @keys = {} @data = {}.extend Hashie::Extensions::DeepFetch end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'lib/reality/config.rb', line 5 def data @data end |
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
5 6 7 |
# File 'lib/reality/config.rb', line 5 def keys @keys end |
Instance Method Details
#fetch(*path) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/reality/config.rb', line 20 def fetch(*path) data.deep_fetch(*path){ if (known = @keys[path]) fail KeyError, "Expected #{path.join('.')} to exist in config. It is #{known[:desc]}" else fail KeyError, "Expected #{path.join('.')} to exist in config." end } end |
#load(str) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/reality/config.rb', line 12 def load(str) if File.exists?(str) str = File.read(str) end @data.update(YAML.load(str)) end |
#register(*path, **opts) ⇒ Object
30 31 32 |
# File 'lib/reality/config.rb', line 30 def register(*path, **opts) @keys[path] = opts end |