Class: Stretchy::Builders::WhereBuilder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/stretchy/builders/where_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ WhereBuilder

Returns a new instance of WhereBuilder.



9
10
11
12
13
14
# File 'lib/stretchy/builders/where_builder.rb', line 9

def initialize(options = {})
  @must         = FilterBuilder.new
  @must_not     = FilterBuilder.new(inverse: true)
  @should       = FilterBuilder.new(should:  true)
  @should_not   = FilterBuilder.new(inverse: true, should: true)
end

Instance Attribute Details

#mustObject

Returns the value of attribute must.



7
8
9
# File 'lib/stretchy/builders/where_builder.rb', line 7

def must
  @must
end

#must_notObject

Returns the value of attribute must_not.



7
8
9
# File 'lib/stretchy/builders/where_builder.rb', line 7

def must_not
  @must_not
end

#shouldObject

Returns the value of attribute should.



7
8
9
# File 'lib/stretchy/builders/where_builder.rb', line 7

def should
  @should
end

#should_notObject

Returns the value of attribute should_not.



7
8
9
# File 'lib/stretchy/builders/where_builder.rb', line 7

def should_not
  @should_not
end

Instance Method Details

#add_geo(field, distance, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/stretchy/builders/where_builder.rb', line 35

def add_geo(field, distance, options = {})
  if options[:geo_point]
    geo_point = Types::GeoPoint.new(options[:geo_point])
  else
    geo_point = Types::GeoPoint.new(options)
  end
  
  builder_from_options(options).add_geo(field, distance, geo_point)
end

#add_param(field, param, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/stretchy/builders/where_builder.rb', line 24

def add_param(field, param, options = {})
  case param
  when nil
    builder_from_options(options.merge(inverse: !options[:inverse])).add_exists(field)
  when ::Range, Types::Range
    builder_from_options(options).add_range(field, param)
  else
    builder_from_options(options).add_terms(field, param)
  end
end

#add_range(field, options = {}) ⇒ Object



45
46
47
# File 'lib/stretchy/builders/where_builder.rb', line 45

def add_range(field, options = {})
  builder_from_options(options).add_range(field, options)
end

#any?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/stretchy/builders/where_builder.rb', line 16

def any?
  must.any? || must_not.any? || should.any? || should_not.any?
end

#to_filterObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/stretchy/builders/where_builder.rb', line 49

def to_filter
  if use_bool?
    bool_filter
  elsif must.any?
    and_filter
  elsif must_not.any?
    not_filter
  else
    nil
  end
end

#use_bool?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/stretchy/builders/where_builder.rb', line 20

def use_bool?
  (must.any? && must_not.any?) || should.any? || should_not.any?
end