class Architect::Config

Parse the configuration file and ARGV variables

Public Class Methods

new(conffile = nil) click to toggle source
# File lib/architect/config.rb, line 11
def initialize(conffile = nil)
  config = {
    :domain => Facter['domain'].value,
    :plugins => {},
  }
  if conffile.nil? or conffile.kind_of?(String)
    conffile ||= File.dirname(__FILE__) + "/../../conf/architect.yaml"
    conffile = File.realpath(conffile)
    if File.exist?(conffile)
      config.merge! parse(conffile)
    else
      raise "configuration file #{conffile} not found"
    end
  elsif conffile.kind_of?(Hash)
    config.merge! symbolize(conffile)
  else
    raise ArgumentError
  end
  config.keys.each { |k| publish k }
  @config = config
end
symbolize_hash(obj) click to toggle source

TODO: find a better way to do this

# File lib/architect/config.rb, line 34
def self.symbolize_hash(obj)
  symbolize(obj)
end

Public Instance Methods

to_hash() click to toggle source
# File lib/architect/config.rb, line 7
def to_hash
  @config
end

Private Instance Methods

parse(path) click to toggle source

Parse a configuration file and return a Hash

# File lib/architect/config.rb, line 50
def parse(path)
  # FIXME: want to disallow group-readable also
  raise "Insecure permissions on #{path}; please chmod to 0600" \
    if File.stat(path).world_readable? 
  symbolize(YAML.load_file(path))
end
publish(key) click to toggle source

Given a key, create a read-only accessor method

# File lib/architect/config.rb, line 58
def publish(key)
  self.class.class_eval do
    define_method(key.to_sym) { @config[key.to_sym] }
  end
end
symbolize(obj) click to toggle source

Given a nested hash, convert all keys from String to Symbol type Based on stackoverflow.com/questions/800122/best-way-to-convert-strings-to-symbols-in-hash

# File lib/architect/config.rb, line 43
def symbolize(obj)
  return obj.inject({}){|memo,(k,v)| memo[k.to_sym] =  symbolize(v); memo} if obj.is_a? Hash
  return obj.inject([]){|memo,v    | memo           << symbolize(v); memo} if obj.is_a? Array
  return obj
end