Class: RestfulQuery::Sort

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/restful_query/sort.rb,
lib/sequel/extensions/restful_query.rb

Constant Summary collapse

DIRECTIONS =
{
  'up' => 'ASC',
  'asc' => 'ASC',
  'ASC' => 'ASC',
  'down' => 'DESC',
  'desc' => 'DESC',
  'DESC' => 'DESC'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column, direction) ⇒ Sort

Returns a new instance of Sort.



19
20
21
22
# File 'lib/restful_query/sort.rb', line 19

def initialize(column, direction)
  self.column    = column
  self.direction = direction
end

Instance Attribute Details

#columnObject

Returns the value of attribute column.



7
8
9
# File 'lib/restful_query/sort.rb', line 7

def column
  @column
end

#directionObject

Returns the value of attribute direction.



7
8
9
# File 'lib/restful_query/sort.rb', line 7

def direction
  @direction
end

Class Method Details

.next_direction(current_direction) ⇒ Object

Makes a roundabout for directions nil -> desc -> asc -> nil



49
50
51
52
53
54
55
56
57
58
# File 'lib/restful_query/sort.rb', line 49

def self.next_direction(current_direction)
  case current_direction.to_s.downcase
  when 'desc'
    'asc'
  when 'asc'
    nil
  else
    'desc'
  end
end

.parse(sort_string, split_on = /\-|\ /) ⇒ Object



24
25
26
27
28
# File 'lib/restful_query/sort.rb', line 24

def self.parse(sort_string, split_on = /\-|\ /)
  return unless sort_string
  column, direction = sort_string.split(split_on)
  new(column, direction)
end

Instance Method Details

#==(other) ⇒ Object



43
44
45
46
# File 'lib/restful_query/sort.rb', line 43

def ==(other)
  return false unless other.is_a?(Sort)
  column == other.column && direction == other.direction
end

#next_directionObject



60
61
62
# File 'lib/restful_query/sort.rb', line 60

def next_direction
  self.class.next_direction(direction)
end

#reverse_directionObject



39
40
41
# File 'lib/restful_query/sort.rb', line 39

def reverse_direction
  direction == 'ASC' ? 'DESC' : 'ASC'
end

#to_s(join = '-') ⇒ Object



64
65
66
# File 'lib/restful_query/sort.rb', line 64

def to_s(join = '-')
  "#{column}#{join}#{direction.downcase}"
end

#to_sequelObject



6
7
8
# File 'lib/sequel/extensions/restful_query.rb', line 6

def to_sequel
  column.to_sym.send(direction.downcase)
end

#to_sqlObject



68
69
70
# File 'lib/restful_query/sort.rb', line 68

def to_sql
  "#{column} #{direction}"
end