Class: Warped::Sort

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, alias_name: nil) ⇒ Sort

Returns a new instance of Sort.

Parameters:

  • name (String)

    The name of the sort.

  • alias_name (String) (defaults to: nil)

    The alias name of the sort, used for renaming the sort key in the URL params

Raises:

  • (ArgumentError)


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_nameObject

Returns the value of attribute alias_name.



12
13
14
# File 'lib/warped/api/sort.rb', line 12

def alias_name
  @alias_name
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/warped/api/sort.rb', line 12

def name
  @name
end

Class Method Details

.directionsArray<String>

Returns The valid sort directions.

Returns:

  • (Array<String>)

    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.

Parameters:

  • direction (String)

    The sort direction.

Returns:

  • (String)

    The sort direction.

Raises:



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.

Parameters:

  • direction (String)

    The sort direction.

Returns:

  • (String)

    The opposite sort direction.



44
45
46
# File 'lib/warped/api/sort.rb', line 44

def opposite_direction(direction)
  opposite_directions[direction]
end

#parameter_nameString

Returns The name to use in the URL params.

Returns:

  • (String)

    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