mongoid_geospatial 2.0.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/Gemfile +2 -1
  2. data/README.md +84 -60
  3. data/lib/mongoid_geospatial.rb +3 -7
  4. data/lib/mongoid_geospatial/{geospatial → extensions}/core_ext.rb +0 -0
  5. data/lib/mongoid_geospatial/field_option.rb +3 -4
  6. data/lib/mongoid_geospatial/fields/geometry_field.rb +34 -0
  7. data/lib/mongoid_geospatial/fields/line_string.rb +5 -7
  8. data/lib/mongoid_geospatial/fields/point.rb +18 -33
  9. data/lib/mongoid_geospatial/fields/polygon.rb +6 -14
  10. data/lib/mongoid_geospatial/geospatial.rb +20 -25
  11. data/lib/mongoid_geospatial/version.rb +1 -1
  12. data/lib/mongoid_geospatial/wrappers/georuby.rb +28 -0
  13. data/lib/mongoid_geospatial/wrappers/rgeo.rb +32 -0
  14. data/spec/models/farm.rb +5 -2
  15. data/spec/models/person.rb +4 -14
  16. data/spec/models/phone.rb +1 -3
  17. data/spec/mongoid_geospatial/extensions/core_ext_spec.rb +20 -0
  18. data/spec/mongoid_geospatial/field_option_spec.rb +11 -0
  19. data/spec/mongoid_geospatial/fields/line_string_spec.rb +40 -0
  20. data/spec/mongoid_geospatial/fields/point_spec.rb +136 -10
  21. data/spec/mongoid_geospatial/fields/polygon_spec.rb +75 -0
  22. data/spec/mongoid_geospatial/geospatial_spec.rb +88 -3
  23. data/spec/mongoid_geospatial/wrappers/rgeo_spec.rb +43 -0
  24. data/spec/spec_helper.rb +5 -21
  25. metadata +14 -26
  26. data/lib/mongoid_geospatial/contextual/mongo.rb +0 -118
  27. data/lib/mongoid_geospatial/criteria.rb +0 -10
  28. data/lib/mongoid_geospatial/criterion/complex.rb +0 -26
  29. data/lib/mongoid_geospatial/criterion/inclusion.rb +0 -14
  30. data/lib/mongoid_geospatial/criterion/near_spatial.rb +0 -52
  31. data/lib/mongoid_geospatial/criterion/within_spatial.rb +0 -62
  32. data/lib/mongoid_geospatial/extensions/symbol.rb +0 -46
  33. data/lib/mongoid_geospatial/finders.rb +0 -5
  34. data/lib/mongoid_geospatial/geospatial/geo_near_results.rb +0 -140
  35. data/spec/mongoid_geospatial/contextual/mongo_spec.rb +0 -135
  36. data/spec/mongoid_geospatial/criterion/complex_spec.rb +0 -15
  37. data/spec/mongoid_geospatial/criterion/inclusion_spec.rb +0 -375
  38. data/spec/mongoid_geospatial/criterion/near_spatial_spec.rb +0 -39
  39. data/spec/mongoid_geospatial/criterion/within_spatial_spec.rb +0 -54
  40. data/spec/mongoid_geospatial/geospatial/geo_near_results_spec.rb +0 -78
  41. data/spec/mongoid_geospatial/mongoid_geospatial_spec.rb +0 -83
