Class: Stannum::Constraints::Tuples::ExtraItems

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

Overview

Constraint for validating the length of an indexed object.

Examples:

constraint = Stannum::Constraints::Tuples::ExtraItems.new(3)

constraint.matches?([])           #=> true
constraint.matches?([1])          #=> true
constraint.matches?([1, 2, 3])    #=> true
constraint.matches?([1, 2, 3, 4]) #=> false

Direct Known Subclasses

Parameters::ExtraArguments

Constant Summary collapse

NEGATED_TYPE =

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

'stannum.constraints.tuples.no_extra_items'
TYPE =

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

'stannum.constraints.tuples.extra_items'

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(expected_count, **options) ⇒ ExtraItems

Returns a new instance of ExtraItems.

Parameters:

  • expected_count (Integer, Proc)

    The number of expected items. If a Proc, will be evaluated each time the constraint is matched.

  • options (Hash<Symbol, Object>)

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



26
27
28
# File 'lib/stannum/constraints/tuples/extra_items.rb', line 26

def initialize(expected_count, **options)
  super(expected_count: expected_count, **options)
end

Instance Method Details

#does_not_match?(actual) ⇒ true, false

Returns true if the object responds to #size and the object size is greater than the number of expected items; otherwise false.

Returns:

  • (true, false)

    true if the object responds to #size and the object size is greater than the number of expected items; otherwise false.



32
33
34
35
36
# File 'lib/stannum/constraints/tuples/extra_items.rb', line 32

def does_not_match?(actual)
  return false unless actual.respond_to?(:size)

  actual.size > expected_count
end

#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:



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/stannum/constraints/tuples/extra_items.rb', line 39

def errors_for(actual, errors: nil)
  errors ||= Stannum::Errors.new

  unless actual.respond_to?(:size)
    return add_invalid_tuple_error(actual: actual, errors: errors)
  end

  each_extra_item(actual) do |item, index|
    errors[index].add(type, value: item)
  end

  errors
end

#expected_countInteger

Returns the number of expected items.

Returns:

  • (Integer)

    the number of expected items.



54
55
56
57
58
# File 'lib/stannum/constraints/tuples/extra_items.rb', line 54

def expected_count
  count = options[:expected_count]

  count.is_a?(Proc) ? count.call : count
end

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

Returns true if the object responds to #size and the object size is less than or equal to than the number of expected items; otherwise false.

Returns:

  • (true, false)

    true if the object responds to #size and the object size is less than or equal to than the number of expected items; otherwise false.



63
64
65
66
67
# File 'lib/stannum/constraints/tuples/extra_items.rb', line 63

def matches?(actual)
  return false unless actual.respond_to?(:size)

  actual.size <= expected_count
end