class MarkMapper::Normalizers::SortValue

Public Class Methods

new(args = {}) click to toggle source

Public: Initializes a MarkMapper::Normalizers::SortValue

args - The hash of arguments

:key_normalizer - What to use to normalize keys, must
                  respond to call.
# File lib/mark_mapper/normalizers/sort_value.rb, line 11
def initialize(args = {})
  @key_normalizer = args.fetch(:key_normalizer) {
    raise ArgumentError, "Missing required key :key_normalizer"
  }
end

Public Instance Methods

call(value) click to toggle source

Public: Given a value returns it normalized for MarkLogic’s sort option

# File lib/mark_mapper/normalizers/sort_value.rb, line 18
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
normalized_direction(field, direction=nil) click to toggle source

Private

# File lib/mark_mapper/normalizers/sort_value.rb, line 48
def normalized_direction(field, direction=nil)
  direction ||= 'ASC'
  direction = direction.upcase == 'ASC' ? 1 : -1
  [@key_normalizer.call(field).to_s, direction]
end
normalized_sort_piece(value) click to toggle source

Private

# File lib/mark_mapper/normalizers/sort_value.rb, line 32
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