Class: Aws::Templates::Utils::Parametrized::Constraint

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/templates/utils/parametrized/constraints.rb

Overview

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.

Defined Under Namespace

Classes: AllOf, DependsOnValue, Enum, Matches, NotNil, Requires, SatisfiesCondition

Instance Method Summary collapse

Instance Method Details

#check_wrapper(parameter, value, instance) ⇒ Object

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



340
341
342
343
344
# File 'lib/aws/templates/utils/parametrized/constraints.rb', line 340

def check_wrapper(parameter, value, instance)
  check(parameter, value, instance)
rescue StandardError
  raise ParameterValueInvalid.new(parameter, instance, value)
end

#to_procObject

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



322
323
324
325
326
327
328
# File 'lib/aws/templates/utils/parametrized/constraints.rb', line 322

def to_proc
  constraint = self

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