Class: ConfigClass

Inherits:
Hash
  • Object
show all
Defined in:
lib/buzzcore/config.rb

Direct Known Subclasses

ConfigXmlClass

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aDefaultValues, aNewValues = nil, &aBlock) ⇒ ConfigClass

Returns a new instance of ConfigClass.



8
9
10
11
12
13
14
# File 'lib/buzzcore/config.rb', line 8

def initialize(aDefaultValues,aNewValues=nil,&aBlock)
	@default_values = aDefaultValues.clone
	reset()
	if aNewValues
		block_given? ? read(aNewValues,&aBlock) : read(aNewValues) 
	end
end

Instance Attribute Details

#default_valuesObject (readonly)

Returns the value of attribute default_values.



6
7
8
# File 'lib/buzzcore/config.rb', line 6

def default_values
  @default_values
end

Instance Method Details

#copy_booleans(aHash, *aKeys) ⇒ Object



105
106
107
108
109
# File 'lib/buzzcore/config.rb', line 105

def copy_booleans(aHash,*aKeys)
	aKeys.each do |k|
		set_boolean(k,aHash[k])
	end
end

#copy_floats(aHash, *aKeys) ⇒ Object



99
100
101
102
103
# File 'lib/buzzcore/config.rb', line 99

def copy_floats(aHash,*aKeys)
	aKeys.each do |k|
		set_float(k,aHash[k])
	end
end

#copy_ints(*aDb) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/buzzcore/config.rb', line 91

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/buzzcore/config.rb', line 68

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 ;
		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



85
86
87
88
89
# File 'lib/buzzcore/config.rb', line 85

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



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/buzzcore/config.rb', line 17

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

#resetObject

reset values back to defaults



30
31
32
33
34
# File 'lib/buzzcore/config.rb', line 30

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



52
53
54
55
56
57
58
59
# File 'lib/buzzcore/config.rb', line 52

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



44
45
46
47
48
49
50
# File 'lib/buzzcore/config.rb', line 44

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



36
37
38
39
40
41
42
# File 'lib/buzzcore/config.rb', line 36

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



61
62
63
64
65
66
# File 'lib/buzzcore/config.rb', line 61

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_hashObject



111
112
113
# File 'lib/buzzcore/config.rb', line 111

def to_hash
	{}.merge(self)
end