Class: Stretchy::Builders::WhereBuilder

Inherits:
Object
  • Object
show all
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.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/stretchy/builders/where_builder.rb', line 10

def initialize(options = {})
  @terms            = Hash.new { [] }
  @antiterms        = Hash.new { [] }
  @shouldterms      = Hash.new { [] }
  @shouldnotterms   = Hash.new { [] }
  
  @ranges           = {}
  @antiranges       = {}
  @shouldranges     = {}
  @shouldnotranges  = {}
  
  @geos             = {}
  @antigeos         = {}
  @shouldgeos       = {}
  @shouldnotgeos    = {}

  @exists           = []
  @antiexists       = []
  @shouldexists     = []
  @shouldnotexists  = []
end

Instance Attribute Details

#antiexistsObject

Returns the value of attribute antiexists.



5
6
7
# File 'lib/stretchy/builders/where_builder.rb', line 5

def antiexists
  @antiexists
end

#antigeosObject

Returns the value of attribute antigeos.



5
6
7
# File 'lib/stretchy/builders/where_builder.rb', line 5

def antigeos
  @antigeos
end

#antirangesObject

Returns the value of attribute antiranges.



5
6
7
# File 'lib/stretchy/builders/where_builder.rb', line 5

def antiranges
  @antiranges
end

#antitermsObject

Returns the value of attribute antiterms.



5
6
7
# File 'lib/stretchy/builders/where_builder.rb', line 5

def antiterms
  @antiterms
end

#existsObject

Returns the value of attribute exists.



5
6
7
# File 'lib/stretchy/builders/where_builder.rb', line 5

def exists
  @exists
end

#geosObject

Returns the value of attribute geos.



5
6
7
# File 'lib/stretchy/builders/where_builder.rb', line 5

def geos
  @geos
end

#rangesObject

Returns the value of attribute ranges.



5
6
7
# File 'lib/stretchy/builders/where_builder.rb', line 5

def ranges
  @ranges
end

#shouldexistsObject

Returns the value of attribute shouldexists.



5
6
7
# File 'lib/stretchy/builders/where_builder.rb', line 5

def shouldexists
  @shouldexists
end

#shouldgeosObject

Returns the value of attribute shouldgeos.



5
6
7
# File 'lib/stretchy/builders/where_builder.rb', line 5

def shouldgeos
  @shouldgeos
end

#shouldnotexistsObject

Returns the value of attribute shouldnotexists.



5
6
7
# File 'lib/stretchy/builders/where_builder.rb', line 5

def shouldnotexists
  @shouldnotexists
end

#shouldnotgeosObject

Returns the value of attribute shouldnotgeos.



5
6
7
# File 'lib/stretchy/builders/where_builder.rb', line 5

def shouldnotgeos
  @shouldnotgeos
end

#shouldnotrangesObject

Returns the value of attribute shouldnotranges.



5
6
7
# File 'lib/stretchy/builders/where_builder.rb', line 5

def shouldnotranges
  @shouldnotranges
end

#shouldnottermsObject

Returns the value of attribute shouldnotterms.



5
6
7
# File 'lib/stretchy/builders/where_builder.rb', line 5

def shouldnotterms
  @shouldnotterms
end

#shouldrangesObject

Returns the value of attribute shouldranges.



5
6
7
# File 'lib/stretchy/builders/where_builder.rb', line 5

def shouldranges
  @shouldranges
end

#shouldtermsObject

Returns the value of attribute shouldterms.



5
6
7
# File 'lib/stretchy/builders/where_builder.rb', line 5

def shouldterms
  @shouldterms
end

#termsObject

Returns the value of attribute terms.



5
6
7
# File 'lib/stretchy/builders/where_builder.rb', line 5

def terms
  @terms
end

Instance Method Details

#and_filterObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/stretchy/builders/where_builder.rb', line 93

def and_filter
  filter = nil
  filters = build_filters(
    terms:  @terms,
    exists: @exists,
    ranges: @ranges,
    geos:   @geos
  )
  if filters.count > 1
    filter = Stretchy::Filters::AndFilter.new(filters)
  else
    filter = filters.first
  end
  filter
end

#any?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/stretchy/builders/where_builder.rb', line 71

def any?
  musts? || must_nots? || shoulds? || should_nots?
