Class: Elasticband::Sort

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticband/sort.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sort_by) ⇒ Sort

Returns a new instance of Sort.



31
32
33
# File 'lib/elasticband/sort.rb', line 31

def initialize(sort_by)
  @sort_by = sort_by
end

Instance Attribute Details

#sort_byObject (readonly)

Returns the value of attribute sort_by.



29
30
31
# File 'lib/elasticband/sort.rb', line 29

def sort_by
  @sort_by
end

Class Method Details

.parse(options) ⇒ Object

Parses sort option to a Elasticsearch syntax.

#### Options

  • ‘sort:` List of sort params.

#### Examples “‘ Sort.parse(sort: { name: :desc })

> { sort: [{ name: :desc }] }

Sort.parse(sort: [{ name: :desc }, { created_at: :asc }])

> { sort: [{ name: :desc }, { created_at: :asc }] }

Sort.parse(sort: ‘-name,+created_at,id’)

> { sort: [{ name: :desc }, { created_at: :asc }, { id: :asc }] }

Sort.parse(sort: [‘-name’, ‘+created_at’, ‘id’])

> { sort: [{ name: :desc }, { created_at: :asc }, { id: :asc }] }

“‘



24
25
26
# File 'lib/elasticband/sort.rb', line 24

def parse(options)
  new(options[:sort]).parse
end

Instance Method Details

#parseObject



35
36
37
38
39
40
41
# File 'lib/elasticband/sort.rb', line 35

def parse
  return if sort_by.blank?
  return [sort_by] if sort_by.is_a?(Hash)

  @sort_by = sort_by.split(','.freeze) if sort_by.is_a?(String)
  sort_by.map { |param| parse_param(param) }
end