class Stannum::Constraints::Tuples::ExtraItems

Constraint for validating the length of an indexed object.

@example

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

Constants

NEGATED_TYPE

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

TYPE

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

Public Class Methods

new(expected_count, **options) click to toggle source

@param expected_count [Integer, Proc] The number of expected items. If a

Proc, will be evaluated each time the constraint is matched.

@param options [Hash<Symbol, Object>] Configuration options for the

constraint. Defaults to an empty Hash.
Calls superclass method Stannum::Constraints::Base::new
# File lib/stannum/constraints/tuples/extra_items.rb, line 26
def initialize(expected_count, **options)
  super(expected_count: expected_count, **options)
end

Public Instance Methods

does_not_match?(actual) click to toggle source

@return [true, false] true if the object responds to size and the object

size is greater than the number of expected items; otherwise false.
# 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) click to toggle source

(see Stannum::Constraints::Base#errors_for)

# 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_count() click to toggle source

@return [Integer] the number of expected items.

# 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
match?(actual)
Alias for: matches?
matches?(actual) click to toggle source

@return [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.
# 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
Also aliased as: match?

Private Instance Methods

add_invalid_tuple_error(actual:, errors:) click to toggle source
# File lib/stannum/constraints/tuples/extra_items.rb, line 72
def add_invalid_tuple_error(actual:, errors:)
  Stannum::Constraints::Signature
    .new(:size)
    .errors_for(actual, errors: errors)
end
each_extra_item(actual, &block) click to toggle source
# File lib/stannum/constraints/tuples/extra_items.rb, line 78
def each_extra_item(actual, &block)
  return if matches?(actual)

  actual[expected_count..-1].each.with_index(expected_count, &block)
end