Class: Chewy::Query::Nodes::Range

Inherits:
Expr show all
Defined in:
lib/chewy/query/nodes/range.rb

Constant Summary collapse

EXECUTION =
{
  :i => :index,
  :index => :index,
  :f => :fielddata,
  :fielddata => :fielddata,
}

Instance Method Summary collapse

Methods inherited from Expr

#!, #|, #~

Methods inherited from Base

#eql?, #render

Constructor Details

#initialize(name, *args) ⇒ Range

Returns a new instance of Range.



12
13
14
15
16
17
18
19
# File 'lib/chewy/query/nodes/range.rb', line 12

def initialize name, *args
  @name = name.to_s
  @options = args.extract_options!
  @range = @options.reject { |k, v| ![:gt, :lt].include?(k) }
  @bounds = @options.reject { |k, v| ![:left_closed, :right_closed].include?(k) }
  execution = EXECUTION[args.first.to_sym] if args.first
  @options[:execution] = execution if execution
end

Instance Method Details

#&(other) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/chewy/query/nodes/range.rb', line 21

def & other
  if other.is_a?(self.class) && other.__name__ == @name
    state = __state__.merge(other.__state__)

    cache = other.__options__[:cache] || @options[:cache]
    state[:cache] = cache unless cache.nil?

    execution = other.__options__[:execution] || @options[:execution]
    state[:execution] = execution unless execution.nil?

    self.class.new(@name, state)
  else
    super
  end
end

#__name__Object



37
38
39
# File 'lib/chewy/query/nodes/range.rb', line 37

def __name__
  @name
end

#__options__Object



45
46
47
# File 'lib/chewy/query/nodes/range.rb', line 45

def __options__
  @options
end

#__render__Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/chewy/query/nodes/range.rb', line 49

def __render__
  body = {}
  body[@bounds[:left_closed] ? :gte : :gt] = @range[:gt] if @range.key?(:gt)
  body[@bounds[:right_closed] ? :lte : :lt] = @range[:lt] if @range.key?(:lt)

  filter = {@name => body}
  filter[:_cache] = !!@options[:cache] if @options.key?(:cache)
  filter.merge!(@options.slice(:execution))

  {range: filter}
end

#__state__Object



41
42
43
# File 'lib/chewy/query/nodes/range.rb', line 41

def __state__
  @range.merge(@bounds)
end