Class: RestfulQuery::Sort
- Inherits:
-
Object
- Object
- RestfulQuery::Sort
- 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
-
#column ⇒ Object
Returns the value of attribute column.
-
#direction ⇒ Object
Returns the value of attribute direction.
Class Method Summary collapse
-
.next_direction(current_direction) ⇒ Object
Makes a roundabout for directions nil -> desc -> asc -> nil.
- .parse(sort_string, split_on = /\-|\ /) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(column, direction) ⇒ Sort
constructor
A new instance of Sort.
- #next_direction ⇒ Object
- #reverse_direction ⇒ Object
- #to_s(join = '-') ⇒ Object
- #to_sequel ⇒ Object
- #to_sql ⇒ Object
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
#column ⇒ Object
Returns the value of attribute column.
7 8 9 |
# File 'lib/restful_query/sort.rb', line 7 def column @column end |
#direction ⇒ Object
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_direction ⇒ Object
60 61 62 |
# File 'lib/restful_query/sort.rb', line 60 def next_direction self.class.next_direction(direction) end |
#reverse_direction ⇒ Object
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_sequel ⇒ Object
6 7 8 |
# File 'lib/sequel/extensions/restful_query.rb', line 6 def to_sequel column.to_sym.send(direction.downcase) end |
#to_sql ⇒ Object
68 69 70 |
# File 'lib/restful_query/sort.rb', line 68 def to_sql "#{column} #{direction}" end |