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)


26
27
28
29
30
31
32
33
# File 'lib/dbee/query/sorter.rb', line 26

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.



24
25
26
# File 'lib/dbee/query/sorter.rb', line 24

def direction
  @direction
end

#key_pathObject (readonly)

Returns the value of attribute key_path.



24
25
26
# File 'lib/dbee/query/sorter.rb', line 24

def key_path
  @key_path
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



47
48
49
# File 'lib/dbee/query/sorter.rb', line 47

def ==(other)
  other.key_path == key_path && other.direction == direction
end

#ascending?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/dbee/query/sorter.rb', line 39

def ascending?
  !descending?
end

#descending?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/dbee/query/sorter.rb', line 35

def descending?
  direction == DESCENDING
end

#hashObject



43
44
45
# File 'lib/dbee/query/sorter.rb', line 43

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