Module: Chef::Mixin::ParamsValidate
- Included in:
- ApiClient, Cookbook::Metadata, DataBag, DataBagItem, Environment, FileCache, Node, Platform, Resource, ResourceDefinition, Role, RunList, Runner, User
- Defined in:
- lib/chef/mixin/params_validate.rb
Instance Method Summary collapse
- #lazy(&block) ⇒ Object
- #set_or_return(symbol, arg, validation) ⇒ Object
-
#validate(opts, map) ⇒ Object
Takes a hash of options, along with a map to validate them.
Instance Method Details
#lazy(&block) ⇒ Object
80 81 82 |
# File 'lib/chef/mixin/params_validate.rb', line 80 def lazy(&block) DelayedEvaluator.new(&block) end |
#set_or_return(symbol, arg, validation) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/chef/mixin/params_validate.rb', line 84 def set_or_return(symbol, arg, validation) iv_symbol = "@#{symbol.to_s}".to_sym if arg == nil && self.instance_variable_defined?(iv_symbol) == true ivar = self.instance_variable_get(iv_symbol) if(ivar.is_a?(DelayedEvaluator)) validate({ symbol => ivar.call }, { symbol => validation })[symbol] else ivar end else if(arg.is_a?(DelayedEvaluator)) val = arg else val = validate({ symbol => arg }, { symbol => validation })[symbol] # Handle the case where the "default" was a DelayedEvaluator. In # this case, the block yields an optional parameter of +self+, # which is the equivalent of "new_resource" if val.is_a?(DelayedEvaluator) val = val.call(self) end end self.instance_variable_set(iv_symbol, val) end end |
#validate(opts, map) ⇒ Object
Takes a hash of options, along with a map to validate them. Returns the original options hash, plus any changes that might have been made (through things like setting default values in the validation map)
For example:
validate({ :one => "neat" }, { :one => { :kind_of => String }})
Would raise an exception if the value of :one above is not a kind_of? string. Valid map options are:
- :default
-
Sets the default value for this parameter.
- :callbacks
-
Takes a hash of Procs, which should return true if the argument is valid. The key will be inserted into the error message if the Proc does not return true:
"Option #{key}'s value #{value} #{}!"
- :kind_of
-
Ensure that the value is a kind_of?(Whatever). If passed an array, it will ensure that the value is one of those types.
- :respond_to
-
Ensure that the value has a given method. Takes one method name or an array of method names.
- :required
-
Raise an exception if this parameter is missing. Valid values are true or false, by default, options are not required.
- :regex
-
Match the value of the paramater against a regular expression.
- :equal_to
-
Match the value of the paramater with ==. An array means it can be equal to any of the values.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/chef/mixin/params_validate.rb', line 48 def validate(opts, map) #-- # validate works by taking the keys in the validation map, assuming it's a hash, and # looking for _pv_:symbol as methods. Assuming it find them, it calls the right # one. #++ raise ArgumentError, "Options must be a hash" unless opts.kind_of?(Hash) raise ArgumentError, "Validation Map must be a hash" unless map.kind_of?(Hash) map.each do |key, validation| unless key.kind_of?(Symbol) || key.kind_of?(String) raise ArgumentError, "Validation map keys must be symbols or strings!" end case validation when true _pv_required(opts, key) when false true when Hash validation.each do |check, carg| check_method = "_pv_#{check.to_s}" if self.respond_to?(check_method, true) self.send(check_method, opts, key, carg) else raise ArgumentError, "Validation map has unknown check: #{check}" end end end end opts end |