Class: CouchProxy::Collator

Inherits:
Object
  • Object
show all
Defined in:
lib/couchproxy/collator.rb

Overview

Implements the JSON sorting rules defined at wiki.apache.org/couchdb/View_collation.

Instance Method Summary collapse

Constructor Details

#initialize(reverse = false) ⇒ Collator

Returns a new instance of Collator.



9
10
11
# File 'lib/couchproxy/collator.rb', line 9

def initialize(reverse=false)
  @reverse = reverse
end

Instance Method Details

#compare(a, b) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/couchproxy/collator.rb', line 13

def compare(a, b)
  klass = compare_class(a, b)
  val = case klass
    when 0
      case a
        when String then compare_string(a, b)
        when Array  then compare_array(a, b)
        when Hash   then compare_array(a.to_a, b.to_a)
        else a <=> b
      end
    else
      klass
    end
  @reverse ? val * -1 : val
end