Class: Squeel::Nodes::Order
- Inherits:
-
Object
- Object
- Squeel::Nodes::Order
- Defined in:
- lib/squeel/nodes/order.rb
Overview
A node that represents SQL orderings, such as “people.id DESC”
Instance Attribute Summary collapse
-
#direction ⇒ Fixnum
readonly
1 or -1, depending on ascending or descending direction, respectively.
-
#expr ⇒ Object
readonly
The expression being ordered on.
Instance Method Summary collapse
-
#asc ⇒ Order
Set this node’s direction to ascending.
-
#ascending? ⇒ Boolean
Whether or not this node represents an ascending order.
-
#desc ⇒ Order
Set this node’s direction to descending.
-
#descending? ⇒ Boolean
Whether or not this node represents a descending order.
-
#initialize(expr, direction = 1) ⇒ Order
constructor
Create a new Order node with the given expression and direction.
-
#reverse! ⇒ Order
Reverse the node’s direction.
Constructor Details
#initialize(expr, direction = 1) ⇒ Order
Create a new Order node with the given expression and direction
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
#direction ⇒ Fixnum (readonly)
Returns 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 |
#expr ⇒ Object (readonly)
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
#asc ⇒ Order
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 |
#desc ⇒ Order
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 |
#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 |