lib/configliere/define.rb in configliere-0.0.1 vs lib/configliere/define.rb in configliere-0.0.2
- old
+ new
@@ -11,34 +11,19 @@
# Settings.define 'delorean.power_source', :description => 'Delorean subsytem supplying power to the Flux Capacitor.'
# Settings.define :password, :required => true, :obscure => true
#
def define param, definitions={}
self.param_definitions[param].merge! definitions
+ self[param] = definitions[:default] if definitions.include?(:default)
+ self.environment_variables definitions[:environment], param if definitions.include?(:environment)
end
def param_definitions
# initialize the param_definitions as an auto-vivifying hash if it's never been set
@param_definitions ||= Hash.new{|hsh, key| hsh[key] = {} }
end
- protected
- # all params with a value for the definable aspect
- #
- # @param definable the aspect to list (:description, :type, :encrypted, etc.)
- def params_with defineable
- param_definitions.keys.find_all{|param| param_definitions[param][defineable] } || []
- end
-
- def definitions_for defineable
- hsh = {}
- param_definitions.each do |param, defs|
- hsh[param] = defs[defineable] if defs[defineable]
- end
- hsh
- end
- public
-
# performs type coercion
def resolve!
resolve_types!
begin ; super() ; rescue NoMethodError ; nil ; end
self
@@ -63,11 +48,11 @@
param_definitions[param][:description]
end
# All described params with their descriptions
def descriptions
- definitions_for(:description)
+ definitions_for(:description).reject{|param, desc| param_definitions[param][:hide_help] }
end
# List of params that have descriptions
def described_params
params_with(:description)
@@ -84,37 +69,42 @@
def type_for param
param_definitions[param][:type]
end
- # All described params with their descriptions
- def types
+ # All typed params with their descriptions
+ def typed_params
definitions_for(:type)
end
# List of params that have descriptions
- def typed_params
+ def typed_param_names
params_with(:type)
end
+ require 'date'
+
# Coerce all params with types defined to their proper form
def resolve_types!
- types.each do |param, type|
+ typed_params.each do |param, type|
val = self[param]
case
- when val.nil? then val = nil
+ when val.nil? then val = nil
when (type == :boolean) then
- if ['false', '0', ''].include?(val.to_s) then val = false else val = true end
- when (val.to_s == '') then val = nil
+ if ['false', false, 0, '0', ''].include?(val) then val = false else val = true end
+ when ((type == Array) && val.is_a?(String))
+ val = val.split(",") rescue nil
+ # following types map blank to nil
+ when (val.blank?) then val = nil
when (type == Float) then val = val.to_f
when (type == Integer) then val = val.to_i
+ when (type == Symbol) then val = val.to_s.to_sym rescue nil
+ when ((val.to_s == 'now') && (type == Date)) then val = Date.today
+ when ((val.to_s == 'now') && (type == DateTime)) then val = DateTime.now
when (type == Date) then val = Date.parse(val) rescue nil
when (type == DateTime) then val = DateTime.parse(val) rescue nil
- when (type == Time) then
- require 'time'
- val = Time.parse(val) rescue nil
- when (type == Symbol) then val = val.to_s.to_sym rescue nil
+ else # nothing
end
self[param] = val
end
end
@@ -140,9 +130,31 @@
missing << param if self[param].nil?
end
raise "Missing values for #{missing.map{|s| s.to_s }.sort.join(", ")}" if (! missing.empty?)
end
+ # all params with a value for the definable aspect
+ #
+ # @param definable the aspect to list (:description, :type, :encrypted, etc.)
+ def params_with defineable
+ param_definitions.keys.find_all{|param| param_definitions[param][defineable] } || []
+ end
+
+ # all params without a value for the definable aspect
+ #
+ # @param definable the aspect to reject (:description, :type, :encrypted, etc.)
+ def params_without defineable
+ param_definitions.keys.reject{|param| param_definitions[param].include?(defineable) } || []
+ end
+
+ def definitions_for defineable
+ hsh = {}
+ param_definitions.each do |param, defs|
+ hsh[param] = defs[defineable] if defs[defineable]
+ end
+ hsh
+ end
+ public
end
Param.class_eval do
include Configliere::Define
end