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/version.rb,
lib/configatron/configatron.rb,
lib/generators/configatron/install/install_generator.rb

Defined Under Namespace

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

Constant Summary collapse

VERSION =
"2.9.1"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigatron

:nodoc:



23
24
25
26
# File 'lib/configatron/configatron.rb', line 23

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

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



29
30
31
# File 'lib/configatron/configatron.rb', line 29

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

Class Method Details

.logObject



11
12
13
14
15
16
17
18
19
# File 'lib/configatron/configatron.rb', line 11

def log
  unless @logger
    if defined?(::Rails)
      @logger = ::Rails.logger
    end
    @logger = ::Logger.new(STDOUT) if @logger.nil?
  end
  return @logger
end

Instance Method Details

#reset!Object

Removes ALL configuration parameters



39
40
41
# File 'lib/configatron/configatron.rb', line 39

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

#respond_to?(method) ⇒ Boolean

respond_to to respond_to

Returns:

  • (Boolean)


34
35
36
# File 'lib/configatron/configatron.rb', line 34

def respond_to?(method)
  !@_store[@_namespace.last].send(method).nil? || super
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.



47
48
49
50
51
52
53
54
55
56
# File 'lib/configatron/configatron.rb', line 47

def temp(options = nil)
  begin
    temp_start(options)
    yield @_store[@_namespace.last]
  rescue Exception => e
    raise e
  ensure
    temp_end
  end
end

#temp_endObject



67
68
69
# File 'lib/configatron/configatron.rb', line 67

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

#temp_start(options = nil) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/configatron/configatron.rb', line 58

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