/
sort_value.rb
47 lines (43 loc) · 1.27 KB
/
sort_value.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module Plucky
module Normalizers
class SortValue
def initialize(args = {})
@key_normalizer = args.fetch(:key_normalizer) { raise ArgumentError, "Missing required key :key_normalizer" }
end
# Public: Given a value returns it normalized for Mongo's sort option
def call(value)
case value
when Array
if value.size == 1 && value[0].is_a?(String)
normalized_sort_piece(value[0])
else
value.compact.map { |v| normalized_sort_piece(v).flatten }
end
else
normalized_sort_piece(value)
end
end
# Private
def normalized_sort_piece(value)
case value
when SymbolOperator
[normalized_direction(value.field, value.operator)]
when String
value.split(',').map do |piece|
normalized_direction(*piece.split(' '))
end
when Symbol
[normalized_direction(value)]
else
value
end
end
# Private
def normalized_direction(field, direction=nil)
direction ||= 'ASC'
direction = direction.upcase == 'ASC' ? 1 : -1
[@key_normalizer.call(field).to_s, direction]
end
end
end
end