class Aws::Templates::Utils::Parametrized::Constraint

Constraint functor class

A constraint is a Proc which accepts one parameter which is a value which needs to be checked and ir is expected to throw an exception if the value is not in compliance with the constraint

The class implements functor pattern through to_proc method and closure. Essentially, all constraints can be used everywhere where a block is expected.

It provides protected method check which should be overriden in all concrete constraint classes.

Public Instance Methods

check_wrapper(parameter, value, instance) click to toggle source

Wraps constraint-dependent method

It wraps constraint-dependent “check” method into a rescue block to standardize exception type and information provided by failed constraint validation

  • parameter - the Parameter object which the constraint is evaluated

    against
    
  • value - parameter value to be checked

  • instance - the instance value is checked for

# File lib/aws/templates/utils/parametrized/constraint.rb, line 52
def check_wrapper(parameter, value, instance)
  check(parameter, value, instance) if pre_condition.check(value, instance)
rescue StandardError
  raise Templates::Exception::ParameterValueInvalid.new(parameter, instance, value)
end
if(*params, &blk) click to toggle source

Change precondition of the constraint

Pre-condition is a modifier to the main constraint. The constraint won't be evaluated if pre-condition is not met. Default condition is that value should be not nil meaning that if the value is nil then the constraint will be ignored.

# File lib/aws/templates/utils/parametrized/constraint.rb, line 64
def if(*params, &blk)
  @pre_condition = Condition.for(
    if params.empty?
      raise 'Block must be specified' unless block_given?
      blk
    else
      params.first
    end
  )

  self
end
pre_condition() click to toggle source
# File lib/aws/templates/utils/parametrized/constraint.rb, line 77
def pre_condition
  @pre_condition ||= Condition.not_nil
end
to_proc() click to toggle source

Creates closure with checker invocation

It's an interface method required for Constraint to expose functor properties. It encloses invocation of Constraint check_wrapper method into a closure. The closure itself is executed in the context of Parametrized instance which provides proper set “self” variable.

The closure itself accepts 2 parameters:

  • parameter - the Parameter object which the constraint is evaluated

    against
    
  • value - parameter value to be checked

…where instance is assumed from self

# File lib/aws/templates/utils/parametrized/constraint.rb, line 34
def to_proc
  constraint = self

  lambda do |parameter, value|
    constraint.check_wrapper(parameter, value, self)
  end
end

Protected Instance Methods

check(parameter, value, instance) click to toggle source

Constraint-dependent check

  • parameter - the Parameter object which the constraint is evaluated

    against
    
  • value - parameter value to be checked

  • instance - the instance value is checked for

# File lib/aws/templates/utils/parametrized/constraint.rb, line 90
def check(parameter, value, instance); end