module Chef::Mixin::ParamsValidate

Public Instance Methods

lazy(&block) click to toggle source
# File lib/chef/mixin/params_validate.rb, line 80
def lazy(&block)
  DelayedEvaluator.new(&block)
end
set_or_return(symbol, arg, validation) click to toggle source
# 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) click to toggle source

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} #{message}!"
: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.

# 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

Private Instance Methods

_pv_callbacks(opts, key, callbacks) click to toggle source

Check a parameter against a hash of proc's.

# File lib/chef/mixin/params_validate.rb, line 219
def _pv_callbacks(opts, key, callbacks)
  raise ArgumentError, "Callback list must be a hash!" unless callbacks.kind_of?(Hash)
  value = _pv_opts_lookup(opts, key)
  if value != nil
    callbacks.each do |message, zeproc|
      if zeproc.call(value) != true
        raise Exceptions::ValidationFailed, "Option #{key}'s value #{value} #{message}!"
      end
    end
  end
end
_pv_cannot_be(opts, key, predicate_method_base_name) click to toggle source

Assert that parameter returns false when passed a predicate method. For example, :cannot_be => :blank will raise a Exceptions::ValidationFailed error value.blank? returns a 'truthy' (not nil or false) value.

Note, this will PASS if the object doesn't respond to the method. So, to make sure a value is not nil and not blank, you need to do both :cannot_be => :blank and :cannot_be => :nil (or :required => true)

# File lib/chef/mixin/params_validate.rb, line 181
def _pv_cannot_be(opts, key, predicate_method_base_name)
  value = _pv_opts_lookup(opts, key)
  predicate_method = (predicate_method_base_name.to_s + "?").to_sym

  if value.respond_to?(predicate_method)
    if value.send(predicate_method)
      raise Exceptions::ValidationFailed, "Option #{key} cannot be #{predicate_method_base_name}"
    end
  end
end
_pv_default(opts, key, default_value) click to toggle source

Assign a default value to a parameter.

# File lib/chef/mixin/params_validate.rb, line 193
def _pv_default(opts, key, default_value)
  value = _pv_opts_lookup(opts, key)
  if value == nil
    opts[key] = default_value
  end
end
_pv_equal_to(opts, key, to_be) click to toggle source
# File lib/chef/mixin/params_validate.rb, line 135
def _pv_equal_to(opts, key, to_be)
  value = _pv_opts_lookup(opts, key)
  unless value.nil?
    passes = false
    Array(to_be).each do |tb|
      passes = true if value == tb
    end
    unless passes
      raise Exceptions::ValidationFailed, "Option #{key} must be equal to one of: #{to_be.join(", ")}!  You passed #{value.inspect}."
    end
  end
end
_pv_kind_of(opts, key, to_be) click to toggle source

Raise an exception if the parameter is not a kind_of?(to_be)

# File lib/chef/mixin/params_validate.rb, line 149
def _pv_kind_of(opts, key, to_be)
  value = _pv_opts_lookup(opts, key)
  unless value.nil?
    passes = false
    Array(to_be).each do |tb|
      passes = true if value.kind_of?(tb)
    end
    unless passes
      raise Exceptions::ValidationFailed, "Option #{key} must be a kind of #{to_be}!  You passed #{value.inspect}."
    end
  end
end
_pv_name_attribute(opts, key, is_name_attribute=true) click to toggle source

Allow a parameter to default to @name

# File lib/chef/mixin/params_validate.rb, line 232
def _pv_name_attribute(opts, key, is_name_attribute=true)
  if is_name_attribute
    if opts[key] == nil
      opts[key] = self.instance_variable_get("@name")
    end
  end
end
_pv_opts_lookup(opts, key) click to toggle source

Return the value of a parameter, or nil if it doesn't exist.

# File lib/chef/mixin/params_validate.rb, line 113
def _pv_opts_lookup(opts, key)
  if opts.has_key?(key.to_s)
    opts[key.to_s]
  elsif opts.has_key?(key.to_sym)
    opts[key.to_sym]
  else
    nil
  end
end
_pv_regex(opts, key, regex) click to toggle source

Check a parameter against a regular expression.

# File lib/chef/mixin/params_validate.rb, line 201
def _pv_regex(opts, key, regex)
  value = _pv_opts_lookup(opts, key)
  if value != nil
    passes = false
    [ regex ].flatten.each do |r|
      if value != nil
        if r.match(value.to_s)
          passes = true
        end
      end
    end
    unless passes
      raise Exceptions::ValidationFailed, "Option #{key}'s value #{value} does not match regular expression #{regex.inspect}"
    end
  end
end
_pv_required(opts, key, is_required=true) click to toggle source

Raise an exception if the parameter is not found.

# File lib/chef/mixin/params_validate.rb, line 124
def _pv_required(opts, key, is_required=true)
  if is_required
    if (opts.has_key?(key.to_s) && !opts[key.to_s].nil?) ||
        (opts.has_key?(key.to_sym) && !opts[key.to_sym].nil?)
      true
    else
      raise Exceptions::ValidationFailed, "Required argument #{key} is missing!"
    end
  end
end
_pv_respond_to(opts, key, method_name_list) click to toggle source

Raise an exception if the parameter does not respond to a given set of methods.

# File lib/chef/mixin/params_validate.rb, line 163
def _pv_respond_to(opts, key, method_name_list)
  value = _pv_opts_lookup(opts, key)
  unless value.nil?
    Array(method_name_list).each do |method_name|
      unless value.respond_to?(method_name)
        raise Exceptions::ValidationFailed, "Option #{key} must have a #{method_name} method!"
      end
    end
  end
end