option normalization

git-svn-id: file:///home/svn/incoming/trunk@3035 4d416f70-5f16-0410-b530-b9f4589650da
This commit is contained in:
Matt Miller 2005-11-15 21:50:10 +00:00
parent fc42fef941
commit 510669e7ef
3 changed files with 29 additions and 3 deletions

View File

@ -324,7 +324,7 @@ class ReadableText
])
ds.keys.sort.each { |k|
tbl << [ k, ds[k] ]
tbl << [ k, (ds[k] != nil) ? ds[k].to_s : '' ]
}
return ds.length > 0 ? tbl.to_s : "#{tbl.header_to_s}No entries in data store.\n"

View File

@ -130,7 +130,7 @@ class ModuleDataStore < DataStore
def fetch(key)
val = super
if (!val and @_module and @_module.framework)
if (val == nil and @_module and @_module.framework)
val = @_module.framework.datastore[key]
end
@ -143,7 +143,7 @@ class ModuleDataStore < DataStore
def [](key)
val = super
if (!val and @_module and @_module.framework)
if (val == nil and @_module and @_module.framework)
val = @_module.framework.datastore[key]
end

View File

@ -54,6 +54,14 @@ class OptBase
return (required? and (value == nil or value.to_s.empty?)) ? false : true
end
#
# Normalizes the supplied value to conform with the type that the option is
# conveying.
#
def normalize(value)
value
end
#
# Returns the value of the option as a string.
#
@ -137,6 +145,9 @@ end
#
###
class OptBool < OptBase
TrueRegex = /^(y|yes|t|1|true)$/i
def type
return 'bool'
end
@ -150,6 +161,14 @@ class OptBool < OptBase
true
end
def normalize(value)
if (value.match(TrueRegex) != nil)
true
else
false
end
end
def is_true?
return (value.match(/^(y|yes|t|1|true)$/i) != nil) ? true : false
end
@ -236,6 +255,10 @@ class OptInt < OptBase
return 'integer'
end
def normalize(value)
value.to_i
end
def valid?(value)
if (value.to_s.match(/^\d+$/) == nil)
return false
@ -391,6 +414,9 @@ class OptionContainer < Hash
each_pair { |name, option|
if (!option.valid?(datastore[name]))
errors << name
# If the option is valid, normalize its format to the correct type.
elsif ((val = option.normalize(datastore[name])) != nil)
datastore[name] = val
end
}