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



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/econfig/configuration.rb', line 34

def method_missing(name, *args)
  if respond_to?(name)
    raise ArgumentError, "too many arguments (#{args.length} for 0)" if args.length > 0
    if ::ENV["ECONFIG_PERMISSIVE"].to_s.empty?
      fetch(name)
    else
      self[name]
    end
  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



19
20
21
22
23
# File 'lib/econfig/configuration.rb', line 19

def [](key)
  key = key.to_s
  backend = backends.backend_for(key)
  backend.get(key) if backend
end

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

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
# File 'lib/econfig/configuration.rb', line 25

def []=(backend_name = default_write_backend, key, value)
  raise ArgumentError, "no backend given" unless backend_name
  if backend = backends[backend_name]
    backend.set(key.to_s, 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
12
13
14
15
16
17
# File 'lib/econfig/configuration.rb', line 9

def fetch(key)
  key = key.to_s
  backend = backends.backend_for(key)
  if backend
    backend.get(key)
  else
    raise Econfig::NotFound, "configuration key '#{key}' is not set"
  end
end

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/econfig/configuration.rb', line 47

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