mongoid-geospatial 5.1.0 → 7.1.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 (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +81 -65
  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 +28 -13
  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/spatial.rb +2 -0
  14. data/lib/mongoid/geospatial/helpers/sphere.rb +3 -1
  15. data/lib/mongoid/geospatial/version.rb +3 -1
  16. data/lib/mongoid/geospatial/wrappers/georuby.rb +2 -0
  17. data/lib/mongoid/geospatial/wrappers/rgeo.rb +2 -0
  18. data/lib/mongoid/geospatial.rb +234 -27
  19. metadata +7 -80
  20. data/.coveralls.yml +0 -1
  21. data/.gitignore +0 -49
  22. data/.hound.yml +0 -2
  23. data/.rspec +0 -3
  24. data/.rubocop.yml +0 -6
  25. data/.rubocop_todo.yml +0 -93
  26. data/.travis.yml +0 -33
  27. data/CHANGELOG.md +0 -19
  28. data/CONTRIBUTING.md +0 -118
  29. data/Dangerfile +0 -1
  30. data/Gemfile +0 -37
  31. data/Guardfile +0 -16
  32. data/RELEASING.md +0 -62
  33. data/Rakefile +0 -20
  34. data/bench/bench +0 -64
  35. data/mongoid-geospatial.gemspec +0 -18
  36. data/spec/models/address.rb +0 -69
  37. data/spec/models/alarm.rb +0 -12
  38. data/spec/models/bar.rb +0 -13
  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 -96
  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 -79
  50. data/spec/mongoid/geospatial/fields/point_spec.rb +0 -269
  51. data/spec/mongoid/geospatial/fields/polygon_spec.rb +0 -87
  52. data/spec/mongoid/geospatial/geospatial_spec.rb +0 -150
  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 -39
  56. data/spec/mongoid/geospatial/helpers/sphere_spec.rb +0 -29
  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/spec_helper.rb +0 -47
  60. data/spec/support/authentication.rb +0 -28
  61. data/spec/support/mongod.conf +0 -3
  62. data/spec/support/mongoid.yml +0 -19
@@ -1,79 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mongoid::Geospatial::LineString do
4
- describe '(de)mongoize' do
5
- it 'should support a field mapped as linestring' do
6
- river = River.new(course: [[5, 5], [6, 5], [6, 6], [5, 6]])
7
- expect(river.course).to be_a Mongoid::Geospatial::LineString
8
- expect(river.course).to eq([[5, 5], [6, 5], [6, 6], [5, 6]])
9
- end
10
-
11
- it 'should update line string too' do
12
- river = River.create!(name: 'Amazonas')
13
- river.course = [[1, 1], [1, 1], [9, 9], [9, 9]]
14
- river.save
15
- expect(River.first.course).to eq(river.course)
16
- end
17
-
18
- it 'should line_string += point nicely' do
19
- river = River.create!(name: 'Amazonas', course: [[1, 1], [9, 9]])
20
- river.course += [[10, 10]]
21
- river.save
22
- expect(River.first.course).to eq([[1, 1], [9, 9], [10, 10]])
23
- end
24
-
25
- it 'should parent.line_string << point nicely' do
26
- pending 'Mongoid Issue #...'
27
- river = River.create!(name: 'Amazonas', course: [[1, 1], [9, 9]])
28
- river.course.push [10, 10]
29
- river.save
30
- expect(River.first.course).to eq([[1, 1], [9, 9], [10, 10]])
31
- end
32
-
33
- it 'should have same obj id' do
34
- pending 'Mongoid Issue #...'
35
- river = River.create!(name: 'Amazonas', course: [[1, 1], [9, 9]])
36
- expect(river.course.object_id).to eq(river.course.object_id)
37
- end
38
-
39
- it 'should have same obj id ary' do
40
- river = River.create!(name: 'Amazonas', mouth_array: [[1, 1], [9, 9]])
41
- expect(river.mouth_array.object_id).to eq(river.mouth_array.object_id)
42
- end
43
-
44
- it 'should support a field mapped as linestring' do
45
- River.create!(course: [[5, 5], [6, 5], [6, 6], [5, 6]])
46
- expect(River.first.course).to eq([[5, 5], [6, 5], [6, 6], [5, 6]])
47
- end
48
-
49
- it 'should have a bounding box' do
50
- l = Mongoid::Geospatial::LineString.new [[1, 5], [6, 5], [6, 6], [5, 6]]
51
- expect(l.bbox).to eq([[1, 5], [6, 6]])
52
- end
53
-
54
- it 'should have a geom box' do
55
- l = Mongoid::Geospatial::LineString.new [[1, 1], [5, 5]]
56
- expect(l.geom_box).to eq([[1, 1], [1, 5], [5, 5], [5, 1], [1, 1]])
57
- end
58
-
59
- it 'should have a geom box' do
60
- l = Mongoid::Geospatial::LineString.new [[1, 1], [2, 2], [3, 4], [5, 5]]
61
- expect(l.geom_box).to eq([[1, 1], [1, 5], [5, 5], [5, 1], [1, 1]])
62
- end
63
-
64
- it 'should have a center point' do
65
- l = Mongoid::Geospatial::LineString.new [[1, 1], [1, 1], [9, 9], [9, 9]]
66
- expect(l.center).to eq([5.0, 5.0])
67
- end
68
-
69
- it 'should have a radius helper' do
70
- l = Mongoid::Geospatial::LineString.new [[1, 1], [1, 1], [9, 9], [9, 9]]
71
- expect(l.radius(10)).to eq([[5.0, 5.0], 10])
72
- end
73
-
74
- it 'should have a radius sphere' do
75
- l = Mongoid::Geospatial::LineString.new [[1, 1], [1, 1], [9, 9], [9, 9]]
76
- expect(l.radius_sphere(10)[1]).to be_within(0.001).of(0.001569)
77
- end
78
- end
79
- end
@@ -1,269 +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 #reverse to get lat, lon' do
57
- bar = Bar.create!(name: "Moe's", location: [1, 2])
58
- expect(bar.location.reverse).to eq([2, 1])
59
- end
60
-
61
- it 'should set point to nil' do
62
- bar = Bar.create!(name: "Moe's", location: [1, 1])
63
- bar.location = nil
64
- expect(bar.location).to be_nil
65
- expect(bar.save).to be_truthy
66
- expect(Bar.where(location: nil).first).to eq(bar)
67
- end
68
-
69
- it 'should update point x' do
70
- bar = Bar.create!(name: "Moe's", location: [1, 1])
71
- bar.location = [2, 3]
72
- expect(bar.save).to be_truthy
73
- expect(Bar.first.location.to_a).to eq([2, 3])
74
- end
75
-
76
- it 'should set point empty string to nil' do
77
- bar = Bar.create!(name: "Moe's", location: [1, 1])
78
- bar.location = ''
79
- expect(bar.location).to be_nil
80
- expect(bar.save).to be_truthy
81
- expect(Bar.where(location: nil).first).to eq(bar)
82
- end
83
-
84
- it 'should set point empty array to nil' do
85
- bar = Bar.create!(name: "Moe's", location: [1, 1])
86
- bar.location = []
87
- expect(bar.location).to be_nil
88
- expect(bar.save).to be_truthy
89
- expect(Bar.where(location: nil).first).to eq(bar)
90
- end
91
-
92
- describe 'methods' do
93
- let(:bar) { Bar.create!(location: [3, 2]) }
94
-
95
- it 'should have a .to_a' do
96
- expect(bar.location.to_a[0..1]).to eq([3.0, 2.0])
97
- end
98
-
99
- it 'should have an array [] accessor' do
100
- expect(bar.location[0]).to eq(3.0)
101
- end
102
-
103
- it 'should have an ActiveModel symbol accessor' do
104
- expect(bar[:location].to_a).to eq([3, 2])
105
- end
106
-
107
- it 'should have a radius helper' do
108
- expect(bar.location.radius).to eql([[3.0, 2.0], 1])
109
- end
110
-
111
- it 'should have a radius sphere helper' do
112
- expect(bar.location.radius_sphere[1])
113
- .to be_within(0.0001).of(0.00015)
114
- end
115
-
116
- it 'should have a radius sphere helper in meters' do
117
- expect(bar.location.radius_sphere(1000, :m)[1])
118
- .to be_within(0.0001).of(0.00015)
119
- end
120
-
121
- it 'should have a radius sphere helper in miles' do
122
- expect(bar.location.radius_sphere(1, :mi)[1])
123
- .to be_within(0.0001).of(0.00025)
124
- end
125
- end
126
-
127
- describe 'queryable' do
128
- before do
129
- Bar.create_indexes
130
- end
131
-
132
- describe ':near :near_sphere' do
133
- let!(:berlin) do
134
- Bar.create(name: :berlin, location: [52.30, 13.25])
135
- end
136
-
137
- let!(:prague) do
138
- Bar.create(name: :prague, location: [50.5, 14.26])
139
- end
140
-
141
- let!(:paris) do
142
- Bar.create(name: :paris, location: [48.48, 2.20])
143
- end
144
-
145
- let!(:jim) do
146
- Person.new(location: [41.23, 2.9])
147
- end
148
-
149
- it 'returns the documents sorted closest to furthest' do
150
- expect(Bar.where(:location.near => jim.location))
151
- .to eq([paris, prague, berlin])
152
- end
153
-
154
- it 'returns the documents sorted closest to furthest' do
155
- expect(Bar.near(location: jim.location))
156
- .to eq([paris, prague, berlin])
157
- end
158
-
159
- it 'returns the documents sorted closest to furthest sphere' do
160
- person = Person.new(location: [41.23, 2.9])
161
- expect(Bar.near_sphere(location: person.location))
162
- .to eq([paris, prague, berlin])
163
- end
164
-
165
- it 'returns the documents sorted closest to furthest sphere' do
166
- person = Person.new(location: [41.23, 2.9])
167
- expect(Bar.where(:location.near_sphere => person.location))
168
- .to eq([paris, prague, berlin])
169
- end
170
-
171
- it 'returns the documents sorted closest to furthest with max' do
172
- expect(Bar.near(location: jim.location).max_distance(location: 10).to_a)
173
- .to eq([paris]) # , prague, berlin ]
174
- end
175
- end
176
-
177
- describe ':within_circle :within_spherical_circle' do
178
- let!(:mile1) do
179
- Bar.create(name: 'mile1', location: [-73.997345, 40.759382])
180
- end
181
-
182
- let!(:mile3) do
183
- Bar.create(name: 'mile3', location: [-73.927088, 40.752151])
184
- end
185
-
186
- let!(:mile7) do
187
- Bar.create(name: 'mile7', location: [-74.0954913, 40.7161472])
188
- end
189
-
190
- let!(:mile9) do
191
- Bar.create(name: 'mile9', location: [-74.0604951, 40.9178011])
192
- end
193
-
194
- let!(:elvis) do
195
- Person.new(location: [-73.98, 40.75])
196
- end
197
-
198
- it 'returns the documents within a circle' do
199
- pending 'Circle'
200
- l = [elvis.location, 500.0 / Mongoid::Geospatial::EARTH_RADIUS_KM]
201
- expect(Bar.where(:location.within_circle => l).to_a).to include(mile3)
202
- end
203
-
204
- it 'returns the documents within a spherical circle' do
205
- pending 'Circle'
206
- expect(Bar.where(:location.within_spherical_circle =>
207
- [elvis.location, 0.0005]).to_a).to eq([mile1])
208
- end
209
-
210
- it 'returns the documents within a center circle' do
211
- pending 'Circle'
212
- expect(Bar.where(:location.within_center_circle =>
213
- [elvis.location, 0.0005]).to_a).to eq([mile1])
214
- end
215
-
216
- it 'returns the documents within a box' do
217
- poly = Mongoid::Geospatial::LineString.new(
218
- [elvis.location.map { |c| c + 1 },
219
- elvis.location.map { |c| c - 1 }]
220
- )
221
- expect(Bar.where(:location.within_polygon => [poly.geom_box]).to_a)
222
- .to include(mile3)
223
- end
224
- end
225
- end
226
-
227
- describe '(de)mongoize' do
228
- it 'should mongoize array' do
229
- bar = Bar.new(location: [10, -9])
230
- expect(bar.location.class).to eql(Mongoid::Geospatial::Point)
231
- expect(bar.location.x).to be_within(0.1).of(10)
232
- expect(bar.location.y).to be_within(0.1).of(-9)
233
- end
234
-
235
- it 'should mongoize hash' do
236
- geom = Bar.new(location: { x: 10, y: -9 }).location
237
- expect(geom.class).to eql(Mongoid::Geospatial::Point)
238
- expect(geom.x).to be_within(0.1).of(10)
239
- expect(geom.y).to be_within(0.1).of(-9)
240
- end
241
-
242
- it 'should mongoize hash with symbols in any order' do
243
- geom = Bar.new(location: { y: -9, x: 10 }).location
244
- expect(geom.class).to eql(Mongoid::Geospatial::Point)
245
- expect(geom.x).to be_within(0.1).of(10)
246
- expect(geom.y).to be_within(0.1).of(-9)
247
- end
248
-
249
- it 'should mongoize hash with string keys in any order' do
250
- geom = Bar.new(location: { 'y' => -9, 'x' => 10 }).location
251
- expect(geom.class).to eql(Mongoid::Geospatial::Point)
252
- expect(geom.x).to be_within(0.1).of(10)
253
- expect(geom.y).to be_within(0.1).of(-9)
254
- end
255
-
256
- # should raise
257
- # geom.to_geo
258
-
259
- describe 'with rgeo' do
260
- describe 'instantiated' do
261
- let(:bar) { Bar.create!(name: 'Vitinho', location: [10, 10]) }
262
-
263
- it 'should demongoize to rgeo' do
264
- expect(bar.location.class).to eql(Mongoid::Geospatial::Point)
265
- end
266
- end
267
- end
268
- end
269
- 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_spacial(: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,150 +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
-
33
- let!(:jfk) do
34
- Bar.create(name: 'jfk', location: [-73.77694444, 40.63861111])
35
- end
36
-
37
- let!(:lax) do
38
- Bar.create(name: 'lax', location: [-118.40, 33.94])
39
- end
40
-
41
- it 'should work specifing center and different location' do
42
- expect(Bar.nearby(lax.location)).to eq([lax, jfk])
43
- end
44
- end
45
-
46
- context '#nearby 2dsphere' do
47
- before do
48
- Alarm.create_indexes
49
- end
50
-
51
- let!(:jfk) do
52
- Alarm.create(name: 'jfk', spot: [-73.77694444, 40.63861111])
53
- end
54
-
55
- let!(:lax) do
56
- Alarm.create(name: 'lax', spot: [-118.40, 33.94])
57
- end
58
-
59
- it 'should work with specific center and different spot attribute' do
60
- expect(Alarm.nearby(lax.spot)).to eq([lax, jfk])
61
- end
62
-
63
- it 'should work with default origin' do
64
- expect(Alarm.near_sphere(spot: lax.spot)).to eq([lax, jfk])
65
- end
66
-
67
- it 'should work with default origin key' do
68
- expect(Alarm.where(:spot.near_sphere => lax.spot)).to eq([lax, jfk])
69
- end
70
-
71
- context ':paginate' do
72
- before do
73
- Alarm.create_indexes
74
- 50.times do
75
- Alarm.create(spot: [rand(1..10), rand(1..10)])
76
- end
77
- end
78
-
79
- it 'limits fine with 25' do
80
- expect(Alarm.near_sphere(spot: [5, 5])
81
- .limit(25).to_a.size).to eq 25
82
- end
83
-
84
- it 'limits fine with 25 and skips' do
85
- expect(Alarm.near_sphere(spot: [5, 5])
86
- .skip(25).limit(25).to_a.size).to eq 25
87
- end
88
-
89
- it 'paginates 50' do
90
- page1 = Alarm.near_sphere(spot: [5, 5]).limit(25)
91
- page2 = Alarm.near_sphere(spot: [5, 5]).skip(25).limit(25)
92
- expect((page1 + page2).uniq.size).to eq(50)
93
- end
94
- end
95
-
96
- context ':query' do
97
- before do
98
- Alarm.create_indexes
99
- 3.times do
100
- Alarm.create(spot: [jfk.spot.x + rand(0), jfk.spot.y + rand(0)])
101
- end
102
- end
103
-
104
- it 'should filter using extra query option' do
105
- query = Alarm.near_sphere(spot: jfk.spot).where(name: jfk.name)
106
- expect(query.to_a).to eq [jfk]
107
- end
108
- end
109
-
110
- context ':maxDistance' do
111
- it 'should get 1 item' do
112
- spot = 2465 / Mongoid::Geospatial.earth_radius[:mi]
113
- query = Alarm.near_sphere(spot: lax.spot).max_distance(spot: spot)
114
- expect(query.to_a.size).to eq 1
115
- end
116
- end
117
-
118
- # context ':distance_multiplier' do
119
- # it "should multiply returned distance with multiplier" do
120
- # Bar.geo_near(lax.location,
121
- # ::distance_multiplier=> Mongoid::Geospatial.earth_radius[:mi])
122
- # .second.geo[:distance].to_i.should be_within(1).of(2469)
123
- # end
124
- # end
125
-
126
- # context ':unit' do
127
- # it "should multiply returned distance with multiplier" do
128
- # Bar.geo_near(lax.location, :spherical => true, :unit => :mi)
129
- # .second.geo[:distance].to_i.should be_within(1).of(2469)
130
- # end
131
-
132
- # it "should convert max_distance to radians with unit" do
133
- # Bar.geo_near(lax.location, :spherical => true,
134
- # :max_distance => 2465, :unit => :mi).size.should == 1
135
- # end
136
-
137
- # end
138
-
139
- # end
140
-
141
- # context 'criteria chaining' do
142
- # it "should filter by where" do
143
- # Bar.where(:name => jfk.name).geo_near(jfk.location).should == [jfk]
144
- # Bar.any_of({:name => jfk.name},{:name => lax.name})
145
- # .geo_near(jfk.location).should == [jfk,lax]
146
- # end
147
- # end
148
- # end
149
- end
150
- 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