mongoid-geospatial 7.0.0 → 7.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +109 -33
  3. data/lib/mongoid/geospatial/config/point.rb +5 -2
  4. data/lib/mongoid/geospatial/config.rb +2 -0
  5. data/lib/mongoid/geospatial/ext/rgeo_spherical_point_impl.rb +2 -0
  6. data/lib/mongoid/geospatial/fields/box.rb +2 -0
  7. data/lib/mongoid/geospatial/fields/circle.rb +2 -2
  8. data/lib/mongoid/geospatial/fields/line_string.rb +2 -0
  9. data/lib/mongoid/geospatial/fields/point.rb +59 -50
  10. data/lib/mongoid/geospatial/fields/polygon.rb +2 -0
  11. data/lib/mongoid/geospatial/geometry_field.rb +4 -2
  12. data/lib/mongoid/geospatial/helpers/delegate.rb +2 -0
  13. data/lib/mongoid/geospatial/helpers/geom.rb +22 -0
  14. data/lib/mongoid/geospatial/helpers/spatial.rb +2 -0
  15. data/lib/mongoid/geospatial/helpers/sphere.rb +2 -0
  16. data/lib/mongoid/geospatial/keys.rb +20 -0
  17. data/lib/mongoid/geospatial/version.rb +3 -1
  18. data/lib/mongoid/geospatial/wrappers/georuby.rb +2 -0
  19. data/lib/mongoid/geospatial/wrappers/rgeo.rb +2 -0
  20. data/lib/mongoid/geospatial.rb +196 -16
  21. metadata +7 -75
  22. data/.coveralls.yml +0 -1
  23. data/.gitignore +0 -49
  24. data/.hound.yml +0 -2
  25. data/.rspec +0 -3
  26. data/.rubocop.yml +0 -8
  27. data/.travis.yml +0 -33
  28. data/CHANGELOG.md +0 -23
  29. data/CONTRIBUTING.md +0 -118
  30. data/Gemfile +0 -17
  31. data/Guardfile +0 -16
  32. data/RELEASING.md +0 -62
  33. data/Rakefile +0 -20
  34. data/mongoid-geospatial.gemspec +0 -18
  35. data/spec/bench +0 -64
  36. data/spec/models/address.rb +0 -69
  37. data/spec/models/alarm.rb +0 -12
  38. data/spec/models/bar.rb +0 -14
  39. data/spec/models/bus.rb +0 -12
  40. data/spec/models/event.rb +0 -18
  41. data/spec/models/farm.rb +0 -13
  42. data/spec/models/person.rb +0 -91
  43. data/spec/models/phone.rb +0 -8
  44. data/spec/models/place.rb +0 -13
  45. data/spec/models/river.rb +0 -22
  46. data/spec/mongoid/geospatial/config_spec.rb +0 -22
  47. data/spec/mongoid/geospatial/fields/box_spec.rb +0 -8
  48. data/spec/mongoid/geospatial/fields/circle_spec.rb +0 -8
  49. data/spec/mongoid/geospatial/fields/line_string_spec.rb +0 -78
  50. data/spec/mongoid/geospatial/fields/point_spec.rb +0 -296
  51. data/spec/mongoid/geospatial/fields/polygon_spec.rb +0 -87
  52. data/spec/mongoid/geospatial/geospatial_spec.rb +0 -222
  53. data/spec/mongoid/geospatial/helpers/core_spec.rb +0 -35
  54. data/spec/mongoid/geospatial/helpers/delegate_spec.rb +0 -67
  55. data/spec/mongoid/geospatial/helpers/spatial_spec.rb +0 -40
  56. data/spec/mongoid/geospatial/helpers/sphere_spec.rb +0 -31
  57. data/spec/mongoid/geospatial/wrappers/georuby_spec.rb +0 -63
  58. data/spec/mongoid/geospatial/wrappers/rgeo_spec.rb +0 -115
  59. data/spec/mongoid.yml +0 -298
  60. data/spec/spec_helper.rb +0 -42
  61. data/spec/support/authentication.rb +0 -28
  62. data/spec/support/mongod.conf +0 -3
  63. data/spec/support/mongoid.yml +0 -19
