module ConfigContext

Private Class Methods

deprecate(old_method, new_method) click to toggle source
# File lib/config_context.rb, line 11
def self.deprecate(old_method, new_method) 

  define_method(old_method) do |*arguments, &block|
    
    warn("#{old_method}() is deprecated. Use #{new_method}() instead.")
    send(new_method, *arguments, &block)
  end 
end

Public Instance Methods

[](key) click to toggle source

Backward compability…

# File lib/config_context.rb, line 101
def [](key)
  
  @config || init 
  @config[key]
end
configure(source=nil, options={}) { |self| ... } click to toggle source
# File lib/config_context.rb, line 53
def configure(source=nil, options={}, &block)

  @config || init
  
  options = {:source=>nil, :context=>:root}.merge(options)
  
  if options[:context] == :root

    case source
      when /\.(yml|yaml)/i then @config.merge!(YAML.load_file(source)) rescue raise ConfigError.new("Problems loading file: #{source}")
      when /\.json/i       then @config.merge!(JSON.parse(File.read(source))) rescue raise ConfigError.new("Problems loading file: #{source}")
      when Hash            then @config.merge!(source)
    else 
      yield self if block_given?
    end
  else

    context          = options[:context]
    @config[context] ||= { } # New context
    
    case source
      when /\.(yml|yaml)/i then @config[context].merge!(YAML.load_file(source)) rescue raise ConfigError.new("Problems loading file: #{source}")
      when /\.json/i       then @config[context].merge!(JSON.parse(File.read(source))) rescue raise ConfigError.new("Problems loading file: #{source}")
      when Hash            then @config[context].merge!(source)
    else 
      yield self if block_given?
    end
  end
  
  self
end
erase!() click to toggle source
# File lib/config_context.rb, line 49
def erase!()
  @config.clear
end
fetch(key,default=nil) click to toggle source
# File lib/config_context.rb, line 107
def fetch(key,default=nil)
  
  @config || init
  if @config.include?(key)
    
    @config[key]
  else
    default ? default : nil
  end 
end
fetch!(key,default) click to toggle source
# File lib/config_context.rb, line 118
def fetch!(key,default)
  
  @config || init
  
  @config[key] = default unless @config.include?(key)
  @config[key]
end
inspect() click to toggle source
# File lib/config_context.rb, line 89
def inspect
  
  @config || init
  @config.inspect
end
method_missing(method, *arguments, &block) click to toggle source
Calls superclass method
# File lib/config_context.rb, line 25
def method_missing(method, *arguments, &block)
  
  @config || init
  
  case method.to_s
    when /(.+)=$/  then
      property_key = method.to_s.delete('=').to_sym
      
      @config[property_key] = (arguments.size == 1) ? arguments[0] : arguments
    
    when /(.+)\?$/ then 
      property_key = method.to_s.delete('?').to_sym

      @config.keys.include?(property_key) #true/false
  else

    if @config.keys.include?(method) # any type
      @config[method]
   else
      super
    end
  end    
end
to_hash() click to toggle source
# File lib/config_context.rb, line 85
def to_hash 
  @config || init
end
to_s() click to toggle source
# File lib/config_context.rb, line 95
def to_s
  "#{@config.inspect}"
end

Private Instance Methods

init() click to toggle source
# File lib/config_context.rb, line 20
def init
  @config ||= Hash.new
end