Class: Cuprum::Collections::Constraints::Ordering

Inherits:
Stannum::Constraints::Union
  • Object
show all
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 Method Summary collapse

Constructor Details

#initialize(optional: nil, required: nil, **options) ⇒ Ordering

Returns a new instance of Ordering.

Parameters:

  • options (Hash<Symbol, Object>)

    Configuration options for the constraint. Defaults to an empty Hash.



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, **options)
  super(
    *ordering_constraints,
    **resolve_required_option(
      optional: optional,
      required: required,
      **options
    )
  )
end

Class Method Details

.instanceCuprum::Collections::Constraints::Order::AttributesArray

Returns a cached instance of the constraint with default options.

Returns:



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

Generates an errors object for the given object.

Parameters:

  • actual (Object)

    The object to generate errors for.

  • errors (Stannum::Errors) (defaults to: nil)

    The errors object to append errors to. If an errors object is not given, a new errors object will be created.

Returns:

  • (Stannum::Errors)

    the given or generated errors object.



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.

Parameters:

  • actual (Object)

    The object to match.

Returns:

  • (true, false)

    true if the object is a valid ordering; otherwise false.



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

Generates an errors object for the given object when negated.

Parameters:

  • actual (Object)

    The object to generate errors for.

  • errors (Stannum::Errors) (defaults to: nil)

    The errors object to append errors to. If an errors object is not given, a new errors object will be created.

Returns:

  • (Stannum::Errors)

    the given or generated errors object.



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.

Parameters:

  • options (Hash)

    The options to update.

Returns:

  • (Stannum::Constraints::Base)

    the copied constraint.



99
100
101
# File 'lib/cuprum/collections/constraints/ordering.rb', line 99

def with_options(**options)
  super(**resolve_required_option(**options))
end