class Olelo::Config

Attributes

base[R]
hash[R]

Public Class Methods

[](key) click to toggle source
# File lib/olelo/config.rb, line 56
def self.[](key)
  instance[key]
end
instance() click to toggle source
# File lib/olelo/config.rb, line 52
def self.instance
  @instance ||= Config.new
end
new(base = nil) click to toggle source
# File lib/olelo/config.rb, line 7
def initialize(base = nil)
  @hash = {}
  @base = base.freeze
end

Public Instance Methods

[](key) click to toggle source
# File lib/olelo/config.rb, line 12
def [](key)
  key = key.to_s
  if i = key.index('.')
    not_found(key) unless Config === hash[key[0...i]]
    hash[key[0...i]][key[i+1..-1]]
  else
    not_found(key) if !hash.include?(key)
    hash[key]
  end
end
[]=(key, value) click to toggle source
# File lib/olelo/config.rb, line 23
def []=(key, value)
  key = key.to_s
  if i = key.index('.')
    child(key[0...i])[key[i+1..-1]] = value
  elsif Hash === value
    child(key).update(value)
  else
    hash[key] = value.freeze
  end
end
each(&block) click to toggle source
# File lib/olelo/config.rb, line 48
def each(&block)
  hash.each(&block)
end
freeze() click to toggle source
Calls superclass method
# File lib/olelo/config.rb, line 68
def freeze
  hash.freeze
  super
end
load(file) click to toggle source
# File lib/olelo/config.rb, line 40
def load(file)
  load!(file) if File.file?(file)
end
load!(file) click to toggle source
# File lib/olelo/config.rb, line 44
def load!(file)
  update(Util.yaml_load_file(file))
end
to_hash() click to toggle source
# File lib/olelo/config.rb, line 60
def to_hash
  h = Hash.with_indifferent_access
  hash.each_pair do |k, v|
    h[k] = Config === v ? v.to_hash : v
  end
  h
end
update(hash) click to toggle source
# File lib/olelo/config.rb, line 34
def update(hash)
  hash.each_pair do |key, value|
    self[key] = value
  end
end

Private Instance Methods

child(key) click to toggle source
# File lib/olelo/config.rb, line 79
def child(key)
  if child = hash[key]
    raise "Configuration key #{key} is already used" unless Config === child
    child
  else
    hash[key] = Config.new(base ? "#{base}.#{key}" : key)
  end
end
not_found(key) click to toggle source
# File lib/olelo/config.rb, line 75
def not_found(key)
  raise(NameError, "Configuration key #{base ? "#{base}.#{key}" : key} not found")
end