Class: Econfig::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/econfig/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/econfig/configuration.rb', line 30

def method_missing(name, *args)
  if respond_to?(name)
    raise ArgumentError, "too many arguments (#{args.length} for 0)" if args.length > 0
    fetch(name)
  else
    super
  end
end

Instance Attribute Details

#default_write_backendObject

Returns the value of attribute default_write_backend.



3
4
5
# File 'lib/econfig/configuration.rb', line 3

def default_write_backend
  @default_write_backend
end

Instance Method Details

#[](key) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/econfig/configuration.rb', line 13

def [](key)
  backends.each do |backend|
    value = backend.get(key.to_s)
    return value if value
  end
  nil
end

#[]=(backend_name = default_write_backend, key, value) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
# File 'lib/econfig/configuration.rb', line 21

def []=(backend_name = default_write_backend, key, value)
  raise ArgumentError, "no backend given" unless backend_name
  if backend = backends[backend_name]
    backend.set(key, value)
  else
    raise KeyError, "#{backend_name} is not set"
  end
end

#backendsObject



5
6
7
# File 'lib/econfig/configuration.rb', line 5

def backends
  @backends ||= BackendCollection.new
end

#fetch(key) ⇒ Object



9
10
11
# File 'lib/econfig/configuration.rb', line 9

def fetch(key)
  self[key] or raise Econfig::NotFound, "configuration key '#{key}' is not set"
end

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/econfig/configuration.rb', line 39

def respond_to_missing?(name, *)
  name = name.to_s
  not(name.end_with?("=") or name.end_with?("!") or name.end_with?("?"))
end