Class: Mongoid::Contexts::Enumerable::Sort

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/contexts/enumerable/sort.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#directionObject (readonly)

Returns the value of attribute direction.



6
7
8
# File 'lib/mongoid/contexts/enumerable/sort.rb', line 6

def direction
  @direction
end

#valueObject (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.

Returns:



16
17
18
# File 'lib/mongoid/contexts/enumerable/sort.rb', line 16

def ascending?
  direction == :asc
end