Class: Warped::Sort
- Inherits:
-
Object
- Object
- Warped::Sort
- Defined in:
- lib/warped/api/sort.rb,
lib/warped/api/sort/value.rb
Defined Under Namespace
Classes: DirectionError, Value
Constant Summary collapse
- SORT_DIRECTIONS =
%w[asc desc].freeze
- NULLS_SORT_DIRECTION =
%w[asc_nulls_first asc_nulls_last desc_nulls_first desc_nulls_last].freeze
Instance Attribute Summary collapse
-
#alias_name ⇒ Object
Returns the value of attribute alias_name.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
-
.directions ⇒ Array<String>
The valid sort directions.
Instance Method Summary collapse
-
#direction!(direction) ⇒ String
The sort direction.
-
#initialize(name, alias_name: nil) ⇒ Sort
constructor
A new instance of Sort.
-
#opposite_direction(direction) ⇒ String
The opposite sort direction.
-
#parameter_name ⇒ String
The name to use in the URL params.
Constructor Details
#initialize(name, alias_name: nil) ⇒ Sort
Returns a new instance of Sort.
21 22 23 24 25 26 |
# File 'lib/warped/api/sort.rb', line 21 def initialize(name, alias_name: nil) raise ArgumentError, "name cannot be nil" if name.nil? @name = name.to_s @alias_name = alias_name&.to_s end |
Instance Attribute Details
#alias_name ⇒ Object
Returns the value of attribute alias_name.
12 13 14 |
# File 'lib/warped/api/sort.rb', line 12 def alias_name @alias_name end |
#name ⇒ Object
Returns the value of attribute name.
12 13 14 |
# File 'lib/warped/api/sort.rb', line 12 def name @name end |
Class Method Details
.directions ⇒ Array<String>
Returns The valid sort directions.
15 16 17 |
# File 'lib/warped/api/sort.rb', line 15 def self.directions @directions ||= SORT_DIRECTIONS + NULLS_SORT_DIRECTION end |
Instance Method Details
#direction!(direction) ⇒ String
Returns The sort direction.
36 37 38 39 40 |
# File 'lib/warped/api/sort.rb', line 36 def direction!(direction) raise DirectionError, "Invalid direction: #{direction}" unless valid_direction?(direction.to_s) direction.to_s end |
#opposite_direction(direction) ⇒ String
Returns The opposite sort direction.
44 45 46 |
# File 'lib/warped/api/sort.rb', line 44 def opposite_direction(direction) opposite_directions[direction] end |
#parameter_name ⇒ String
Returns The name to use in the URL params.
29 30 31 |
# File 'lib/warped/api/sort.rb', line 29 def parameter_name alias_name.presence || name end |