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.
-
#options ⇒ Object
Returns the value of attribute options.
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, options = {}) ⇒ 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, options = {}) ⇒ Sort
Returns a new instance of Sort.
19 20 21 22 23 |
# File 'lib/restful_query/sort.rb', line 19 def initialize(column, direction, = {}) self.column = column self.direction = direction self. = 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 |
#options ⇒ Object
Returns the value of attribute options.
7 8 9 |
# File 'lib/restful_query/sort.rb', line 7 def end |
Class Method Details
.next_direction(current_direction) ⇒ Object
Makes a roundabout for directions nil -> desc -> asc -> nil
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/restful_query/sort.rb', line 63 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
25 26 27 28 29 |
# File 'lib/restful_query/sort.rb', line 25 def self.parse(sort_string, split_on = /\-|\ /) return unless sort_string column, direction, = sort_string.split(split_on, 3) new(column, direction, ) end |
Instance Method Details
#==(other) ⇒ Object
57 58 59 60 |
# File 'lib/restful_query/sort.rb', line 57 def ==(other) return false unless other.is_a?(Sort) column == other.column && direction == other.direction end |
#next_direction ⇒ Object
74 75 76 |
# File 'lib/restful_query/sort.rb', line 74 def next_direction self.class.next_direction(direction) end |
#reverse_direction ⇒ Object
53 54 55 |
# File 'lib/restful_query/sort.rb', line 53 def reverse_direction direction == 'ASC' ? 'DESC' : 'ASC' end |
#to_s(join = '-') ⇒ Object
78 79 80 81 82 |
# File 'lib/restful_query/sort.rb', line 78 def to_s(join = '-') s = "#{column}#{join}#{direction.downcase}" s << " #{options.inspect}" unless .empty? s 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
84 85 86 87 88 89 90 |
# File 'lib/restful_query/sort.rb', line 84 def to_sql sql = "#{column} #{direction}" unless .empty? sql << ' ' << .to_a.flatten.collect {|k| k.to_s.upcase }.join(' ') end sql end |