Class: Stannum::Constraints::Enum

Inherits:
Base
  • Object
show all
Defined in:
lib/stannum/constraints/enum.rb

Overview

An enum constraint asserts that the object is one of the given values.

Examples:

Using an Enum Constraint

constraint = Stannum::Constraints::Enum.new('red', 'green', 'blue')

constraint.matches?('red')    #=> true
constraint.matches?('yellow') #=> false

Constant Summary collapse

NEGATED_TYPE =

The :type of the error generated for a matching object.

'stannum.constraints.is_in_list'
TYPE =

The :type of the error generated for a non-matching object.

'stannum.constraints.is_not_in_list'

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#==, #clone, #does_not_match?, #dup, #match, #message, #negated_match, #negated_message, #negated_type, #type, #with_options

Constructor Details

#initialize(*expected_values, **options) ⇒ Enum

Returns a new instance of Enum.

Parameters:

  • expected_values (Array)

    the possible values for the object.

  • options (Hash<Symbol, Object>)

    Configuration options for the constraint. Defaults to an empty Hash.



26
27
28
29
30
31
32
# File 'lib/stannum/constraints/enum.rb', line 26

def initialize(first, *rest, **options)
  expected_values = rest.unshift(first)

  super(expected_values: expected_values, **options)

  @matching_values = Set.new(expected_values)
end

Instance Method Details

#errors_for(actual, errors: nil) ⇒ Stannum::Errors

Note:

This method should only be called for an object that does not match the constraint. Generating errors for a matching object can result in undefined behavior.

Generates an errors object for the given object.

The errors object represents the difference between the given object and the expected properties or behavior. It may be the same for all objects, or different based on the details of the object or the constraint.

Examples:

Generating errors for a non-matching object.

constraint = CustomConstraint.new
object     = NonMatchingObject.new
errors     = constraint.errors_for(object)

errors.class #=> Stannum::Errors
errors.to_a  #=> [{ type: 'some_error', message: 'some error message' }]

Parameters:

  • actual (Object)

    The object to generate errors for.

  • errors (Stannum::Errors) (defaults to: nil)

    The errors object to append errors to. If an errors object is not given, a new errors object will be created.

Returns:

See Also:



35
36
37
# File 'lib/stannum/constraints/enum.rb', line 35

def errors_for(actual, errors: nil) # rubocop:disable Lint/UnusedMethodArgument
  (errors || Stannum::Errors.new).add(type, values: expected_values)
end

#expected_valuesArray

Returns the possible values for the object.

Returns:

  • (Array)

    the possible values for the object.



40
41
42
# File 'lib/stannum/constraints/enum.rb', line 40

def expected_values
  options[:expected_values]
end

#matches?(actual) ⇒ true, false Also known as: match?

Checks that the object is in the list of expected values.

Returns:

  • (true, false)

    false if the object is in the list of expected values, otherwise true.

See Also:



55
56
57
# File 'lib/stannum/constraints/enum.rb', line 55

def matches?(actual)
  @matching_values.include?(actual)
end

#negated_errors_for(actual, errors: nil) ⇒ Stannum::Errors

Note:

This method should only be called for an object that matches the constraint. Generating errors for a matching object can result in undefined behavior.

Generates an errors object for the given object when negated.

The errors object represents the difference between the given object and the expected properties or behavior when the constraint is negated. It may be the same for all objects, or different based on the details of the object or the constraint.

Examples:

Generating errors for a matching object.

constraint = CustomConstraint.new
object     = MatchingObject.new
errors     = constraint.negated_errors_for(object)

errors.class #=> Stannum::Errors
errors.to_a  #=> [{ type: 'some_error', message: 'some error message' }]

Parameters:

  • actual (Object)

    The object to generate errors for.

  • errors (Stannum::Errors) (defaults to: nil)

    The errors object to append errors to. If an errors object is not given, a new errors object will be created.

Returns:

See Also:



45
46
47
# File 'lib/stannum/constraints/enum.rb', line 45

def negated_errors_for(actual, errors: nil) # rubocop:disable Lint/UnusedMethodArgument
  (errors || Stannum::Errors.new).add(negated_type, values: expected_values)
end