class Cuprum::Collections::Constraints::Ordering

Asserts that the object is a valid query ordering.

A valid ordering can be any of the following:

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

instance() click to toggle source

@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
new(optional: nil, required: nil, **options) click to toggle source

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

constraint. Defaults to an empty Hash.
Calls superclass method
# 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

errors_for(_actual, errors: nil) click to toggle source

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

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.
Calls superclass method
# File lib/cuprum/collections/constraints/ordering.rb, line 75
def matches?(actual)
  return true if optional? && actual.nil?

  super
end
Also aliased as: match?
negated_errors_for(_actual, errors: nil) click to toggle source

@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
with_options(**options) click to toggle source

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.

Calls superclass method
# File lib/cuprum/collections/constraints/ordering.rb, line 99
def with_options(**options)
  super(**resolve_required_option(**options))
end

Private Instance Methods

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