Class: Squeel::Nodes::Order

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

Constructor Details

#initialize(expr, direction = 1) ⇒ Order

Create a new Order node with the given expression and direction

Parameters:

  • expr

    The expression to order on

  • direction (Fixnum) (defaults to: 1)

    1 or -1, depending on the desired sort 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)

Returns 1 or -1, depending on ascending or descending direction, respectively.

Returns:

  • (Fixnum)

    1 or -1, depending on ascending or descending direction, respectively



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

def direction
  @direction
end

#exprObject (readonly)

Returns The expression being ordered on. Might be an attribute, function, or operation.

Returns:

  • The expression being ordered on. Might be an attribute, function, or operation



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

Returns:

  • (Order)

    This order node with an ascending direction



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

Returns:

  • (Boolean)

    True if the order is ascending



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

def ascending?
  @direction == 1
end

#descOrder

Set this node’s direction to descending

Returns:

  • (Order)

    This order node with a descending direction



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

Returns:

  • (Boolean)

    True if the order is descending



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

def descending?
  @direction == -1
end

#reverse!Order

Reverse the node’s direction

Returns:

  • (Order)

    This node with a reversed direction



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

def reverse!
  @direction = - @direction
  self
end