end

#bool_filterObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/stretchy/builders/where_builder.rb', line 75

def bool_filter
  Stretchy::Filters::BoolFilter.new(
    must: build_filters(
      terms:  @terms,
      exists: @exists,
      ranges: @ranges,
      geos:   @geos
    ),
    must_not: build_filters(
      terms:  @antiterms,
      exists: @antiexists,
      ranges: @antiranges,
      geos:   @antigeos
    ),
    should: build_should
  )
end

#buildObject



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

def build
  if use_bool?
    bool_filter
  elsif musts?
    and_filter
  elsif must_nots?
    not_filter
  else
    nil
  end
end

#build_filters(options = {}) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/stretchy/builders/where_builder.rb', line 162

def build_filters(options = {})
  filters = []
  terms       = Hash(options[:terms])
  ranges      = Hash(options[:ranges])
  geos        = Hash(options[:geos])
  near_fields = Hash(options[:near_fields])
  exists      = Array(options[:exists])
  
  terms.each do |field, values|
    filters << Stretchy::Filters::TermsFilter.new(field, values)
  end
  
  filters += exists.map do |field|
    Stretchy::Filters::ExistsFilter.new(field)
  end

  filters += ranges.map do |field, value|
    Stretchy::Filters::RangeFilter.new(field: field, stretchy_range: value)
  end

  filters += geos.map do |field, values|
    Stretchy::Filters::GeoFilter.new(
      field: field,
      distance: values[:distance],
      geo_point: values[:geo_point],
      lat: values[:lat],
      lng: values[:lng]
    )
  end
  filters
end

#build_shouldObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/stretchy/builders/where_builder.rb', line 120

def build_should
  if shoulds? && should_nots?
    Stretchy::Filters::BoolFilter.new(
      must: build_filters(
        terms:  @shouldterms,
        exists: @shouldexists,
        ranges: @shouldranges,
        geos:   @shouldgeos
      ),
      must_not: build_filters(
        terms:  @shouldnotterms,
        exists: @shouldnotexists,
        ranges: @shouldnotranges,
        geos:   @shouldnotgeos
      )
    )
  elsif should_nots?
    filters = build_filters(
      terms:  @shouldnotterms,
      exists: @shouldnotexists,
      ranges: @shouldnotranges,
      geos:   @shouldnotgeos
    )
    if filters.count > 1
      filters = Stretchy::Filters::OrFilter.new(filters) 
    else
      filters = filters.first
    end
    
    Stretchy::Filters::NotFilter.new(filters)
  else
    filters = build_filters(
      terms:  @shouldterms,
      exists: @shouldexists,
      ranges: @shouldranges,
      geos:   @shouldgeos
    )
    filters = Stretchy::Filters::AndFilter.new(filters) if filters.count > 1
    filters
  end
end

#must_nots?Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/stretchy/builders/where_builder.rb', line 48

def must_nots?
  @antiterms.any?   || @antiexists.any? || 
  @antiranges.any?  || @antigeos.any?
end

#musts?Boolean

Returns:

  • (Boolean)


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

def musts?
  @terms.any? || @exists.any? || @ranges.any? || @geos.any?
end

#not_filterObject



109
110
111
112
113
114
115
116
117
118
# File 'lib/stretchy/builders/where_builder.rb', line 109

def not_filter
  filter = build_filters(
    terms:    @antiterms,
    exists:   @antiexists,
    ranges:   @antiranges,
    geos:     @antigeos
  )
  filter = Stretchy::Filters::OrFilter.new(filter) if filter.count > 1
  Stretchy::Filters::NotFilter.new(filter)
end

#should_nots?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'lib/stretchy/builders/where_builder.rb', line 60

def should_nots?
  @shouldnotterms.any?    ||
  @shouldnotranges.any?   ||
  @shouldnotgeos.any?     ||
  @shouldnotexists.any?
end

#shoulds?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
# File 'lib/stretchy/builders/where_builder.rb', line 53

def shoulds?
  @shouldterms.any?       ||
  @shouldranges.any?      ||
  @shouldgeos.any?        ||
  @shouldexists.any?
end

#use_bool?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/stretchy/builders/where_builder.rb', line 67

def use_bool?
  (musts? && must_nots?) || shoulds? || should_nots?
end