class Buzztools::Config

Attributes

default_values[R]

Public Class Methods

new(aDefaultValues,aNewValues=nil,&aBlock) click to toggle source
# File lib/buzztools/config.rb, line 6
def initialize(aDefaultValues,aNewValues=nil,&aBlock)
        @default_values = aDefaultValues.dup
        reset()
        if aNewValues
                block_given? ? read(aNewValues,&aBlock) : read(aNewValues)
        end
end

Public Instance Methods

copy_booleans(aHash,*aKeys) click to toggle source
# File lib/buzztools/config.rb, line 103
def copy_booleans(aHash,*aKeys)
        aKeys.each do |k|
                set_boolean(k,aHash[k])
        end
end
copy_floats(aHash,*aKeys) click to toggle source
# File lib/buzztools/config.rb, line 97
def copy_floats(aHash,*aKeys)
        aKeys.each do |k|
                set_float(k,aHash[k])
        end
end
copy_ints(*aDb) click to toggle source
# File lib/buzztools/config.rb, line 89
def copy_ints(*aDb)
        aHash = aDb.shift
        aKeys = aDb
        aKeys.each do |k|
                set_int(k,aHash[k])
        end
end
copy_item(aHash,aKey) click to toggle source
# File lib/buzztools/config.rb, line 66
def copy_item(aHash,aKey)
        d = default_values[aKey]
        d_class = (d.is_a?(Class) ? d : d.class)
        cname = d_class.name.to_sym
        case cname
                when :NilClass then self[aKey] = aHash[aKey];
                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)
                when :Proc then self[aKey] = aHash[aKey] if aHash[aKey].is_a?(Proc)
                else
                        raise StandardError.new('unsupported type')
        end
end
copy_strings(aHash,*aKeys) click to toggle source
# File lib/buzztools/config.rb, line 83
def copy_strings(aHash,*aKeys)
        aKeys.each do |k|
                self[k] = aHash[k].to_s unless aHash[k].nil?
        end
end
read(aSource) { |k,v,aSource && aSource| ... } click to toggle source

aBlock allows values to be filtered based on key,default and new values

# File lib/buzztools/config.rb, line 15
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].nil?
        end
        self
end
reset() click to toggle source

reset values back to defaults

# File lib/buzztools/config.rb, line 28
def reset
        self.clear
        me = self
        @default_values.each {|n,v| me[n] = v.is_a?(Class) ? nil : v}
end
set_boolean(aKey,aValue) click to toggle source
# File lib/buzztools/config.rb, line 50
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
set_float(aKey,aValue) click to toggle source
# File lib/buzztools/config.rb, line 42
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
set_int(aKey,aValue) click to toggle source
# File lib/buzztools/config.rb, line 34
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
set_symbol(aKey,aValue) click to toggle source
# File lib/buzztools/config.rb, line 59
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
to_hash() click to toggle source
# File lib/buzztools/config.rb, line 109
def to_hash
        {}.merge(self)
end