@@ -1,296 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mongoid::Geospatial::Point do
4
- describe "Moe's Bar" do
5
- let(:bar) { Bar.create!(name: "Moe's") }
6
-
7
- it 'should not interfer with mongoid' do
8
- expect(bar.class.count).to eql(1)
9
- end
10
-
11
- it 'should not fail if point is nil' do
12
- expect(bar.location).to be_nil
13
- end
14
-
15
- it 'should set point methodically' do
16
- bar.location = Mongoid::Geospatial::Point.new(8, 9)
17
- expect(bar.save).to be_truthy
18
- expect(Bar.first.location.x).to eq(8)
19
- expect(Bar.first.location.y).to eq(9)
20
- end
21
-
22
- it 'should set point with comma separated text' do
23
- bar.location = '2.99,3.99'
24
- expect(bar.location.mongoize).to eq([2.99, 3.99])
25
- end
26
-
27
- it 'should set point with space separated text' do
28
- bar.location = '2.99 3.99'
29
- expect(bar.location.mongoize).to eq([2.99, 3.99])
30
- end
31
-
32
- it 'should set point with space comma separated text' do
33
- bar.location = '2.99 , 3.99'
34
- expect(bar.location.mongoize).to eq([2.99, 3.99])
35
- end
36
-
37
- it 'should set point from hash' do
38
- bar.location = { latitude: 2.99, longitude: 3.99 }
39
- expect(bar.location.mongoize).to eq([3.99, 2.99])
40
- end
41
-
42
- context 'configured as latlon' do
43
- before do
44
- Mongoid::Geospatial.configure do |config|
45
- config.point.x = Mongoid::Geospatial.lat_symbols
46
- config.point.y = Mongoid::Geospatial.lng_symbols
47
- end
48
- end
49
- it 'should set point from hash' do
50
- bar.location = { latitude: 2.99, longitude: 3.99 }
51
- expect(bar.location.mongoize).to eq([2.99, 3.99])
52
- end
53
- end
54
- end
55
-
56
- it "should have a to_s method" do
57
- bar = Bar.create!(name: "Moe's", location: [1, 2])
58
- expect(bar.location.to_s).to eq('1.0, 2.0')
59
- end
60
-
61
- it "should have a to_s method" do
62
- bar = Bar.create!(name: "Moe's", location: [1.0009, 21.009])
63
- expect(bar.location.to_s).to eq('1.0009, 21.009')
64
- end
65
-
66
- it "should have a to_geo_json method" do
67
- bar = Bar.create!(name: "Moe's", location: [1.0009, 21.009])
68
- expect(bar.location.to_geo_json).to eq({
69
- type: "Point", coordinates: [1.0009, 21.009]
70
- })
71
- end
72
-
73
- it "should have a to_json method" do
74
- bar = Bar.create!(name: "Moe's", location: [1.0009, 21.009])
75
- expect(bar.location.to_json).to eq("[1.0009,21.009]")
76
- end
77
-
78
- it 'should have #reverse to get lat, lon' do
79
- bar = Bar.create!(name: "Moe's", location: [1, 2])
80
- expect(bar.location.reverse).to eq([2, 1])
81
- end
82
-
83
- it 'should set point to nil' do
84
- bar = Bar.create!(name: "Moe's", location: [1, 1])
85
- bar.location = nil
86
- expect(bar.location).to be_nil
87
- expect(bar.save).to be_truthy
88
- expect(Bar.where(location: nil).first).to eq(bar)
89
- end
90
-
91
- it 'should update point x' do
92
- bar = Bar.create!(name: "Moe's", location: [1, 1])
93
- bar.location = [2, 3]
94
- expect(bar.save).to be_truthy
95
- expect(Bar.first.location.to_a).to eq([2, 3])
96
- end
97
-
98
- it 'should set point empty string to nil' do
99
- bar = Bar.create!(name: "Moe's", location: [1, 1])
100
- bar.location = ''
101
- expect(bar.location).to be_nil
102
- expect(bar.save).to be_truthy
103
- expect(Bar.where(location: nil).first).to eq(bar)
104
- end
105
-
106
- it 'should set point empty array to nil' do
107
- bar = Bar.create!(name: "Moe's", location: [1, 1])
108
- bar.location = []
109
- expect(bar.location).to be_nil
110
- expect(bar.save).to be_truthy
111
- expect(Bar.where(location: nil).first).to eq(bar)
112
- end
113
-
114
- describe 'methods' do
115
- let(:bar) { Bar.create!(location: [3, 2]) }
116
-
117
- it 'should have a .to_a' do
118
- expect(bar.location.to_a[0..1]).to eq([3.0, 2.0])
119
- end
120
-
121
- it 'should have an array [] accessor' do
122
- expect(bar.location[0]).to eq(3.0)
123
- end
124
-
125
- it 'should have an ActiveModel symbol accessor' do
126
- expect(bar[:location].to_a).to eq([3, 2])
127
- end
128
-
129
- it 'should have a radius helper' do
130
- expect(bar.location.radius).to eql([[3.0, 2.0], 1])
131
- end
132
-
133
- it 'should have a radius sphere helper' do
134
- expect(bar.location.radius_sphere[1])
135
- .to be_within(0.0001).of(0.00015)
136
- end
137
-
138
- it 'should have a radius sphere helper in meters' do
139
- expect(bar.location.radius_sphere(1000, :m)[1])
140
- .to be_within(0.0001).of(0.00015)
141
- end
142
-
143
- it 'should have a radius sphere helper in miles' do
144
- expect(bar.location.radius_sphere(1, :mi)[1])
145
- .to be_within(0.0001).of(0.00025)
146
- end
147
- end
148
-
149
- describe 'queryable' do
150
- before do
151
- Bar.create_indexes
152
- end
153
-
154
- describe ':near :near_sphere' do
155
- let!(:berlin) do
156
- Bar.create(name: :berlin, location: [52.30, 13.25])
157
- end
158
-
159
- let!(:prague) do
160
- Bar.create(name: :prague, location: [50.5, 14.26])
161
- end
162
-
163
- let!(:paris) do
164
- Bar.create(name: :paris, location: [48.48, 2.20])
165
- end
166
-
167
- let!(:jim) do
168
- Person.new(location: [41.23, 2.9])
169
- end
170
-
171
- it 'returns the documents sorted closest to furthest' do
172
- expect(Bar.closest_to_location(jim.location).to_a)
173
- .to eq([paris, prague, berlin])
174
- end
175
-
176
- it 'returns the documents sorted closest to furthest' do
177
- expect(Bar.where(:location.near => jim.location).to_a)
178
- .to eq([paris, prague, berlin])
179
- end
180
-
181
- it 'returns the documents sorted closest to furthest' do
182
- expect(Bar.near(location: jim.location))
183
- .to eq([paris, prague, berlin])
184
- end
185
-
186
- it 'returns the documents sorted closest to furthest sphere' do
187
- person = Person.new(location: [41.23, 2.9])
188
- expect(Bar.near_sphere(location: person.location))
189
- .to eq([paris, prague, berlin])
190
- end
191
-
192
- it 'returns the documents sorted closest to furthest sphere' do
193
- person = Person.new(location: [41.23, 2.9])
194
- expect(Bar.where(:location.near_sphere => person.location))
195
- .to eq([paris, prague, berlin])
196
- end
197
-
198
- it 'returns the documents sorted closest to furthest with max' do
199
- expect(Bar.near(location: jim.location).max_distance(location: 10).to_a)
200
- .to eq([paris]) # , prague, berlin ]
201
- end
202
- end
203
-
204
- describe ':within_circle :within_spherical_circle' do
205
- let!(:mile1) do
206
- Bar.create(name: 'mile1', location: [-73.997345, 40.759382])
207
- end
208
-
209
- let!(:mile3) do
210
- Bar.create(name: 'mile3', location: [-73.927088, 40.752151])
211
- end
212
-
213
- let!(:mile7) do
214
- Bar.create(name: 'mile7', location: [-74.0954913, 40.7161472])
215
- end
216
-
217
- let!(:mile9) do
218
- Bar.create(name: 'mile9', location: [-74.0604951, 40.9178011])
219
- end
220
-
221
- let!(:elvis) do
222
- Person.new(location: [-73.98, 40.75])
223
- end
224
-
225
- it 'returns the documents within a circle' do
226
- pending 'Circle'
227
- l = [elvis.location, 500.0 / Mongoid::Geospatial::EARTH_RADIUS_KM]
228
- expect(Bar.where(:location.within_circle => l).to_a).to include(mile3)
229
- end
230
-
231
- it 'returns the documents within a spherical circle' do
232
- pending 'Circle'
233
- expect(Bar.where(:location.within_spherical_circle =>
234
- [elvis.location, 0.0005]).to_a).to eq([mile1])
235
- end
236
-
237
- it 'returns the documents within a center circle' do
238
- pending 'Circle'
239
- expect(Bar.where(:location.within_center_circle =>
240
- [elvis.location, 0.0005]).to_a).to eq([mile1])
241
- end
242
-
243
- it 'returns the documents within a box' do
244
- poly = Mongoid::Geospatial::LineString.new(
245
- [elvis.location.map { |c| c + 1 },
246
- elvis.location.map { |c| c - 1 }]
247
- )
248
- expect(Bar.where(:location.within_polygon => [poly.geom_box]).to_a)
249
- .to include(mile3)
250
- end
251
- end
252
- end
253
-
254
- describe '(de)mongoize' do
255
- it 'should mongoize array' do
256
- bar = Bar.new(location: [10, -9])
257
- expect(bar.location.class).to eql(Mongoid::Geospatial::Point)
258
- expect(bar.location.x).to be_within(0.1).of(10)
259
- expect(bar.location.y).to be_within(0.1).of(-9)
260
- end
261
-
262
- it 'should mongoize hash' do
263
- geom = Bar.new(location: { x: 10, y: -9 }).location
264
- expect(geom.class).to eql(Mongoid::Geospatial::Point)
265
- expect(geom.x).to be_within(0.1).of(10)
266
- expect(geom.y).to be_within(0.1).of(-9)
267
- end
268
-
269
- it 'should mongoize hash with symbols in any order' do
270
- geom = Bar.new(location: { y: -9, x: 10 }).location
271
- expect(geom.class).to eql(Mongoid::Geospatial::Point)
272
- expect(geom.x).to be_within(0.1).of(10)
273
- expect(geom.y).to be_within(0.1).of(-9)
274
- end
275
-
276
- it 'should mongoize hash with string keys in any order' do
277
- geom = Bar.new(location: { 'y' => -9, 'x' => 10 }).location
278
- expect(geom.class).to eql(Mongoid::Geospatial::Point)
279
- expect(geom.x).to be_within(0.1).of(10)
280
- expect(geom.y).to be_within(0.1).of(-9)
281
- end
282
-
283
- # should raise
284
- # geom.to_geo
285
-
286
- describe 'with rgeo' do
287
- describe 'instantiated' do
288
- let(:bar) { Bar.new(name: 'Vitinho', location: [10, 10]) }
289
-
290
- it 'should demongoize to rgeo' do
291
- expect(bar.location.class).to eql(Mongoid::Geospatial::Point)
292
- end
293
- end
294
- end
295
- end
296
- end
@@ -1,87 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mongoid::Geospatial::Polygon do
4
- it 'should have correct indexes on farm' do
5
- Farm.create_indexes
6
- expect(Farm.collection.indexes.get(geom: '2dsphere')).not_to be_nil
7
- end
8
-
9
- describe '(de)mongoize' do
10
- it 'should support a field mapped as polygon' do
11
- farm = Farm.new(area: [[5, 5], [6, 5], [6, 6], [5, 6]])
12
- expect(farm.area).to be_a Mongoid::Geospatial::Polygon
13
- expect(farm.area).to eq([[5, 5], [6, 5], [6, 6], [5, 6]])
14
- end
15
-
16
- it 'should store as array on mongo' do
17
- Farm.create(area: [[5, 5], [6, 5], [6, 6], [5, 6]])
18
- expect(Farm.first.area).to eq([[5, 5], [6, 5], [6, 6], [5, 6]])
19
- end
20
-
21
- it 'should have a bounding box' do
22
- geom = Mongoid::Geospatial::Polygon.new [[1, 5], [6, 5], [6, 6], [5, 6]]
23
- expect(geom.bbox).to eq([[1, 5], [6, 6]])
24
- end
25
-
26
- it 'should have a center point' do
27
- geom = Mongoid::Geospatial::Polygon.new [[1, 1], [1, 1], [9, 9], [9, 9]]
28
- expect(geom.center).to eq([5.0, 5.0])
29
- end
30
-
31
- it 'should have a radius helper' do
32
- geom = Mongoid::Geospatial::Polygon.new [[1, 1], [1, 1], [9, 9], [9, 9]]
33
- expect(geom.radius(10)).to eq([[5.0, 5.0], 10])
34
- end
35
-
36
- it 'should have a radius sphere' do
37
- geom = Mongoid::Geospatial::Polygon.new [[1, 1], [1, 1], [9, 9], [9, 9]]
38
- expect(geom.radius_sphere(10)[1]).to be_within(0.001).of(0.001569)
39
- end
40
-
41
- describe 'with rgeo' do
42
- # farm.area.should be_a RGeo::Geographic::SphericalPolygonImpl
43
- end
44
- end
45
-
46
- describe 'query' do
47
- context ':box, :polygon' do
48
- before do
49
- Farm.create_indexes
50
- end
51
-
52
- let(:ranch) do
53
- Farm.create!(name: 'Ranch',
54
- area: [[1, 1], [3, 3], [3, 1], [1, 1]],
55
- geom: [2, 2])
56
- end
57
-
58
- let(:farm) do
59
- Farm.create!(name: 'Farm',
60
- area: [[47, 1], [48, 1.5], [49, 3], [49, 1], [47, 1]],
61
- geom: [48, 1.28])
62
- end
63
-
64
- it 'returns the documents within a box' do
65
- query = Farm.geo_spatial(:geom.within_polygon => [ranch.area])
66
- expect(query.to_a).to eq([ranch])
67
- end
68
-
69
- it 'returns the documents within a polygon' do
70
- query = Farm.where(:geom.within_polygon => [farm.area])
71
- expect(query.to_a).to eq([farm])
72
- end
73
-
74
- # it 'returns the documents within a center' do
75
- # pending 'Moped'
76
- # expect(Farm.where(:geom.within_circle =>
77
- # [ranch.geom, 0.4]).first).to eq(ranch)
78
- # end
79
-
80
- # it 'returns the documents within a center_sphere' do
81
- # pending 'Moped'
82
- # expect(Farm.where(:geom.within_spherical_circle =>
83
- # [ranch.geom, 0.1]).first).to eq(ranch)
84
- # end
85
- end
86
- end
87
- end
@@ -1,222 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mongoid::Geospatial do
4
- context 'Class Stuff' do
5
- it 'should have an lng_symbols accessor' do
6
- expect(Mongoid::Geospatial.lng_symbols).to be_instance_of Array
7
- expect(Mongoid::Geospatial.lng_symbols).to include :x
8
- end
9
-
10
- it 'should have an lat_symbols accessor' do
11
- expect(Mongoid::Geospatial.lat_symbols).to be_instance_of Array
12
- expect(Mongoid::Geospatial.lat_symbols).to include :y
13
- end
14
- end
15
-
16
- context 'Creating indexes' do
17
- it 'should create a 2d index' do
18
- Bar.create_indexes
19
- expect(Bar.collection.indexes.get(location: '2d')).not_to be_nil
20
- end
21
-
22
- it 'should create a 2dsphere index' do
23
- Alarm.create_indexes
24
- expect(Alarm.collection.indexes.get(spot: '2dsphere')).not_to be_nil
25
- end
26
- end
27
-
28
- context '#nearby 2d' do
29
- before do
30
- Bar.create_indexes
31
- end
32
- after do
33
- Bar.collection.indexes.drop_all
34
- end
35
-
36
- let!(:moes) do
37
- Bar.create!(name: "Moe's", location: [-73.77694444, 40.63861111])
38
- end
39
-
40
- let!(:rose) do
41
- Bar.create!(name: 'Rosa', location: [-118.40, 33.94])
42
- end
43
-
44
- let!(:jane) do
45
- Bar.create!(name: "Jane's", location: [1, 1])
46
- end
47
-
48
- let!(:foo) do
49
- Bar.create!(name: "Foo", location: [3, 3])
50
- end
51
-
52
- it 'should work specifing center and different location foo' do
53
- expect(Bar.nearby(foo.location)).to be_a Mongoid::Criteria
54
- expect(Bar.nearby(foo.location).selector).to eq({"location" => {"$near" => [3.0, 3.0]}})
55
- end
56
-
57
- it 'should work specifing center and different location moes' do
58
- expect(Bar.nearby(moes.location).limit(2)).to eq([moes, rose])
59
- end
60
-
61
- it 'should work finding first' do
62
- expect(Bar.nearby(moes.location).first).to eq(moes)
63
- end
64
-
65
- it 'really should work find first nearby' do
66
- expect(Bar.count).to eq(4)
67
- expect(Bar.nearby([1, 1]).to_a).to eq([jane, foo, moes, rose])
68
- expect(Bar.nearby([2, 2]).to_a.first).to eq(jane)
69
- # THIS WILL FAIL MONGOID ISSUE
70
- # expect(Bar.nearby(rose.location).first).to eq(rose)
71
- end
72
-
73
- it 'should work specifing first' do
74
- bars = Bar.nearby(rose.location).to_a
75
- expect(bars.first).to eq(rose)
76
- end
77
-
78
- it 'should work specifing first' do
79
- expect(Bar.nearby(rose.location).to_a.first).to eq(rose)
80
- end
81
-
82
- it 'should work specifing first' do
83
- expect(Bar.nearby(rose.location).to_a.first).to eq(rose)
84
- end
85
-
86
- it 'should work specifing first' do
87
- expect(Bar.nearby(rose.location).first).to eq(rose)
88
- end
89
-
90
- it 'returns the documents sorted closest to furthest' do
91
- expect(Bar.closest_to_location(rose.location).to_a)
92
- .to eq([rose, moes, jane, foo])
93
- end
94
-
95
- it 'returns the documents sorted closest to furthest' do
96
- expect(Bar.closest_to_location(rose.location).limit(2))
97
- .to eq([rose, moes])
98
- end
99
-
100
- it 'returns the documents sorted closest to furthest' do
101
- expect(Bar.closest_to_location(rose.location).first).to eq(rose)
102
- end
103
-
104
- it 'returns the documents sorted closest to furthest' do
105
- expect(Bar.closest_to_location(rose.location).to_a)
106
- .to eq([rose, moes, jane, foo])
107
- end
108
-
109
- it 'should work specifing center and different location foo' do
110
- expect(Bar.closest_to_location(foo.location)).to be_a Mongoid::Criteria
111
- expect(Bar.closest_to_location(foo.location).selector).to eq({"location" => {"$near" => [3.0, 3.0]}})
112
- end
113
-
114
- end
115
-
116
- context '#nearby 2dsphere' do
117
- before do
118
- Alarm.create_indexes
119
- end
120
- after do
121
- Alarm.collection.indexes.drop_all
122
- end
123
- let!(:jfk) do
124
- Alarm.create(name: 'jfk', spot: [-73.77694444, 40.63861111])
125
- end
126
-
127
- let!(:lax) do
128
- Alarm.create(name: 'lax', spot: [-118.40, 33.94])
129
- end
130
-
131
- it 'should work with specific center and different spot attribute' do
132
- expect(Alarm.nearby(lax.spot)).to eq([lax, jfk])
133
- end
134
-
135
- it 'should work with default origin' do
136
- expect(Alarm.near_sphere(spot: lax.spot)).to eq([lax, jfk])
137
- end
138
-
139
- it 'should work with default origin key' do
140
- expect(Alarm.where(:spot.near_sphere => lax.spot)).to eq([lax, jfk])
141
- end
142
-
143
- context ':paginate' do
144
- before do
145
- Alarm.create_indexes
146
- 50.times do
147
- Alarm.create(spot: [rand(1..10), rand(1..10)])
148
- end
149
- end
150
-
151
- it 'limits fine with 25' do
152
- expect(Alarm.near_sphere(spot: [5, 5])
153
- .limit(25).to_a.size).to eq 25
154
- end
155
-
156
- it 'limits fine with 25 and skips' do
157
- expect(Alarm.near_sphere(spot: [5, 5])
158
- .skip(25).limit(25).to_a.size).to eq 25
159
- end
160
-
161
- it 'paginates 50' do
162
- page1 = Alarm.near_sphere(spot: [5, 5]).limit(25)
163
- page2 = Alarm.near_sphere(spot: [5, 5]).skip(25).limit(25)
164
- expect((page1 + page2).uniq.size).to eq(50)
165
- end
166
- end
167
-
168
- context ':query' do
169
- before do
170
- Alarm.create_indexes
171
- 3.times do
172
- Alarm.create(spot: [jfk.spot.x + rand(0), jfk.spot.y + rand(0)])
173
- end
174
- end
175
-
176
- it 'should filter using extra query option' do
177
- query = Alarm.near_sphere(spot: jfk.spot).where(name: jfk.name)
178
- expect(query.to_a).to eq [jfk]
179
- end
180
- end
181
-
182
- context ':maxDistance' do
183
- it 'should get 1 item' do
184
- spot = 2465 / Mongoid::Geospatial.earth_radius[:mi]
185
- query = Alarm.near_sphere(spot: lax.spot).max_distance(spot: spot)
186
- expect(query.to_a.size).to eq 1
187
- end
188
- end
189
-
190
- # context ':distance_multiplier' do
191
- # it "should multiply returned distance with multiplier" do
192
- # Bar.geo_near(lax.location,
193
- # ::distance_multiplier=> Mongoid::Geospatial.earth_radius[:mi])
194
- # .second.geo[:distance].to_i.should be_within(1).of(2469)
195
- # end
196
- # end
197
-
198
- # context ':unit' do
199
- # it "should multiply returned distance with multiplier" do
200
- # Bar.geo_near(lax.location, :spherical => true, :unit => :mi)
201
- # .second.geo[:distance].to_i.should be_within(1).of(2469)
202
- # end
203
-
204
- # it "should convert max_distance to radians with unit" do
205
- # Bar.geo_near(lax.location, :spherical => true,
206
- # :max_distance => 2465, :unit => :mi).size.should == 1
207
- # end
208
-
209
- # end
210
-
211
- # end
212
-
213
- # context 'criteria chaining' do
214
- # it "should filter by where" do
215
- # Bar.where(:name => jfk.name).geo_near(jfk.location).should == [jfk]
216
- # Bar.any_of({:name => jfk.name},{:name => lax.name})
217
- # .geo_near(jfk.location).should == [jfk,lax]
218
- # end
219
- # end
220
- # end
221
- end
222
- end
@@ -1,35 +0,0 @@
1
- module Mongoid
2
- module Geospatial
3
- def self.from_array(ary)
4
- ary[0..1].map(&:to_f)
5
- end
6
-
7
- def self.from_hash(hsh)
8
- raise 'Hash must have at least 2 items' if hsh.size < 2
9
-
10
- [from_hash_x(hsh), from_hash_y(hsh)]
11
- end
12
-
13
- def self.from_hash_y(hsh)
14
- v = (Mongoid::Geospatial.lat_symbols & hsh.keys).first
15
- return hsh[v].to_f if !v.nil? && hsh[v]
16
-
17
- if Mongoid::Geospatial.lng_symbols.index(hsh.keys[1])
18
- raise "Hash cannot contain #{Mongoid::Geospatial.lng_symbols.inspect} "\
19
- "as second arg without #{Mongoid::Geospatial.lat_symbols.inspect}"
20
- end
21
- hsh.values[1].to_f
22
- end
23
-
24
- def self.from_hash_x(hsh)
25
- v = (Mongoid::Geospatial.lng_symbols & hsh.keys).first
26
- return hsh[v].to_f if !v.nil? && hsh[v]
27
-
28
- if Mongoid::Geospatial.lat_symbols.index(keys[0])
29
- raise "Hash cannot contain #{Mongoid::Geospatial.lat_symbols.inspect} "\
30
- "as first arg without #{Mongoid::Geospatial.lng_symbols.inspect}"
31
- end
32
- values[0].to_f
33
- end
34
- end
35
- end
@@ -1,67 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mongoid::Fields do
4
- context 'delegate' do
5
- before do
6
- Bus.create_indexes
7
- end
8
-
9
- context 'x, y helpers' do
10
- let(:bus) { Bus.create!(name: 'Far', location: [7, 8]) }
11
-
12
- it 'should set instance method x' do
13
- expect(bus.x).to eq(7)
14
- end
15
-
16
- it 'should set instance method y' do
17
- expect(bus.y).to eq(8)
18
- end
19
-
20
- it 'should set instance method x=' do
21
- bus.x = 9
22
- expect(bus.x).to eq(9)
23
- end
24
-
25
- it 'should set instance method y=' do
26
- bus.y = 9
27
- expect(bus.y).to eq(9)
28
- end
29
- end
30
-
31
- it 'should set instance methods x= and y=' do
32
- bus = Bus.create!(name: 'B', location: [7, 7])
33
- bus.x = 8
34
- bus.y = 9
35
- expect(bus.location.to_a).to eq([8, 9])
36
- end
37
-
38
- it 'should work fine with default values' do
39
- event = Event.create!(name: 'Bvent')
40
- event.x = 8
41
- event.y = 9
42
- expect(event.location.to_a).to eq([8, 9])
43
- end
44
-
45
- it 'should not work fine with nils' do
46
- bus = Bus.create!(name: 'B', location: nil)
47
- expect do
48
- bus.x = 9
49
- bus.y = 9
50
- end.to raise_error(NoMethodError)
51
- end
52
-
53
- it 'should update point x' do
54
- bus = Bus.create!(name: '0789', location: [1, 1])
55
- bus.x = 2
56
- expect(bus.save).to be_truthy
57
- expect(Bus.first.location.to_a).to eq([2, 1])
58
- end
59
-
60
- it 'should update point y' do
61
- bus = Bus.create!(name: '0987', location: [1, 1])
62
- bus.y = 2
63
- expect(bus.save).to be_truthy
64
- expect(Bus.first.location.to_a).to eq([1.0, 2.0])
65
- end
66
- end
67
- end