Class: Squeel::Nodes::Order

Inherits:
Node
  • Object
show all
Defined in:
lib/squeel/nodes/order.rb

Overview

A node that represents SQL orderings, such as “people.id DESC”

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#each, #grep

Constructor Details

#initialize(expr, direction = 1) ⇒ Order

Create a new Order node with the given expression and direction

Raises:

  • (ArgumentError)


14
15
16
17
# File 'lib/squeel/nodes/order.rb', line 14

def initialize(expr, direction = 1)
  raise ArgumentError, "Direction #{direction} is not valid. Must be -1 or 1." unless [-1,1].include? direction
  @expr, @direction = expr, direction
end

Instance Attribute Details

#directionFixnum (readonly)



9
10
11
# File 'lib/squeel/nodes/order.rb', line 9

def direction
  @direction
end

#exprObject (readonly)



6
7
8
# File 'lib/squeel/nodes/order.rb', line 6

def expr
  @expr
end

Instance Method Details

#ascOrder

Set this node’s direction to ascending



21
22
23
24
# File 'lib/squeel/nodes/order.rb', line 21

def asc
  @direction = 1
  self
end

#ascending?Boolean

Whether or not this node represents an ascending order



35
36
37
# File 'lib/squeel/nodes/order.rb', line 35

def ascending?
  @direction == 1
end

#descOrder

Set this node’s direction to descending



28
29
30
31
# File 'lib/squeel/nodes/order.rb', line 28

def desc
  @direction = -1
  self
end

#descending?Boolean

Whether or not this node represents a descending order



41
42
43
# File 'lib/squeel/nodes/order.rb', line 41

def descending?
  @direction == -1
end

#eql?(other) ⇒ Boolean



56
57
58
59
60
# File 'lib/squeel/nodes/order.rb', line 56

def eql?(other)
  self.class.eql?(other.class) &&
    self.expr.eql?(other.expr) &&
    self.direction.eql?(other.direction)
end

#hashObject



52
53
54
# File 'lib/squeel/nodes/order.rb', line 52

def hash
  [@expr, @direction].hash
end

#reverse!Order

Reverse the node’s direction



47
48
49
50
# File 'lib/squeel/nodes/order.rb', line 47

def reverse!
  @direction = - @direction
  self
end