Class: Dbee::Query::Sorter

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

Instance Method Summary collapse

Constructor Details

#initialize(key_path:, direction: ASCENDING) ⇒ Sorter

Returns a new instance of Sorter.

Raises:

  • (ArgumentError)


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

#directionObject (readonly)

Returns the value of attribute direction.



22
23
24
# File 'lib/dbee/query/sorter.rb', line 22

def direction
  @direction
end

#key_pathObject (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

Returns:

  • (Boolean)


37
38
39
# File 'lib/dbee/query/sorter.rb', line 37

def ascending?
  !descending?
end

#descending?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/dbee/query/sorter.rb', line 33

def descending?
  direction == DESCENDING
end

#hashObject



41
42
43
# File 'lib/dbee/query/sorter.rb', line 41

def hash
  "#{key_path}#{direction}".hash
end