Class: Mongoid::Contexts::Enumerable::Sort
- Defined in:
- lib/mongoid/contexts/enumerable/sort.rb
Instance Attribute Summary collapse
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare two
Sort
objects against each other, taking into consideration the direction of the sorting. -
#ascending? ⇒ Boolean
Return
true
if the direction is:asc
, otherwise false. -
#initialize(value, direction) ⇒ Sort
constructor
Create a new sorting object.
Constructor Details
#initialize(value, direction) ⇒ Sort
Create a new sorting object. This requires a value and a sort direction of :asc
or :desc
.
10 11 12 13 |
# File 'lib/mongoid/contexts/enumerable/sort.rb', line 10 def initialize(value, direction) @value = value @direction = direction end |
Instance Attribute Details
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
6 7 8 |
# File 'lib/mongoid/contexts/enumerable/sort.rb', line 6 def direction @direction end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
6 7 8 |
# File 'lib/mongoid/contexts/enumerable/sort.rb', line 6 def value @value end |
Instance Method Details
#<=>(other) ⇒ Object
Compare two Sort
objects against each other, taking into consideration the direction of the sorting.
22 23 24 25 |
# File 'lib/mongoid/contexts/enumerable/sort.rb', line 22 def <=>(other) cmp = compare(value, other.value) ascending? ? cmp : cmp * -1 end |
#ascending? ⇒ Boolean
Return true
if the direction is :asc
, otherwise false.
16 17 18 |
# File 'lib/mongoid/contexts/enumerable/sort.rb', line 16 def ascending? direction == :asc end |