Class: Confuse::Config

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

Instance Method Summary collapse

Constructor Details

#initialize(definition, source) ⇒ Config

Returns a new instance of Config.



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

def initialize(definition, source)
  @definition = definition
  @source = source
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *_args) ⇒ Object



14
15
16
# File 'lib/confuse/config.rb', line 14

def method_missing(name, *_args)
  self[name] if respond_to?(name)
end

Instance Method Details

#[](name) ⇒ Object



18
19
20
21
# File 'lib/confuse/config.rb', line 18

def [](name)
  namespace, key = @definition.namespace_and_key(name)
  lookup(namespace, key)
end

#checkObject

check items have a value. Will raise Undefined error if a required item has no value.



35
36
37
38
39
40
41
# File 'lib/confuse/config.rb', line 35

def check
  @definition.namespaces.each do |(namespace, ns)|
    ns.items.each do |key, _|
      lookup(namespace, key)
    end
  end
end

#lookup(namespace, key) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/confuse/config.rb', line 23

def lookup(namespace, key)
  unless (item = @definition.find_item(namespace, key))
    raise Errors::Undefined.new(key)
  end

  value = @source[namespace, key] || item.default(self)

  item.convert(value)
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/confuse/config.rb', line 10

def respond_to?(name)
  @definition.defines?(name) || super
end

#to_hashObject



43
44
45
46
47
48
# File 'lib/confuse/config.rb', line 43

def to_hash
  @definition.to_hash.reduce({}) do |a, (k, v)|
    value_added = v.merge(:value => self[k])
    a.merge({ k => value_added })
  end
end