Class: Cuprum::Collections::Constraints::Ordering
- Inherits:
-
Stannum::Constraints::Union
- Object
- Stannum::Constraints::Union
- Cuprum::Collections::Constraints::Ordering
- Includes:
- Stannum::Support::Optional
- Defined in:
- lib/cuprum/collections/constraints/ordering.rb
Overview
Asserts that the object is a valid query ordering.
A valid ordering can be any of the following:
-
An attribute name (a non-empty string or symbol). e.g. ‘name’ or :title
-
An array of attribute names e.g. [‘author’, ‘title’]
-
A hash with attribute key names, whose values are valid sort directions. e.g. { author: :ascending, title: :descending }
-
An array of attribute names, followed by a valid hash. e.g. [‘author’, { title: :descending }]
Valid sort directions are :ascending and :descending (or :asc and :desc), and can be either strings or symbols.
Constant Summary collapse
- NEGATED_TYPE =
The :type of the error generated for a matching object.
'cuprum.collections.constraints.is_valid_ordering'
- TYPE =
The :type of the error generated for a non-matching object.
'cuprum.collections.constraints.is_not_valid_ordering'
Class Method Summary collapse
-
.instance ⇒ Cuprum::Collections::Constraints::Order::AttributesArray
A cached instance of the constraint with default options.
Instance Method Summary collapse
-
#errors_for(actual, errors: nil) ⇒ Stannum::Errors
Generates an errors object for the given object.
-
#initialize(optional: nil, required: nil, **options) ⇒ Ordering
constructor
A new instance of Ordering.
-
#matches?(actual) ⇒ true, false
(also: #match?)
Checks that the given object matches the constraint.
-
#negated_errors_for(actual, errors: nil) ⇒ Stannum::Errors
Generates an errors object for the given object when negated.
-
#with_options(**options) ⇒ Stannum::Constraints::Base
Creates a copy of the constraint and updates the copy’s options.
Constructor Details
#initialize(optional: nil, required: nil, **options) ⇒ Ordering
Returns a new instance of Ordering.
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/cuprum/collections/constraints/ordering.rb', line 46 def initialize(optional: nil, required: nil, **) super( *ordering_constraints, **resolve_required_option( optional: optional, required: required, ** ) ) end |
Class Method Details
.instance ⇒ Cuprum::Collections::Constraints::Order::AttributesArray
Returns a cached instance of the constraint with default options.
40 41 42 |
# File 'lib/cuprum/collections/constraints/ordering.rb', line 40 def self.instance @instance ||= new end |
Instance Method Details
#errors_for(actual, errors: nil) ⇒ Stannum::Errors
65 66 67 |
# File 'lib/cuprum/collections/constraints/ordering.rb', line 65 def errors_for(_actual, errors: nil) (errors || Stannum::Errors.new).add(type) end |
#matches?(actual) ⇒ true, false Also known as: match?
Checks that the given object matches the constraint.
75 76 77 78 79 |
# File 'lib/cuprum/collections/constraints/ordering.rb', line 75 def matches?(actual) return true if optional? && actual.nil? super end |
#negated_errors_for(actual, errors: nil) ⇒ Stannum::Errors
90 91 92 |
# File 'lib/cuprum/collections/constraints/ordering.rb', line 90 def negated_errors_for(_actual, errors: nil) (errors || Stannum::Errors.new).add(negated_type) end |
#with_options(**options) ⇒ Stannum::Constraints::Base
Creates a copy of the constraint and updates the copy’s options.
99 100 101 |
# File 'lib/cuprum/collections/constraints/ordering.rb', line 99 def (**) super(**resolve_required_option(**)) end |