@@ -1,15 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Mongoid::Criterion::Complex do
4
-
5
- let(:complex) { Mongoid::Criterion::Complex.new(:key => :field, :operator => "gt") }
6
-
7
- let(:value) { 40 }
8
-
9
- context "#to_mongo_query" do
10
- it "should turn value into appropriate query" do
11
- complex.to_mongo_query(value).should == {"$gt" => value}
12
- end
13
- end
14
-
15
- end
@@ -1,375 +0,0 @@
1
- require "spec_helper"
2
-
3
- # describe "Mongoid::Criterion::Inclusion" do
4
- describe Mongoid::Criteria do
5
-
6
- before do
7
- Person.delete_all
8
- end
9
-
10
- describe "#where" do
11
-
12
- let(:dob) do
13
- 33.years.ago.to_date
14
- end
15
-
16
- let(:lunch_time) do
17
- 30.minutes.ago
18
- end
19
-
20
- let!(:person) do
21
- Person.create(
22
- :title => "Sir",
23
- :dob => dob,
24
- :lunch_time => lunch_time,
25
- :age => 33,
26
- :aliases => [ "D", "Durran" ],
27
- :things => [ { :phone => 'HTC Incredible' } ]
28
- )
29
- end
30
-
31
- context "when providing 24 character strings" do
32
-
33
- context "when the field is not an id field" do
34
-
35
- let(:string) do
36
- bson_object_id_class.new.to_s
37
- end
38
-
39
- let!(:person) do
40
- Person.create(:title => string)
41
- end
42
-
43
- let(:from_db) do
44
- Person.where(:title => string)
45
- end
46
-
47
- it "does not convert the field to a bson id" do
48
- from_db.should == [ person ]
49
- end
50
- end
51
- end
52
-
53
- context "when providing string object ids" do
54
-
55
- context "when providing a single id" do
56
-
57
- let(:from_db) do
58
- Person.where(:_id => person.id.to_s).first
59
- end
60
-
61
- it "returns the matching documents" do
62
- from_db.should == person
63
- end
64
- end
65
- end
66
-
67
- context "chaining multiple wheres" do
68
-
69
- context "when chaining on the same key" do
70
-
71
- let(:from_db) do
72
- Person.where(:title => "Maam").where(:title => "Sir")
73
- end
74
-
75
- it "overrides the previous key" do
76
- from_db.should == [ person ]
77
- end
78
- end
79
-
80
- context "with different criteria on the same key" do
81
-
82
- it "merges criteria" do
83
- Person.where(:age.gt => 30).where(:age.lt => 40).should == [person]
84
- end
85
-
86
- it "typecasts criteria" do
87
- before_dob = (dob - 1.month).to_s
88
- after_dob = (dob + 1.month).to_s
89
- Person.where(:dob.gt => before_dob).and(:dob.lt => after_dob).should == [person]
90
- end
91
-
92
- end
93
- end
94
-
95
- context "with untyped criteria" do
96
-
97
- it "typecasts integers" do
98
- pending "seems typecasts not work fo mongoid 3.0" if Mongoid::VERSION > '3'
99
- Person.where(:age => "33").should == [ person ]
100
- end
101
-
102
- it "typecasts datetimes" do
103
- pending "seems typecasts not work fo mongoid 3.0" if Mongoid::VERSION > '3'
104
- Person.where(:lunch_time => lunch_time.to_s).should == [ person ]
105
- end
106
-
107
- it "typecasts dates" do
108
- pending "seems typecasts not work fo mongoid 3.0" if Mongoid::VERSION > '3'
109
- Person.where({:dob => dob.to_s}).should == [ person ]
110
- end
111
-
112
- it "typecasts times with zones" do
113
- pending "seems typecasts not work fo mongoid 3.0" if Mongoid::VERSION > '3'
114
- time = lunch_time.in_time_zone("Alaska")
115
- Person.where(:lunch_time => time).should == [ person ]
116
- end
117
-
118
- it "typecasts array elements" do
119
- pending "seems typecasts not work fo mongoid 3.0" if Mongoid::VERSION > '3'
120
- Person.where(:age.in => [17, "33"]).should == [ person ]
121
- end
122
-
123
- it "typecasts size criterion to integer" do
124
- pending "seems typecasts not work fo mongoid 3.0" if Mongoid::VERSION > '3'
125
- Person.where(:aliases.size => "2").should == [ person ]
126
- end
127
-
128
- it "typecasts exists criterion to boolean" do
129
- pending "seems typecasts not work fo mongoid 3.0" if Mongoid::VERSION > '3'
130
- Person.where(:score.exists => "f").should == [ person ]
131
- end
132
- end
133
-
134
- context "with multiple complex criteria" do
135
-
136
- before do
137
- Person.create(:title => "Mrs", :age => 29)
138
- Person.create(:title => "Ms", :age => 41)
139
- end
140
-
141
- it "returns those matching both criteria" do
142
- Person.where(:age.gt => 30, :age.lt => 40).should == [person]
143
- end
144
-
145
- it "returns nothing if in and nin clauses cancel each other out" do
146
- Person.any_in(:title => ["Sir"]).not_in(:title => ["Sir"]).should == []
147
- end
148
-
149
- it "returns nothing if in and nin clauses cancel each other out ordered the other way" do
150
- Person.not_in(:title => ["Sir"]).any_in(:title => ["Sir"]).should == []
151
- end
152
-
153
- it "returns the intersection of in and nin clauses" do
154
- Person.any_in(:title => ["Sir", "Mrs"]).not_in(:title => ["Mrs"]).should == [person]
155
- end
156
-
157
- it "returns the intersection of two in clauses" do
158
- pending 'seems not work fo mongoid 3.0 (selector: {"title"=>{"$in"=>["Sir", "Ms"]})' if Mongoid::VERSION > '3'
159
- Person.where(:title.in => ["Sir", "Mrs"]).where(:title.in => ["Sir", "Ms"]).should == [person]
160
- end
161
- end
162
-
163
- context "with complex criterion" do
164
-
165
- context "#all" do
166
-
167
- it "returns those matching an all clause" do
168
- Person.where(:aliases.all => ["D", "Durran"]).should == [person]
169
- end
170
- end
171
-
172
- context "#exists" do
173
-
174
- it "returns those matching an exists clause" do
175
- Person.where(:title.exists => true).should == [person]
176
- end
177
- end
178
-
179
- context "#gt" do
180
-
181
- it "returns those matching a gt clause" do
182
- Person.where(:age.gt => 30).should == [person]
183
- end
184
- end
185
-
186
- context "#gte" do
187
-
188
- it "returns those matching a gte clause" do
189
- Person.where(:age.gte => 33).should == [person]
190
- end
191
- end
192
-
193
- context "#in" do
194
-
195
- it "returns those matching an in clause" do
196
- Person.where(:title.in => ["Sir", "Madam"]).should == [person]
197
- end
198
-
199
- it "allows nil" do
200
- Person.where(:ssn.in => [nil]).should == [person]
201
- end
202
- end
203
-
204
- context "#lt" do
205
-
206
- it "returns those matching a lt clause" do
207
- Person.where(:age.lt => 34).should == [person]
208
- end
209
- end
210
-
211
- context "#lte" do
212
-
213
- it "returns those matching a lte clause" do
214
- Person.where(:age.lte => 33).should == [person]
215
- end
216
- end
217
-
218
- context "#ne" do
219
-
220
- it "returns those matching a ne clause" do
221
- Person.where(:age.ne => 50).should == [person]
222
- end
223
- end
224
-
225
- context "#nin" do
226
-
227
- it "returns those matching a nin clause" do
228
- Person.where(:title.nin => ["Esquire", "Congressman"]).should == [person]
229
- end
230
- end
231
-
232
- context "#size" do
233
-
234
- it "returns those matching a size clause" do
235
- query_key = Mongoid::VERSION > '3' ? :aliases.with_size : :aliases.size
236
- Person.where( query_key => 2).should == [person]
237
- end
238
- end
239
-
240
- context "#match" do
241
-
242
- it "returns those matching a partial element in a list" do
243
- if Mongoid::VERSION > '3'
244
- Person.where({'things.phone' => "HTC Incredible" }).should == [person]
245
- else
246
- Person.where(:things.matches => { :phone => "HTC Incredible" }).should == [person]
247
- end
248
- end
249
- end
250
-
251
- end
252
-
253
- context "Geo Spatial Complex Where" do
254
-
255
- let!(:home) do
256
- [-73.98,40.77]
257
- end
258
-
259
- describe "#near" do
260
- before do
261
- Bar.delete_all
262
- Bar.create_indexes
263
- end
264
-
265
- let!(:berlin) do
266
- Bar.create(:name => :berlin, :location => [ 52.30, 13.25 ])
267
- end
268
-
269
- let!(:prague) do
270
- Bar.create(:name => :prague, :location => [ 50.5, 14.26 ])
271
- end
272
-
273
- let!(:paris) do
274
- Bar.create(:name => :paris, :location => [ 48.48, 2.20 ])
275
- end
276
-
277
- it "returns the documents sorted closest to furthest" do
278
- Bar.where(:location.near => [ 41.23, 2.9 ]).should == [ paris, prague, berlin ]
279
- end
280
-
281
- it "returns the documents sorted closest to furthest" do
282
- pending "NearSpatial#to_mongo_query seems not work for mongoid 3 " if Mongoid::VERSION > '3'
283
- Bar.where(:location.near => {:point=>[ 41.23, 2.9 ],:max => 20}).should == [ paris, prague, berlin ]
284
- end
285
-
286
- it "returns the documents sorted closest to furthest" do
287
- Bar.where(:location.near_sphere => [ 41.23, 2.9 ]).should == [ paris, prague, berlin ]
288
- end
289
-
290
- it "should find closest using rgeo point" do
291
- Bar.where(:location.near => paris.location).to_a.should == [paris, berlin, prague]
292
- end
293
-
294
- end
295
-
296
- context "#within" do
297
-
298
- context ":box, :polygon" do
299
- before do
300
- Bar.delete_all
301
- Bar.create_indexes
302
- end
303
-
304
- let!(:berlin) do
305
- Bar.create(:name => 'berlin', :location => [ 52.30, 13.25 ])
306
- end
307
-
308
- let!(:prague) do
309
- Bar.create(:name => 'prague',:location => [ 50.5, 14.26 ])
310
- end
311
-
312
- let!(:paris) do
313
- Bar.create(:name => 'prague',:location => [ 48.48, 2.20 ])
314
- end
315
-
316
- it "returns the documents within a box" do
317
- Bar.where(:location.within(:box) => [[ 47, 1 ],[ 49, 3 ]]).should == [ paris ]
318
- end
319
-
320
- it "returns the documents within a polygon" do
321
- Bar.where(:location.within(:polygon) => [[ 47, 1 ],[49,1.5],[ 49, 3 ],[46,5]]).should == [ paris ]
322
- end
323
-
324
- it "returns the documents within a center" do
325
- Bar.where(:location.within(:center) => [[ 47, 1 ],4]).should == [ paris ]
326
- end
327
-
328
- it "returns the documents within a center_sphere" do
329
- Bar.where(:location.within(:center_sphere) => [[ 48, 2 ],0.1]).should == [ paris ]
330
- end
331
-
332
- end
333
- context ":circle :center_sphere" do
334
- before do
335
- Bar.delete_all
336
- Bar.create_indexes
337
- end
338
- let!(:mile1) do
339
- Bar.create(:name => 'mile1', :location => [-73.997345, 40.759382])
340
- end
341
-
342
- let!(:mile3) do
343
- Bar.create(:name => 'mile2', :location => [-73.927088, 40.752151])
344
- end
345
-
346
- let!(:mile7) do
347
- Bar.create(:name => 'mile3', :location => [-74.0954913, 40.7161472])
348
- end
349
-
350
- let!(:mile11) do
351
- Bar.create(:name => 'mile4', :location => [-74.0604951, 40.9178011])
352
- end
353
-
354
- it "returns the documents within a center_sphere" do
355
- Bar.where(:location.within(:center_sphere) => {:point => home,:max => 2, :unit => :mi}).should == [ mile1 ]
356
- end
357
-
358
- it "returns the documents within a center_sphere" do
359
- Bar.where(:location.within(:center_sphere) => {:point => home,:max => 4, :unit => :mi}).should include(mile3)
360
- end
361
-
362
- it "returns the documents within a center_sphere" do
363
- Bar.where(:location.within(:center_sphere) => {:point => home,:max => 8, :unit => :mi}).should include(mile7)
364
- end
365
-
366
- it "returns the documents within a center_sphere" do
367
- Bar.where(:location.within(:center_sphere) => {:point => home,:max => 12, :unit => :mi}).should include(mile11)
368
- end
369
- end
370
- end
371
- end
372
-
373
- end
374
-
375
- end
@@ -1,39 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Mongoid::Criterion::NearSpatial do
4
-
5
- let(:within) do
6
- {
7
- :flat => Mongoid::Criterion::WithinSpatial.new(:key => :field, :operator => "near"),
8
- :sphere => Mongoid::Criterion::WithinSpatial.new(:key => :field, :operator => "nearSphere"),
9
- }
10
- end
11
- NEAR = {
12
- :flat =>
13
- {
14
- 'Point' => [[1,2],5],
15
- 'Hash Point' => {:point => [-73.98, 40.77], :max => 5},
16
- 'Hash Point Unit' => {:point => [-73.98, 40.77], :max => 5, :unit => :km}
17
- },
18
- :sphere =>
19
- {
20
- 'Point' => [[1,2],5],
21
- 'Hash Point' => {:point => [-73.98, 40.77], :max => 5},
22
- 'Hash Point Unit' => {:point => [-73.98, 40.77], :max => 5, :unit => :km}
23
- }
24
- }
25
-
26
- context "#to_mongo_query" do
27
-
28
- NEAR.each do |shape, points|
29
- points.each do |input_name,input|
30
- it "#{shape} should generate a query with #{input_name}" do
31
- within[shape].to_mongo_query(input).should be_a_kind_of(Hash)
32
- end
33
- end
34
- end
35
- end
36
-
37
- end
38
-
39
-
@@ -1,54 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Mongoid::Criterion::WithinSpatial do
4
-
5
- let(:within) do
6
- {
7
- :box => Mongoid::Criterion::WithinSpatial.new(:key => :field, :operator => "box"),
8
- :center => Mongoid::Criterion::WithinSpatial.new(:key => :field, :operator => "center"),
9
- :polygon => Mongoid::Criterion::WithinSpatial.new(:key => :field, :operator => "polygon"),
10
- :center_sphere => Mongoid::Criterion::WithinSpatial.new(:key => :field, :operator => "box"),
11
- }
12
- end
13
-
14
- context "#to_mongo_query" do
15
-
16
- {
17
- :box =>
18
- {
19
- 'Array of Arrays' => [[10,20], [15,25]],
20
- 'Array of Hashes' => [{ x: 10, y: 20 }, { x: 15, y: 25 }],
21
- 'Hash of Hashes' => { a: { x: 10, y: 20 }, b: { x: 15, y: 25 }}
22
- },
23
- :polygon =>
24
- {
25
- 'Array of Arrays' => [[10,20], [15,25]],
26
- 'Array of Hashes' => [{ x: 10, y: 20 }, { x: 15, y: 25 }],
27
- 'Hash of Hashes' => { a: { x: 10, y: 20 }, b: { x: 15, y: 25 }}
28
- },
29
- :center =>
30
- {
31
- 'Point' => [[1,2],5],
32
- 'Hash Point' => {:point => [-73.98, 40.77], :max => 5},
33
- 'Hash Point Unit' => {:point => [-73.98, 40.77], :max => 5, :unit => :km}
34
- },
35
- :center_sphere =>
36
- {
37
- 'Point' => [[1,2],5],
38
- 'Hash Point' => {:point => [-73.98, 40.77], :max => 5},
39
- 'Hash Point Unit' => {:point => [-73.98, 40.77], :max => 5, :unit => :km}
40
- }
41
- }.each do |shape, points|
42
-
43
- points.each do |input_name,input|
44
-
45
- it "should generate a #{shape} query with '#{input_name}'" do
46
- within[shape].to_mongo_query(input).should be_a_kind_of(Hash)
47
- end
48
-
49
- end
50
- end
51
-
52
- end # context
53
- end # describe
54
-