Class: Configatron

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/configatron/proc.rb,
lib/configatron/rails.rb,
lib/configatron/store.rb,
lib/configatron/errors.rb,
lib/configatron/configatron.rb

Defined Under Namespace

Modules: Rails Classes: Delayed, Dynamic, LockedNamespace, Proc, ProtectedParameter, Store

Instance Method Summary collapse

Constructor Details

#initializeConfigatron

:nodoc:



8
9
10
11
# File 'lib/configatron/configatron.rb', line 8

def initialize # :nodoc:
  @_namespace = [:default]
  reset!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

Forwards the method call onto the ‘namespaced’ Configatron::Store



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

def method_missing(sym, *args)
  @_store[@_namespace.last].send(sym, *args)
end

Instance Method Details

#reset!Object

Removes ALL configuration parameters



19
20
21
# File 'lib/configatron/configatron.rb', line 19

def reset!
  @_store = {:default => Configatron::Store.new}
end

#temp(options = nil) ⇒ Object

Allows for the temporary overriding of parameters in a block. Takes an optional Hash of parameters that will be applied before the block gets called. At the end of the block, the temporary settings are deleted and the original settings are reinstated.



27
28
29
30
31
32
33
34
35
36
# File 'lib/configatron/configatron.rb', line 27

def temp(options = nil)
  begin
    temp_start(options)
    yield
  rescue Exception => e
    raise e
  ensure
    temp_end
  end
end

#temp_endObject



47
48
49
# File 'lib/configatron/configatron.rb', line 47

def temp_end
  @_store.delete(@_namespace.pop)
end

#temp_start(options = nil) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/configatron/configatron.rb', line 38

def temp_start(options = nil)
  n_space = rand
  @_store[n_space] = @_store[@_namespace.last].deep_clone
  @_namespace << n_space
  if options
    self.method_missing(:configure_from_hash, options)
  end
end