class ElasticsearchQuery::Sort

Public Class Methods

new( params = {} ) click to toggle source
# File lib/elasticsearch_query/sort.rb, line 3
def initialize( params = {} )
  @params  = params
end

Public Instance Methods

to_hash() click to toggle source
# File lib/elasticsearch_query/sort.rb, line 7
def to_hash
  return {} if sorts&.empty?
  if sorts.length == 1
    { sort: sorts.first }
  else
    { sort: sorts }
  end
end

Private Instance Methods

sorts() click to toggle source
# File lib/elasticsearch_query/sort.rb, line 18
def sorts
  @sorts ||= begin
    sorts = @params.fetch( :sort, "" ).split(',')

    sorts.each_with_object([]) do |field, arr|
      desc, field   = field.match(/^([-_])?(\w+)$/i)[1..2]
      arr << { field => desc ? :desc : :asc }
    end
  end
end