Class: Reality::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/reality/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

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

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/reality/config.rb', line 5

def data
  @data
end

#keysObject (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