Sha256: 3506bd5c9efdcc93e7b50ef8f2a384744377f512da7b888e1af846a1436e4251

Contents?: true

Size: 1.25 KB

Versions: 16

Compression:

Stored size: 1.25 KB

Contents

require 'singleton'

class Configatron
  include Singleton
  
  alias_method :send!, :send
  
  def initialize # :nodoc:
    @_namespace = [:default]
    reset!
  end
  
  # Forwards the method call onto the 'namespaced' Configatron::Store
  def method_missing(sym, *args)
    @_store[@_namespace.last].send(sym, *args)
  end
  
  # Removes ALL configuration parameters
  def reset!
    @_store = {:default => Configatron::Store.new}
  end
  
  # 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.
  def temp(options = nil)
    begin
      temp_start(options)
      yield
    rescue Exception => e
      raise e
    ensure
      temp_end
    end
  end
  
  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
  
  def temp_end
    @_store.delete(@_namespace.pop)
  end
  
  begin
    undef :inspect # :nodoc:
    undef :nil? # :nodoc:
    undef :test # :nodoc:
  rescue Exception => e
  end

  
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
markbates-configatron-2.4.0.20090826115559 lib/configatron/configatron.rb
markbates-configatron-2.4.1.20090826153428 lib/configatron/configatron.rb
markbates-configatron-2.4.2.20090909140253 lib/configatron/configatron.rb
markbates-configatron-2.5.0.20090910111951 lib/configatron/configatron.rb
configatron-2.7.2 lib/configatron/configatron.rb
configatron-2.7.1 lib/configatron/configatron.rb
configatron-2.7.0 lib/configatron/configatron.rb
configatron-2.6.4 lib/configatron/configatron.rb
configatron-2.6.3 lib/configatron/configatron.rb
configatron-2.6.2 lib/configatron/configatron.rb
configatron-2.6.0 lib/configatron/configatron.rb
configatron-2.5.1 lib/configatron/configatron.rb
configatron-2.5.0 lib/configatron/configatron.rb
configatron-2.4.2 lib/configatron/configatron.rb
configatron-2.4.1 lib/configatron/configatron.rb
configatron-2.4.0 lib/configatron/configatron.rb