class Cuprum::Collections::Constraints::Ordering
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.
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
@return [Cuprum::Collections::Constraints::Order::AttributesArray] a
cached instance of the constraint with default options.
# File lib/cuprum/collections/constraints/ordering.rb, line 40 def self.instance @instance ||= new end
@param options [Hash<Symbol, Object>] Configuration options for the
constraint. Defaults to an empty Hash.
# File lib/cuprum/collections/constraints/ordering.rb, line 46 def initialize(optional: nil, required: nil, **options) super( *ordering_constraints, **resolve_required_option( optional: optional, required: required, **options ) ) end
Public Instance Methods
@overload errors_for
(actual, errors: nil)
Generates an errors object for the given object. @param actual [Object] The object to generate errors for. @param errors [Stannum::Errors] The errors object to append errors to. If an errors object is not given, a new errors object will be created. @return [Stannum::Errors] the given or generated errors object.
# File lib/cuprum/collections/constraints/ordering.rb, line 65 def errors_for(_actual, errors: nil) (errors || Stannum::Errors.new).add(type) end
Checks that the given object matches the constraint.
@param actual [Object] The object to match.
@return [true, false] true if the object is a valid ordering; otherwise
false.
# File lib/cuprum/collections/constraints/ordering.rb, line 75 def matches?(actual) return true if optional? && actual.nil? super end
@overload negated_errors_for
(actual, errors: nil)
Generates an errors object for the given object when negated. @param actual [Object] The object to generate errors for. @param errors [Stannum::Errors] The errors object to append errors to. If an errors object is not given, a new errors object will be created. @return [Stannum::Errors] the given or generated errors object.
# File lib/cuprum/collections/constraints/ordering.rb, line 90 def negated_errors_for(_actual, errors: nil) (errors || Stannum::Errors.new).add(negated_type) end
Creates a copy of the constraint and updates the copy’s options.
@param options [Hash] The options to update.
@return [Stannum::Constraints::Base] the copied constraint.
# File lib/cuprum/collections/constraints/ordering.rb, line 99 def with_options(**options) super(**resolve_required_option(**options)) end
Private Instance Methods
# File lib/cuprum/collections/constraints/ordering.rb, line 105 def ordering_constraints [ Cuprum::Collections::Constraints::AttributeName.instance, Cuprum::Collections::Constraints::Order::AttributesArray.instance, Cuprum::Collections::Constraints::Order::AttributesHash.instance, Cuprum::Collections::Constraints::Order::ComplexOrdering.instance ] end