module Reap class Project # Configuration is an open hash. # # TODO: Settings needs improvement -- it's rather sloppy at this point. class Settings < Hash DEFAULT_FILE = File.join(File.dirname(__FILE__), 'default.yaml') REAP_FILE = '{.reap,_reap,reapfile}{.yaml,.yml,}' def self.read(location, metadata) if File.file?(location) file = location else file = Dir.glob(File.join(location, REAP_FILE), File::FNM_CASEFOLD).first end if file data = YAML::load(File.open(file)) || {} else data = {} end new(data, metadata) end attr :metadata # FIXME: when using the settings, I think nil should be considered a none entry and # so false would be required to actually mean "off". This means assigning each key value par one a time? def initialize(data, metadata) super() @metadata = metadata defaults = File.read(DEFAULT_FILE) defaults = instance_eval("<<-XXXXXXXXXXXXX\n#{defaults}\nXXXXXXXXXXXXX") defaults = YAML::load(defaults) settings = defaults.dup data.each do |key, value| settings[key] ||= {} settings[key].update(value) if value end update(settings) end # open hash def method_missing(s, *a) if s =~ /=$/ self[s] = a[0] elsif a.empty? self[s] else super end end end end end