class ConfigClass < Hash attr_reader :default_values def initialize(aDefaultValues,aNewValues=nil,&aBlock) self.merge!(@default_values = aDefaultValues.clone) if aNewValues block_given? ? read(aNewValues,&aBlock) : read(aNewValues) end end # aBlock allows values to be filtered based on key,default and new values def read(aSource,&aBlock) default_values.each do |k,v| done = false if block_given? && ((newv = yield(k,v,aSource && aSource[k])) != nil) self[k] = newv done = true end copy_item(aSource,k) if !done && aSource && aSource[k] end self end # reset values back to defaults def reset self.clear self.merge!(default_values) end def set_int(aKey,aValue) case aValue when String then self[aKey] = aValue.to_integer(self[aKey]); when Fixnum then self[aKey] = aValue; when Float then self[aKey] = aValue.to_i; end end def set_float(aKey,aValue) case aValue when String then self[aKey] = aValue.to_float(self[aKey]); when Fixnum then self[aKey] = aValue.to_f; when Float then self[aKey] = aValue; end end def set_boolean(aKey,aValue) case aValue when TrueClass,FalseClass then self[aKey] = aValue; when String then self[aKey] = (['1','yes','y','true','on'].include?(aValue.downcase)) else set_boolean(aKey,aValue.to_s) end end def set_symbol(aKey,aValue) case aValue when String then self[aKey] = (aValue.to_sym rescue nil); when Symbol then self[aKey] = aValue; end end def copy_item(aHash,aKey) case default_values[aKey] when NilClass then ; when String then self[aKey] = aHash[aKey].to_s unless aHash[aKey].nil? when Float then set_float(aKey,aHash[aKey]); when Fixnum then set_int(aKey,aHash[aKey]); when TrueClass, FalseClass then set_boolean(aKey,aHash[aKey]); when Symbol then self[aKey] = (aHash[aKey].to_sym rescue nil) else raise Error.new('unsupported type') end end def copy_strings(aHash,*aKeys) aKeys.each do |k| self[k] = aHash[k].to_s unless aHash[k].nil? end end def copy_ints(*aDb) aHash = aDb.shift aKeys = aDb aKeys.each do |k| set_int(k,aHash[k]) end end def copy_floats(aHash,*aKeys) aKeys.each do |k| set_float(k,aHash[k]) end end def copy_booleans(aHash,*aKeys) aKeys.each do |k| set_boolean(k,aHash[k]) end end def to_hash {}.merge(self) end end