Class: TweakConfig

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

Overview

This is a cutdown version of ConfigClass from the buzzcore gem, and still is a bit messy. The idea is that the config can be declared with default values (enabling a reset to defaults), and that fields are implicitly declared with a type and value. New values for existing fields will then be converted to the original type. This is particularly useful eg if the values all come in as strings, but need to operate as other types

use like this :

config = TweakConfig.new( :something => String, # giving a class means the type is the given class, and the default value is nil :session_key => ‘_session’, :upload_path=> ‘cms/uploads’, :thumbs_cache => File.expand_path(‘public/thumbs_cache’,RAILS_ROOT), # make sure this exists with correct permissions :thumbs_url => ‘/thumbs_cache’ :delay => 3.5, :log_level => :warn )

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aDefaultValues = nil, aGivenValues = nil) ⇒ TweakConfig

Returns a new instance of TweakConfig.



23
24
25
26
27
# File 'lib/tweak_config.rb', line 23

def initialize(aDefaultValues=nil,aGivenValues=nil)
	@default_values = (aDefaultValues ? aDefaultValues.clone : {})
	reset()
	read(aGivenValues) if aGivenValues
end

Instance Attribute Details

#default_valuesObject (readonly)

Returns the value of attribute default_values.



21
22
23
# File 'lib/tweak_config.rb', line 21

def default_values
  @default_values
end

Class Method Details

.to_float(aValue, aDefault = nil) ⇒ Object



42
43
44
45
46
# File 'lib/tweak_config.rb', line 42

def self.to_float(aValue,aDefault=nil)
	t = aValue.strip
	return aDefault if !t =~ /(\+|-)?([0-9]+\.?[0-9]*|\.[0-9]+)([eE](\+|-)?[0-9]+)?/
	return t.to_f
end

.to_integer(aValue, aDefault = nil) ⇒ Object



36
37
38
39
40
# File 'lib/tweak_config.rb', line 36

def self.to_integer(aValue,aDefault=nil)
	t = aValue.strip
	return aDefault if t.empty? || !t.index(/^-{0,1}[0-9]+$/)
	return t.to_i
end

Instance Method Details

#copy_item(aHash, aKey) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tweak_config.rb', line 80

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 :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
			self[aKey] = aHash[aKey]
	end
end

#read(aSource, aLimitToDefaults = false) ⇒ Object



96
97
98
99
100
101
# File 'lib/tweak_config.rb', line 96

def read(aSource,aLimitToDefaults=false)
	aSource.each do |k,v|
		copy_item(aSource,k) unless aLimitToDefaults && !default_values.include?(k)
	end
	self
end

#resetObject

reset values back to defaults



30
31
32
33
34
# File 'lib/tweak_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



64
65
66
67
68
69
70
71
# File 'lib/tweak_config.rb', line 64

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



56
57
58
59
60
61
62
# File 'lib/tweak_config.rb', line 56

def set_float(aKey,aValue)
	case aValue
		when String then    self[aKey] = TweakConfig::to_float(aValue,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



48
49
50
51
52
53
54
# File 'lib/tweak_config.rb', line 48

def set_int(aKey,aValue)
	case aValue
		when String then    self[aKey] = TweakConfig::to_integer(aValue,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



73
74
75
76
77
78
# File 'lib/tweak_config.rb', line 73

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



103
104
105
# File 'lib/tweak_config.rb', line 103

def to_hash
	{}.merge(self)
end