Class: Stannum::Constraints::Types::ArrayType
- Inherits:
-
Stannum::Constraints::Type
- Object
- Base
- Stannum::Constraints::Type
- Stannum::Constraints::Types::ArrayType
- Defined in:
- lib/stannum/constraints/types/array_type.rb
Overview
An Array type constraint asserts that the object is an Array.
Constant Summary
Constants inherited from Stannum::Constraints::Type
Stannum::Constraints::Type::NEGATED_TYPE, Stannum::Constraints::Type::TYPE
Constants inherited from Base
Base::NEGATED_TYPE, Base::TYPE
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#allow_empty? ⇒ true, false
If false, then the constraint will not match against an Array with no items.
-
#does_not_match?(actual) ⇒ true, false
Checks that the object is not an Array instance.
-
#errors_for(actual, errors: nil) ⇒ Stannum::Errors
Generates an errors object for the given object.
-
#initialize(allow_empty: true, item_type: nil, **options) ⇒ ArrayType
constructor
A new instance of ArrayType.
-
#item_type ⇒ Stannum::Constraints::Base?
The expected type for the items in the array.
-
#matches?(actual) ⇒ true, false
(also: #match?)
Checks that the object is an Array instance and that the items match.
Methods inherited from Stannum::Constraints::Type
#expected_type, #negated_errors_for, #with_options
Methods included from Support::Optional
#optional?, #required?, resolve
Methods inherited from Base
#==, #clone, #dup, #match, #message, #negated_errors_for, #negated_match, #negated_message, #negated_type, #type, #with_options
Constructor Details
#initialize(allow_empty: true, item_type: nil, **options) ⇒ ArrayType
Returns a new instance of ArrayType.
42 43 44 45 46 47 48 49 |
# File 'lib/stannum/constraints/types/array_type.rb', line 42 def initialize(allow_empty: true, item_type: nil, **) super( ::Array, allow_empty: !!allow_empty, item_type: coerce_item_type(item_type), ** ) end |
Instance Method Details
#allow_empty? ⇒ true, false
Returns if false, then the constraint will not match against an Array with no items.
53 54 55 |
# File 'lib/stannum/constraints/types/array_type.rb', line 53 def allow_empty? [:allow_empty] end |
#does_not_match?(actual) ⇒ true, false
Checks that the object is not an Array instance.
63 64 65 |
# File 'lib/stannum/constraints/types/array_type.rb', line 63 def does_not_match?(actual) !matches_type?(actual) 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.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/stannum/constraints/types/array_type.rb', line 68 def errors_for(actual, errors: nil) return super unless actual.is_a?(expected_type) errors ||= Stannum::Errors.new return add_presence_error(errors) unless presence_matches?(actual) unless item_type_matches?(actual) non_matching_items(actual).each do |item, index| item_type.errors_for(item, errors: errors[index]) end end errors end |
#item_type ⇒ Stannum::Constraints::Base?
Returns the expected type for the items in the array.
86 87 88 |
# File 'lib/stannum/constraints/types/array_type.rb', line 86 def item_type [:item_type] end |
#matches?(actual) ⇒ true, false Also known as: match?
Checks that the object is an Array instance and that the items match.
If the constraint was configured with an item_type, each item in the Array will be compared to the expected type. If any items do not match the expectation, then #matches? will return false.
100 101 102 103 104 105 106 107 108 |
# File 'lib/stannum/constraints/types/array_type.rb', line 100 def matches?(actual) return false unless super return false unless presence_matches?(actual) return false unless item_type_matches?(actual) true end |