Sha256: 94c4123301314c1fcc82454081d973142d60a17f2404c783c41ca8d6275c28f4
Contents?: true
Size: 1.01 KB
Versions: 206
Compression:
Stored size: 1.01 KB
Contents
# encoding: utf-8 module Mongoid #:nodoc: module Contexts #:nodoc: class Enumerable class Sort attr_reader :value, :direction # Create a new sorting object. This requires a value and a sort # direction of +:asc+ or +:desc+. def initialize(value, direction) @value = value @direction = direction end # Return +true+ if the direction is +:asc+, otherwise false. def ascending? direction == :asc end # Compare two +Sort+ objects against each other, taking into # consideration the direction of the sorting. def <=>(other) cmp = compare(value, other.value) ascending? ? cmp : cmp * -1 end private # Compare two values allowing for nil values. def compare(a, b) case when a.nil? b.nil? ? 0 : 1 when b.nil? -1 else a <=> b end end end end end end
Version data entries
206 entries across 114 versions & 6 rubygems