Class: Dbee::Query::Sorter
- Inherits:
-
Object
- Object
- Dbee::Query::Sorter
- Includes:
- Direction
- Defined in:
- lib/dbee/query/sorter.rb
Overview
Abstract representation of the ORDER BY part of a SQL statement.
Defined Under Namespace
Modules: Direction
Constant Summary
Constants included from Direction
Direction::ASCENDING, Direction::DESCENDING
Instance Attribute Summary collapse
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#key_path ⇒ Object
readonly
Returns the value of attribute key_path.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #ascending? ⇒ Boolean
- #descending? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(key_path:, direction: ASCENDING) ⇒ Sorter
constructor
A new instance of Sorter.
Constructor Details
#initialize(key_path:, direction: ASCENDING) ⇒ Sorter
Returns a new instance of Sorter.
24 25 26 27 28 29 30 31 |
# File 'lib/dbee/query/sorter.rb', line 24 def initialize(key_path:, direction: ASCENDING) raise ArgumentError, 'key_path is required' if key_path.to_s.empty? @direction = Direction.const_get(direction.to_s.upcase.to_sym) @key_path = KeyPath.get(key_path) freeze end |
Instance Attribute Details
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
22 23 24 |
# File 'lib/dbee/query/sorter.rb', line 22 def direction @direction end |
#key_path ⇒ Object (readonly)
Returns the value of attribute key_path.
22 23 24 |
# File 'lib/dbee/query/sorter.rb', line 22 def key_path @key_path end |
Instance Method Details
#<=>(other) ⇒ Object
50 51 52 |
# File 'lib/dbee/query/sorter.rb', line 50 def <=>(other) "#{key_path}#{direction}" <=> "#{other.key_path}#{other.direction}" end |
#==(other) ⇒ Object Also known as: eql?
45 46 47 |
# File 'lib/dbee/query/sorter.rb', line 45 def ==(other) other.key_path == key_path && other.direction == direction end |
#ascending? ⇒ Boolean
37 38 39 |
# File 'lib/dbee/query/sorter.rb', line 37 def ascending? !descending? end |
#descending? ⇒ Boolean
33 34 35 |
# File 'lib/dbee/query/sorter.rb', line 33 def descending? direction == DESCENDING end |
#hash ⇒ Object
41 42 43 |
# File 'lib/dbee/query/sorter.rb', line 41 def hash "#{key_path}#{direction}".hash end |