Class: Arelastic::Sorts::Field

Inherits:
Sort show all
Defined in:
lib/arelastic/sorts/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Nodes::Node

#==, #convert_to_elastic, #read_option!

Methods included from Arities::Binary

#binary

Methods included from Arities::Polyadic

#polyadic

Methods included from Arities::Unary

#unary

Constructor Details

#initialize(field, extra_options = nil) ⇒ Field

Field.new(‘price’).as_elastic

=> 'price'

Field.new(‘price’ => ‘asc’).as_elastic

=> {'price' => 'asc'}

Field.new(‘price’, ‘order’ => ‘asc’).as_elastic

=> {'price' => {'order', 'asc'}}

Field.new(=> ‘asc’, => ‘_last’).as_elastic

=> {'price' => {'order' => 'asc', 'missing' => '_last'}}


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/arelastic/sorts/field.rb', line 18

def initialize(field, extra_options = nil)
  if field.is_a?(Hash)
    @field, @options = field.first.to_a
    if extra_options
      @options = (@options.is_a?(Hash) ? @options : {'order' => @options}).update(extra_options)
    end
  else
    @field = field
    @options = extra_options
  end
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



4
5
6
# File 'lib/arelastic/sorts/field.rb', line 4

def field
  @field
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/arelastic/sorts/field.rb', line 4

def options
  @options
end

Instance Method Details

#as_elasticObject



44
45
46
47
48
49
50
# File 'lib/arelastic/sorts/field.rb', line 44

def as_elastic
  if options
    {field => options}
  else
    field
  end
end

#orderingObject



40
41
42
# File 'lib/arelastic/sorts/field.rb', line 40

def ordering
  (options.is_a?(Hash) ? options['order'] : options).to_s
end

#reverseObject



30
31
32
33
34
35
36
37
38
# File 'lib/arelastic/sorts/field.rb', line 30

def reverse
  reverse_order = ordering == 'desc' ? 'asc' : 'desc'

  if options.is_a?(Hash)
    Arelastic::Sorts::Field.new(field, options.merge('order' => reverse_order))
  else
    Arelastic::Sorts::Field.new(field => reverse_order)
  end
end