Class: Elasticband::Sort
- Inherits:
-
Object
- Object
- Elasticband::Sort
- Defined in:
- lib/elasticband/sort.rb
Instance Attribute Summary collapse
-
#sort_by ⇒ Object
readonly
Returns the value of attribute sort_by.
Class Method Summary collapse
-
.parse(options) ⇒ Object
Parses sort option to a Elasticsearch syntax.
Instance Method Summary collapse
-
#initialize(sort_by) ⇒ Sort
constructor
A new instance of Sort.
- #parse ⇒ Object
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_by ⇒ Object (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() new([:sort]).parse end |
Instance Method Details
#parse ⇒ Object
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 |