Sha256: 0ddb6f48cb19d5eca05a17e92b108458352025127e79c64ea4aa00ed32caf8cc

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

require 'fileutils'
require 'singleton'
require 'logger'
require 'yamler'

base = File.dirname(__FILE__)

require File.join(base, 'store')
require File.join(base, 'errors')
require File.join(base, 'rails')
require File.join(base, 'proc')

class Configatron
  include Singleton

  alias_method :send!, :send

  class << self

    attr_accessor :strict, :disable_monkey_patching

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

    def reset!
      @strict = false
    end
  end

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

  # Forwards the method call onto the 'namespaced' Configatron::Store
  def method_missing(sym, *args, &block)
    @_store[@_namespace.last].send(sym, *args, &block)
  end

  # respond_to to respond_to
  def respond_to?(method)
    !@_store[@_namespace.last].send(method).nil? || super
  end

  # Removes ALL configuration parameters
  def reset!
    self.class.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 @_store[@_namespace.last]
    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

1 entries across 1 versions & 1 rubygems

Version Path
configatron-2.13.0 lib/configatron/core.rb