Class: Buzztools::Config
- Inherits:
-
Hash
- Object
- Hash
- Buzztools::Config
- Defined in:
- lib/buzztools/config.rb
Instance Attribute Summary collapse
-
#default_values ⇒ Object
readonly
Returns the value of attribute default_values.
Instance Method Summary collapse
- #copy_booleans(aHash, *aKeys) ⇒ Object
- #copy_floats(aHash, *aKeys) ⇒ Object
- #copy_ints(*aDb) ⇒ Object
- #copy_item(aHash, aKey) ⇒ Object
- #copy_strings(aHash, *aKeys) ⇒ Object
-
#initialize(aDefaultValues, aNewValues = nil, &aBlock) ⇒ Config
constructor
A new instance of Config.
-
#read(aSource, &aBlock) ⇒ Object
aBlock allows values to be filtered based on key,default and new values.
-
#reset ⇒ Object
reset values back to defaults.
- #set_boolean(aKey, aValue) ⇒ Object
- #set_float(aKey, aValue) ⇒ Object
- #set_int(aKey, aValue) ⇒ Object
- #set_symbol(aKey, aValue) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(aDefaultValues, aNewValues = nil, &aBlock) ⇒ Config
Returns a new instance of Config.
6 7 8 9 10 11 12 |
# 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 |
Instance Attribute Details
#default_values ⇒ Object (readonly)
Returns the value of attribute default_values.
4 5 6 |
# File 'lib/buzztools/config.rb', line 4 def default_values @default_values end |
Instance Method Details
#copy_booleans(aHash, *aKeys) ⇒ Object
103 104 105 106 107 |
# 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) ⇒ Object
97 98 99 100 101 |
# 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) ⇒ Object
89 90 91 92 93 94 95 |
# 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) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# 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) ⇒ Object
83 84 85 86 87 |
# 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, &aBlock) ⇒ Object
aBlock allows values to be filtered based on key,default and new values
15 16 17 18 19 20 21 22 23 24 25 |
# 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 ⇒ Object
reset values back to defaults
28 29 30 31 32 |
# 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) ⇒ Object
50 51 52 53 54 55 56 57 |
# 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) ⇒ Object
42 43 44 45 46 47 48 |
# 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) ⇒ Object
34 35 36 37 38 39 40 |
# 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) ⇒ Object
59 60 61 62 63 64 |
# 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 ⇒ Object
109 110 111 |
# File 'lib/buzztools/config.rb', line 109 def to_hash {}.merge(self) end |