mongoid-geospatial 5.0.0 → 5.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.
- checksums.yaml +5 -5
- data/.rubocop.yml +6 -0
- data/.rubocop_todo.yml +93 -0
- data/.travis.yml +26 -20
- data/CHANGELOG.md +19 -0
- data/CONTRIBUTING.md +118 -0
- data/Dangerfile +1 -0
- data/Gemfile +26 -14
- data/Guardfile +2 -2
- data/MIT-LICENSE +2 -2
- data/README.md +186 -181
- data/RELEASING.md +62 -0
- data/Rakefile +4 -1
- data/bench/bench +1 -1
- data/lib/mongoid/geospatial.rb +6 -4
- data/lib/mongoid/geospatial/config.rb +29 -0
- data/lib/mongoid/geospatial/config/point.rb +19 -0
- data/lib/mongoid/geospatial/fields/circle.rb +3 -3
- data/lib/mongoid/geospatial/fields/point.rb +16 -9
- data/lib/mongoid/geospatial/geometry_field.rb +6 -4
- data/lib/mongoid/geospatial/version.rb +1 -1
- data/lib/mongoid/geospatial/wrappers/georuby.rb +1 -0
- data/mongoid-geospatial.gemspec +3 -4
- data/spec/models/event.rb +1 -1
- data/spec/models/farm.rb +1 -1
- data/spec/models/person.rb +2 -4
- data/spec/mongoid/geospatial/config_spec.rb +22 -0
- data/spec/mongoid/geospatial/fields/point_spec.rb +21 -2
- data/spec/mongoid/geospatial/geospatial_spec.rb +1 -1
- data/spec/mongoid/geospatial/helpers/core_spec.rb +6 -3
- data/spec/mongoid/geospatial/helpers/spatial_spec.rb +3 -4
- data/spec/mongoid/geospatial/helpers/sphere_spec.rb +4 -6
- data/spec/spec_helper.rb +2 -7
- data/spec/support/authentication.rb +0 -1
- metadata +16 -7
data/RELEASING.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
Releasing Mongoid::Geospatial
|
2
|
+
=============================
|
3
|
+
|
4
|
+
There're no particular rules about when to release mongoid-geospatial. Release bug fixes frequently, features not so frequently and breaking API changes rarely.
|
5
|
+
|
6
|
+
### Release
|
7
|
+
|
8
|
+
Run tests, check that all tests succeed locally.
|
9
|
+
|
10
|
+
```
|
11
|
+
bundle install
|
12
|
+
bundle exec rake
|
13
|
+
```
|
14
|
+
|
15
|
+
Check that the last build succeeded in [Travis CI](https://travis-ci.org/mongoid/mongoid-geospatial) for all supported platforms.
|
16
|
+
|
17
|
+
Change "Next Release" in [CHANGELOG.md](CHANGELOG.md) to the new version.
|
18
|
+
|
19
|
+
```
|
20
|
+
### 0.4.0 (2014-01-27)
|
21
|
+
```
|
22
|
+
|
23
|
+
Remove the line with "Your contribution here.", since there will be no more contributions to this release.
|
24
|
+
|
25
|
+
Commit your changes.
|
26
|
+
|
27
|
+
```
|
28
|
+
git add CHANGELOG.md lib/mongoid/geospatial/version.rb
|
29
|
+
git commit -m "Preparing for release, 0.4.0."
|
30
|
+
git push origin master
|
31
|
+
```
|
32
|
+
|
33
|
+
Release.
|
34
|
+
|
35
|
+
```
|
36
|
+
$ rake release
|
37
|
+
|
38
|
+
mongoid-geospatial 0.4.0 built to pkg/mongoid-geospatial-0.4.0.gem.
|
39
|
+
Tagged v0.4.0.
|
40
|
+
Pushed git commits and tags.
|
41
|
+
Pushed mongoid-geospatial 0.4.0 to rubygems.org.
|
42
|
+
```
|
43
|
+
|
44
|
+
### Prepare for the Next Version
|
45
|
+
|
46
|
+
Add the next release to [CHANGELOG.md](CHANGELOG.md).
|
47
|
+
|
48
|
+
```
|
49
|
+
### 0.4.1 (Next)
|
50
|
+
|
51
|
+
* Your contribution here.
|
52
|
+
```
|
53
|
+
|
54
|
+
Increment the minor version, modify [lib/mongoid/geospatial/version.rb](lib/mongoid/geospatial/version.rb).
|
55
|
+
|
56
|
+
Commit your changes.
|
57
|
+
|
58
|
+
```
|
59
|
+
git add CHANGELOG.md lib/mongoid/geospatial/version.rb
|
60
|
+
git commit -m "Preparing for next developer iteration, 0.4.1."
|
61
|
+
git push origin master
|
62
|
+
```
|
data/Rakefile
CHANGED
data/bench/bench
CHANGED
data/lib/mongoid/geospatial.rb
CHANGED
@@ -27,10 +27,10 @@ module Mongoid
|
|
27
27
|
|
28
28
|
# Symbols accepted as 'longitude', 'x'...
|
29
29
|
LNG_SYMBOLS = [:x, :lon, :long, :lng, :longitude,
|
30
|
-
'x', 'lon', 'long', 'lng', 'longitude']
|
30
|
+
'x', 'lon', 'long', 'lng', 'longitude'].freeze
|
31
31
|
|
32
32
|
# Symbols accepted as 'latitude', 'y'...
|
33
|
-
LAT_SYMBOLS = [:y, :lat, :latitude, 'y', 'lat', 'latitude']
|
33
|
+
LAT_SYMBOLS = [:y, :lat, :latitude, 'y', 'lat', 'latitude'].freeze
|
34
34
|
|
35
35
|
# For distance spherical calculations
|
36
36
|
EARTH_RADIUS_KM = 6371 # taken directly from mongodb
|
@@ -38,12 +38,12 @@ module Mongoid
|
|
38
38
|
|
39
39
|
# Earth radius in multiple units
|
40
40
|
EARTH_RADIUS = {
|
41
|
-
m:
|
41
|
+
m: EARTH_RADIUS_KM * 1000,
|
42
42
|
km: EARTH_RADIUS_KM,
|
43
43
|
mi: EARTH_RADIUS_KM * 0.621371192,
|
44
44
|
ft: EARTH_RADIUS_KM * 5280 * 0.621371192,
|
45
45
|
sm: EARTH_RADIUS_KM * 0.53995680345572 # sea mile
|
46
|
-
}
|
46
|
+
}.freeze
|
47
47
|
|
48
48
|
mattr_accessor :lng_symbols
|
49
49
|
mattr_accessor :lat_symbols
|
@@ -120,3 +120,5 @@ module Mongoid
|
|
120
120
|
end
|
121
121
|
end
|
122
122
|
end
|
123
|
+
|
124
|
+
require 'mongoid/geospatial/config'
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Geospatial
|
3
|
+
module Config
|
4
|
+
autoload :Point, 'mongoid/geospatial/config/point'
|
5
|
+
|
6
|
+
extend self
|
7
|
+
|
8
|
+
def reset!
|
9
|
+
Mongoid::Geospatial::Config::Point.reset!
|
10
|
+
end
|
11
|
+
|
12
|
+
def point
|
13
|
+
Mongoid::Geospatial::Config::Point
|
14
|
+
end
|
15
|
+
|
16
|
+
reset!
|
17
|
+
end
|
18
|
+
|
19
|
+
class << self
|
20
|
+
def configure
|
21
|
+
block_given? ? yield(Config) : Config
|
22
|
+
end
|
23
|
+
|
24
|
+
def config
|
25
|
+
Config
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Geospatial
|
3
|
+
module Config
|
4
|
+
module Point
|
5
|
+
extend self
|
6
|
+
|
7
|
+
attr_accessor :x
|
8
|
+
attr_accessor :y
|
9
|
+
|
10
|
+
def reset!
|
11
|
+
self.x = Mongoid::Geospatial.lng_symbols
|
12
|
+
self.y = Mongoid::Geospatial.lat_symbols
|
13
|
+
end
|
14
|
+
|
15
|
+
reset!
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -18,10 +18,11 @@ module Mongoid
|
|
18
18
|
# @return (Array)
|
19
19
|
def mongoize
|
20
20
|
return nil unless x && y
|
21
|
+
|
21
22
|
[x, y]
|
22
23
|
end
|
23
|
-
|
24
|
-
|
24
|
+
alias to_a mongoize
|
25
|
+
alias to_xy mongoize
|
25
26
|
|
26
27
|
def [](args)
|
27
28
|
mongoize[args]
|
@@ -40,7 +41,7 @@ module Mongoid
|
|
40
41
|
def to_hsh(xl = :x, yl = :y)
|
41
42
|
{ xl => x, yl => y }
|
42
43
|
end
|
43
|
-
|
44
|
+
alias to_hash to_hsh
|
44
45
|
|
45
46
|
#
|
46
47
|
# Helper for [self, radius]
|
@@ -144,7 +145,8 @@ module Mongoid
|
|
144
145
|
when NilClass then nil
|
145
146
|
else
|
146
147
|
return obj.to_xy if obj.respond_to?(:to_xy)
|
147
|
-
|
148
|
+
|
149
|
+
raise 'Invalid Point'
|
148
150
|
end
|
149
151
|
end
|
150
152
|
|
@@ -171,6 +173,7 @@ module Mongoid
|
|
171
173
|
#
|
172
174
|
def from_string(str)
|
173
175
|
return nil if str.empty?
|
176
|
+
|
174
177
|
str.split(/,|\s/).reject(&:empty?).map(&:to_f)
|
175
178
|
end
|
176
179
|
|
@@ -185,6 +188,7 @@ module Mongoid
|
|
185
188
|
#
|
186
189
|
def from_array(array)
|
187
190
|
return nil if array.empty?
|
191
|
+
|
188
192
|
array.flatten[0..1].map(&:to_f)
|
189
193
|
end
|
190
194
|
|
@@ -203,20 +207,23 @@ module Mongoid
|
|
203
207
|
# @return (Array)
|
204
208
|
#
|
205
209
|
def from_hash(hsh)
|
206
|
-
|
210
|
+
raise 'Hash must have at least 2 items' if hsh.size < 2
|
211
|
+
|
207
212
|
[from_hash_x(hsh), from_hash_y(hsh)]
|
208
213
|
end
|
209
214
|
|
210
215
|
def from_hash_y(hsh)
|
211
|
-
v = (Mongoid::Geospatial.
|
216
|
+
v = (Mongoid::Geospatial::Config::Point.y & hsh.keys).first
|
212
217
|
return hsh[v].to_f if !v.nil? && hsh[v]
|
213
|
-
|
218
|
+
|
219
|
+
raise "Hash must contain #{Mongoid::Geospatial::Config::Point.y.inspect}"
|
214
220
|
end
|
215
221
|
|
216
222
|
def from_hash_x(hsh)
|
217
|
-
v = (Mongoid::Geospatial.
|
223
|
+
v = (Mongoid::Geospatial::Config::Point.x & hsh.keys).first
|
218
224
|
return hsh[v].to_f if !v.nil? && hsh[v]
|
219
|
-
|
225
|
+
|
226
|
+
raise "Hash must contain #{Mongoid::Geospatial::Config::Point.x.inspect}"
|
220
227
|
end
|
221
228
|
end # << self
|
222
229
|
end # Point
|
@@ -17,8 +17,10 @@ module Mongoid
|
|
17
17
|
# @return [Array] containing 2 points
|
18
18
|
#
|
19
19
|
def bounding_box
|
20
|
-
max_x
|
21
|
-
|
20
|
+
max_x = -Float::MAX
|
21
|
+
min_x = Float::MAX
|
22
|
+
max_y = -Float::MAX
|
23
|
+
min_y = Float::MAX
|
22
24
|
each do |point|
|
23
25
|
max_y = point[1] if point[1] > max_y
|
24
26
|
min_y = point[1] if point[1] < min_y
|
@@ -27,7 +29,7 @@ module Mongoid
|
|
27
29
|
end
|
28
30
|
[[min_x, min_y], [max_x, max_y]]
|
29
31
|
end
|
30
|
-
|
32
|
+
alias bbox bounding_box
|
31
33
|
|
32
34
|
#
|
33
35
|
# Determines the 5 points geometry bounding box.
|
@@ -53,7 +55,7 @@ module Mongoid
|
|
53
55
|
min, max = *bbox
|
54
56
|
[(min[0] + max[0]) / 2.0, (min[1] + max[1]) / 2.0]
|
55
57
|
end
|
56
|
-
|
58
|
+
alias center center_point
|
57
59
|
|
58
60
|
#
|
59
61
|
# Generates a radius from the point
|
data/mongoid-geospatial.gemspec
CHANGED
@@ -1,12 +1,11 @@
|
|
1
|
-
|
2
|
-
require File.expand_path('../lib/mongoid/geospatial/version', __FILE__)
|
1
|
+
require File.expand_path('lib/mongoid/geospatial/version', __dir__)
|
3
2
|
|
4
3
|
Gem::Specification.new do |gem|
|
5
4
|
gem.authors = ['Ryan Ong', 'Marcos Piccinini']
|
6
5
|
gem.email = ['use@git.hub.com']
|
7
6
|
gem.summary = 'Mongoid Extension that simplifies MongoDB Geospatial Operations.'
|
8
7
|
gem.description = 'Mongoid Extension that simplifies MongoDB casting and operations on spatial Ruby objects.'
|
9
|
-
gem.homepage = 'https://github.com/
|
8
|
+
gem.homepage = 'https://github.com/mongoid/mongoid-geospatial'
|
10
9
|
|
11
10
|
gem.files = `git ls-files`.split("\n")
|
12
11
|
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
@@ -15,5 +14,5 @@ Gem::Specification.new do |gem|
|
|
15
14
|
gem.version = Mongoid::Geospatial::VERSION
|
16
15
|
gem.license = 'MIT'
|
17
16
|
|
18
|
-
gem.add_dependency('mongoid', ['>=
|
17
|
+
gem.add_dependency('mongoid', ['>= 4.0.0'])
|
19
18
|
end
|
data/spec/models/event.rb
CHANGED
@@ -10,7 +10,7 @@ class Event
|
|
10
10
|
|
11
11
|
def self.each_day(start_date, end_date)
|
12
12
|
groups = only(:date).asc(:date)
|
13
|
-
|
13
|
+
.where(:date.gte => start_date, :date.lte => end_date).group
|
14
14
|
groups.each do |hash|
|
15
15
|
yield(hash['date'], hash['group'])
|
16
16
|
end
|
data/spec/models/farm.rb
CHANGED
data/spec/models/person.rb
CHANGED
@@ -27,7 +27,7 @@ class Person
|
|
27
27
|
field :security_code
|
28
28
|
field :blood_alcohol_content, type: Float, default: -> { 0.0 }
|
29
29
|
field :last_drink_taken_at, type: Date,
|
30
|
-
|
30
|
+
default: -> { 1.day.ago.in_time_zone('Alaska') }
|
31
31
|
|
32
32
|
# Geo
|
33
33
|
field :location, type: Point
|
@@ -58,15 +58,13 @@ class Person
|
|
58
58
|
accepts_nested_attributes_for :addresses
|
59
59
|
|
60
60
|
scope :minor, -> { where(:age.lt => 18) }
|
61
|
-
scope :without_ssn, -> {
|
61
|
+
scope :without_ssn, -> { without(:ssn) }
|
62
62
|
|
63
63
|
def score_with_rescoring=(score)
|
64
64
|
@rescored = score.to_i + 20
|
65
65
|
self.score_without_rescoring = score
|
66
66
|
end
|
67
67
|
|
68
|
-
alias_method_chain :score=, :rescoring
|
69
|
-
|
70
68
|
def update_addresses
|
71
69
|
addresses.each do |address|
|
72
70
|
address.street = 'Updated Address'
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::Geospatial::Config do
|
4
|
+
context 'point' do
|
5
|
+
it 'uses LonLat by default' do
|
6
|
+
expect(Mongoid::Geospatial.config.point.x).to eq Mongoid::Geospatial.lng_symbols
|
7
|
+
expect(Mongoid::Geospatial.config.point.y).to eq Mongoid::Geospatial.lat_symbols
|
8
|
+
end
|
9
|
+
context 'configured as latlon' do
|
10
|
+
before do
|
11
|
+
Mongoid::Geospatial.configure do |config|
|
12
|
+
config.point.x = Mongoid::Geospatial.lat_symbols
|
13
|
+
config.point.y = Mongoid::Geospatial.lng_symbols
|
14
|
+
end
|
15
|
+
end
|
16
|
+
it 'uses latlon' do
|
17
|
+
expect(Mongoid::Geospatial.config.point.x).to eq Mongoid::Geospatial.lat_symbols
|
18
|
+
expect(Mongoid::Geospatial.config.point.y).to eq Mongoid::Geospatial.lng_symbols
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -33,6 +33,24 @@ describe Mongoid::Geospatial::Point do
|
|
33
33
|
bar.location = '2.99 , 3.99'
|
34
34
|
expect(bar.location.mongoize).to eq([2.99, 3.99])
|
35
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
|
36
54
|
end
|
37
55
|
|
38
56
|
it 'should have #reverse to get lat, lon' do
|
@@ -83,7 +101,7 @@ describe Mongoid::Geospatial::Point do
|
|
83
101
|
end
|
84
102
|
|
85
103
|
it 'should have an ActiveModel symbol accessor' do
|
86
|
-
expect(bar[:location]).to eq([3, 2])
|
104
|
+
expect(bar[:location].to_a).to eq([3, 2])
|
87
105
|
end
|
88
106
|
|
89
107
|
it 'should have a radius helper' do
|
@@ -198,7 +216,8 @@ describe Mongoid::Geospatial::Point do
|
|
198
216
|
it 'returns the documents within a box' do
|
199
217
|
poly = Mongoid::Geospatial::LineString.new(
|
200
218
|
[elvis.location.map { |c| c + 1 },
|
201
|
-
elvis.location.map { |c| c - 1 }]
|
219
|
+
elvis.location.map { |c| c - 1 }]
|
220
|
+
)
|
202
221
|
expect(Bar.where(:location.within_polygon => [poly.geom_box]).to_a)
|
203
222
|
.to include(mile3)
|
204
223
|
end
|