Class: Stannum::Constraints::Tuples::ExtraItems
- Defined in:
- lib/stannum/constraints/tuples/extra_items.rb
Overview
Constraint for validating the length of an indexed object.
Direct Known Subclasses
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
Instance Method Summary collapse
-
#does_not_match?(actual) ⇒ true, false
True if the object responds to #size and the object size is greater than the number of expected items; otherwise false.
-
#errors_for(actual, errors: nil) ⇒ Stannum::Errors
Generates an errors object for the given object.
-
#expected_count ⇒ Integer
The number of expected items.
-
#initialize(expected_count, **options) ⇒ ExtraItems
constructor
A new instance of ExtraItems.
-
#matches?(actual) ⇒ true, false
(also: #match?)
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.
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.
26 27 28 |
# File 'lib/stannum/constraints/tuples/extra_items.rb', line 26 def initialize(expected_count, **) super(expected_count: expected_count, **) 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.
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
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.
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_count ⇒ Integer
Returns the number of expected items.
54 55 56 57 58 |
# File 'lib/stannum/constraints/tuples/extra_items.rb', line 54 def expected_count count = [: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.
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 |