Class: Stretchy::Builders::WhereBuilder
- Inherits:
-
Object
- Object
- Stretchy::Builders::WhereBuilder
- Defined in:
- lib/stretchy/builders/where_builder.rb
Instance Attribute Summary collapse
-
#antiexists ⇒ Object
Returns the value of attribute antiexists.
-
#antigeos ⇒ Object
Returns the value of attribute antigeos.
-
#antiranges ⇒ Object
Returns the value of attribute antiranges.
-
#antiterms ⇒ Object
Returns the value of attribute antiterms.
-
#exists ⇒ Object
Returns the value of attribute exists.
-
#geos ⇒ Object
Returns the value of attribute geos.
-
#ranges ⇒ Object
Returns the value of attribute ranges.
-
#shouldexists ⇒ Object
Returns the value of attribute shouldexists.
-
#shouldgeos ⇒ Object
Returns the value of attribute shouldgeos.
-
#shouldnotexists ⇒ Object
Returns the value of attribute shouldnotexists.
-
#shouldnotgeos ⇒ Object
Returns the value of attribute shouldnotgeos.
-
#shouldnotranges ⇒ Object
Returns the value of attribute shouldnotranges.
-
#shouldnotterms ⇒ Object
Returns the value of attribute shouldnotterms.
-
#shouldranges ⇒ Object
Returns the value of attribute shouldranges.
-
#shouldterms ⇒ Object
Returns the value of attribute shouldterms.
-
#terms ⇒ Object
Returns the value of attribute terms.
Instance Method Summary collapse
- #and_filter ⇒ Object
- #any? ⇒ Boolean
- #bool_filter ⇒ Object
- #build ⇒ Object
- #build_filters(options = {}) ⇒ Object
- #build_should ⇒ Object
-
#initialize(options = {}) ⇒ WhereBuilder
constructor
A new instance of WhereBuilder.
- #must_nots? ⇒ Boolean
- #musts? ⇒ Boolean
- #not_filter ⇒ Object
- #should_nots? ⇒ Boolean
- #shoulds? ⇒ Boolean
- #use_bool? ⇒ Boolean
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( = {}) @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
#antiexists ⇒ Object
Returns the value of attribute antiexists.
5 6 7 |
# File 'lib/stretchy/builders/where_builder.rb', line 5 def antiexists @antiexists end |
#antigeos ⇒ Object
Returns the value of attribute antigeos.
5 6 7 |
# File 'lib/stretchy/builders/where_builder.rb', line 5 def antigeos @antigeos end |
#antiranges ⇒ Object
Returns the value of attribute antiranges.
5 6 7 |
# File 'lib/stretchy/builders/where_builder.rb', line 5 def antiranges @antiranges end |
#antiterms ⇒ Object
Returns the value of attribute antiterms.
5 6 7 |
# File 'lib/stretchy/builders/where_builder.rb', line 5 def antiterms @antiterms end |
#exists ⇒ Object
Returns the value of attribute exists.
5 6 7 |
# File 'lib/stretchy/builders/where_builder.rb', line 5 def exists @exists end |
#geos ⇒ Object
Returns the value of attribute geos.
5 6 7 |
# File 'lib/stretchy/builders/where_builder.rb', line 5 def geos @geos end |
#ranges ⇒ Object
Returns the value of attribute ranges.
5 6 7 |
# File 'lib/stretchy/builders/where_builder.rb', line 5 def ranges @ranges end |
#shouldexists ⇒ Object
Returns the value of attribute shouldexists.
5 6 7 |
# File 'lib/stretchy/builders/where_builder.rb', line 5 def shouldexists @shouldexists end |
#shouldgeos ⇒ Object
Returns the value of attribute shouldgeos.
5 6 7 |
# File 'lib/stretchy/builders/where_builder.rb', line 5 def shouldgeos @shouldgeos end |
#shouldnotexists ⇒ Object
Returns the value of attribute shouldnotexists.
5 6 7 |
# File 'lib/stretchy/builders/where_builder.rb', line 5 def shouldnotexists @shouldnotexists end |
#shouldnotgeos ⇒ Object
Returns the value of attribute shouldnotgeos.
5 6 7 |
# File 'lib/stretchy/builders/where_builder.rb', line 5 def shouldnotgeos @shouldnotgeos end |
#shouldnotranges ⇒ Object
Returns the value of attribute shouldnotranges.
5 6 7 |
# File 'lib/stretchy/builders/where_builder.rb', line 5 def shouldnotranges @shouldnotranges end |
#shouldnotterms ⇒ Object
Returns the value of attribute shouldnotterms.
5 6 7 |
# File 'lib/stretchy/builders/where_builder.rb', line 5 def shouldnotterms @shouldnotterms end |
#shouldranges ⇒ Object
Returns the value of attribute shouldranges.
5 6 7 |
# File 'lib/stretchy/builders/where_builder.rb', line 5 def shouldranges @shouldranges end |
#shouldterms ⇒ Object
Returns the value of attribute shouldterms.
5 6 7 |
# File 'lib/stretchy/builders/where_builder.rb', line 5 def shouldterms @shouldterms end |
#terms ⇒ Object
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_filter ⇒ Object
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
71 72 73 |
# File 'lib/stretchy/builders/where_builder.rb', line 71 def any? musts? || must_nots? || shoulds? || should_nots? end |
#bool_filter ⇒ Object
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 |
#build ⇒ Object
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( = {}) filters = [] terms = Hash([:terms]) ranges = Hash([:ranges]) geos = Hash([:geos]) near_fields = Hash([:near_fields]) exists = Array([: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_should ⇒ Object
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
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
44 45 46 |
# File 'lib/stretchy/builders/where_builder.rb', line 44 def musts? @terms.any? || @exists.any? || @ranges.any? || @geos.any? end |
#not_filter ⇒ Object
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
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
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
67 68 69 |
# File 'lib/stretchy/builders/where_builder.rb', line 67 def use_bool? (musts? && must_nots?) || shoulds? || should_nots? end |