class Multicfg

A configuration loaded from multiple sources. TODO: proper rdoc

Public Class Methods

load(*args) click to toggle source
# File lib/multicfg.rb, line 59
def load(*args)
  args.reduce(new) { |a, e| a.load(e) }
end
merge(l, r) click to toggle source
# File lib/multicfg.rb, line 53
def merge(l, r)
  return r unless l.is_a?(Hash) && r.is_a?(Hash)
  r.each { |k, v| l[k] = merge(l[k], v) }
  l
end
new(prefix = nil) click to toggle source
# File lib/multicfg.rb, line 8
def initialize(prefix = nil)
  @prefix = prefix
end

Public Instance Methods

filter(hash = {}) click to toggle source
# File lib/multicfg.rb, line 40
def filter(hash = {})
  regexp = /^#{@prefix}[_\-]/i if @prefix
  regexp ||= /^/
  hash.each_with_object({}) do |kv, h|
    h[$'.downcase.to_sym] = kv[1] if kv[0] =~ regexp
  end
end
from_stream(io) click to toggle source
# File lib/multicfg.rb, line 48
def from_stream(io)
  io.read
end
load(x) click to toggle source
# File lib/multicfg.rb, line 16
def load(x)
  case x
  when Pathname then load(x.read)
  when Array    then load(Hash[*x])
  when IO       then load(read(io))
  when String   then load_or_parse(x)
  else               merge(filter(x.to_h))
  end
end
load_or_parse(s) click to toggle source
# File lib/multicfg.rb, line 34
def load_or_parse(s)
  pathname = Pathname.new(s)
  return load(pathname) if pathname.readable?
  parse(s)
end
merge(another) click to toggle source
# File lib/multicfg.rb, line 12
def merge(another)
  self.class.merge(self, another)
end
parse(s) click to toggle source
# File lib/multicfg.rb, line 26
def parse(s)
  merge(YAML.load(s))
end
read(io) click to toggle source
# File lib/multicfg.rb, line 30
def read(io)
  YAML.load(io)
end