Module: Configliere::Define
- Defined in:
- lib/configliere/define.rb
Instance Attribute Summary collapse
-
#param_definitions ⇒ Object
Definitions for params: :description, :type, :encrypted, etc.
Instance Method Summary collapse
- #define(param, definitions = {}, &block) ⇒ Object
- #definitions_for(defineable) ⇒ Object
-
#described_params ⇒ Object
List of params that have descriptions.
-
#description_for(param) ⇒ Object
gets the description (if any) for the param.
-
#descriptions ⇒ Object
All described params with their descriptions.
-
#method_missing(meth, *args) ⇒ Object
Pretend that any #define’d parameter is a method.
-
#params_with(defineable) ⇒ Object
all params with a value for the definable aspect.
-
#params_without(defineable) ⇒ Object
all params without a value for the definable aspect.
-
#required_params ⇒ Array
List of params that are required.
-
#resolve! ⇒ Object
performs type coercion.
-
#resolve_types! ⇒ Object
Coerce all params with types defined to their proper form.
-
#type_for(param) ⇒ Object
.
-
#typed_param_names ⇒ Object
List of params that have descriptions.
-
#typed_params ⇒ Object
All typed params with their descriptions.
- #validate! ⇒ Object
-
#validate_requireds! ⇒ Object
Check that all required params are present.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
Pretend that any #define’d parameter is a method
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/configliere/define.rb', line 169 def method_missing meth, *args meth.to_s =~ /^(\w+)(=)?$/ or return super name, setter = [$1.to_sym, $2] return(super) unless param_definitions.include?(name) if setter && (args.size == 1) self[name] = args.first elsif (!setter) && args.empty? self[name] else super ; end end |
Instance Attribute Details
#param_definitions ⇒ Object
Definitions for params: :description, :type, :encrypted, etc.
4 5 6 |
# File 'lib/configliere/define.rb', line 4 def param_definitions @param_definitions end |
Instance Method Details
#define(param, definitions = {}, &block) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/configliere/define.rb', line 14 def define param, definitions={}, &block self.param_definitions[param].merge! definitions self.use(:env_var) if definitions.include?(:env_var) self.use(:encrypted) if definitions.include?(:encrypted) self.use(:config_block) if definitions.include?(:finally) self[param] = definitions[:default] if definitions.include?(:default) self.env_vars param => definitions[:env_var] if definitions.include?(:env_var) self.finally(&definitions[:finally]) if definitions.include?(:finally) self.finally(&block) if block end |
#definitions_for(defineable) ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/configliere/define.rb', line 155 def definitions_for defineable hsh = {} param_definitions.each do |param, defs| hsh[param] = defs[defineable] if defs[defineable] end hsh end |
#described_params ⇒ Object
List of params that have descriptions
62 63 64 |
# File 'lib/configliere/define.rb', line 62 def described_params params_with(:description) end |
#description_for(param) ⇒ Object
gets the description (if any) for the param
52 53 54 |
# File 'lib/configliere/define.rb', line 52 def description_for param param_definitions[param][:description] end |
#descriptions ⇒ Object
All described params with their descriptions
57 58 59 |
# File 'lib/configliere/define.rb', line 57 def descriptions definitions_for(:description) # .reject{|param, desc| param_definitions[param][:hide_help] } end |
#params_with(defineable) ⇒ Object
all params with a value for the definable aspect
144 145 146 |
# File 'lib/configliere/define.rb', line 144 def params_with defineable param_definitions.keys.find_all{|param| param_definitions[param][defineable] } || [] end |
#params_without(defineable) ⇒ Object
all params without a value for the definable aspect
151 152 153 |
# File 'lib/configliere/define.rb', line 151 def params_without defineable param_definitions.keys.reject{|param| param_definitions[param].include?(defineable) } || [] end |
#required_params ⇒ Array
List of params that are required
128 129 130 |
# File 'lib/configliere/define.rb', line 128 def required_params params_with(:required) end |
#resolve! ⇒ Object
performs type coercion
31 32 33 34 35 |
# File 'lib/configliere/define.rb', line 31 def resolve! resolve_types! super() self end |
#resolve_types! ⇒ Object
Coerce all params with types defined to their proper form
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/configliere/define.rb', line 92 def resolve_types! typed_params.each do |param, type| val = self[param] case when val.nil? then val = nil when (type == :boolean) then 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 == :filename) then val = File.(val) 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 else # nothing end self[param] = val end end |
#type_for(param) ⇒ Object
75 76 77 |
# File 'lib/configliere/define.rb', line 75 def type_for param param_definitions[param][:type] end |
#typed_param_names ⇒ Object
List of params that have descriptions
85 86 87 |
# File 'lib/configliere/define.rb', line 85 def typed_param_names params_with(:type) end |
#typed_params ⇒ Object
All typed params with their descriptions
80 81 82 |
# File 'lib/configliere/define.rb', line 80 def typed_params definitions_for(:type) end |
#validate! ⇒ Object
37 38 39 40 41 |
# File 'lib/configliere/define.rb', line 37 def validate! validate_requireds! super() true end |
#validate_requireds! ⇒ Object
Check that all required params are present.
133 134 135 136 137 138 139 |
# File 'lib/configliere/define.rb', line 133 def validate_requireds! missing = [] required_params.each do |param| missing << param if self[param].nil? end raise "Missing values for #{missing.map{|s| s.to_s }.sort.join(", ")}" if (! missing.empty?) end |