mongoid-geospatial 5.0.0 → 5.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -12,6 +12,9 @@ RSpec::Core::RakeTask.new(:rcov) do |spec|
12
12
  spec.rcov = true
13
13
  end
14
14
 
15
- task default: :spec
15
+ require 'rubocop/rake_task'
16
+ RuboCop::RakeTask.new(:rubocop)
17
+
18
+ task default: %i[rubocop spec]
16
19
 
17
20
  require 'yard'
@@ -2,7 +2,7 @@
2
2
  #
3
3
  # Just for fun
4
4
  #
5
- $LOAD_PATH << File.expand_path('../../lib', __FILE__)
5
+ $LOAD_PATH << File.expand_path('../lib', __dir__)
6
6
 
7
7
  require 'mongoid/geospatial'
8
8
 
@@ -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: EARTH_RADIUS_KM * 1000,
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
@@ -5,10 +5,10 @@ module Mongoid
5
5
  class Circle < GeometryField
6
6
  attr_accessor :center, :radius
7
7
 
8
- def point
9
- Point.new(self[0])
8
+ def center
9
+ Point.new(*self[0])
10
10
  end
11
- alias_method :point, :center
11
+ alias point center
12
12
 
13
13
  def radius
14
14
  self[1]
@@ -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
- alias_method :to_a, :mongoize
24
- alias_method :to_xy, :mongoize
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
- alias_method :to_hash, :to_hsh
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
- fail 'Invalid Point'
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
- fail 'Hash must have at least 2 items' if hsh.size < 2
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.lat_symbols & hsh.keys).first
216
+ v = (Mongoid::Geospatial::Config::Point.y & hsh.keys).first
212
217
  return hsh[v].to_f if !v.nil? && hsh[v]
213
- fail "Hash must contain #{Mongoid::Geospatial.lat_symbols.inspect}"
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.lng_symbols & hsh.keys).first
223
+ v = (Mongoid::Geospatial::Config::Point.x & hsh.keys).first
218
224
  return hsh[v].to_f if !v.nil? && hsh[v]
219
- fail "Hash must contain #{Mongoid::Geospatial.lng_symbols.inspect}"
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, min_x = -Float::MAX, Float::MAX
21
- max_y, min_y = -Float::MAX, Float::MAX
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
- alias_method :bbox, :bounding_box
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
- alias_method :center, :center_point
58
+ alias center center_point
57
59
 
58
60
  #
59
61
  # Generates a radius from the point
@@ -1,6 +1,6 @@
1
1
  module Mongoid
2
2
  # Mongoid Geospatial version
3
3
  module Geospatial
4
- VERSION = '5.0.0'
4
+ VERSION = '5.1.0'.freeze
5
5
  end
6
6
  end
@@ -16,6 +16,7 @@ module Mongoid
16
16
  # @return (GeoRuby::SimpleFeatures::Point)
17
17
  def to_geo
18
18
  return unless valid?
19
+
19
20
  GeoRuby::SimpleFeatures::Point.xy(x, y)
20
21
  end
21
22
 
@@ -1,12 +1,11 @@
1
- # -*- encoding: utf-8 -*-
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/nofxx/mongoid-geospatial'
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', ['>= 5.0.0.beta'])
17
+ gem.add_dependency('mongoid', ['>= 4.0.0'])
19
18
  end
@@ -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
- .where(:date.gte => start_date, :date.lte => end_date).group
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
@@ -6,7 +6,7 @@ class Farm
6
6
  field :name, type: String
7
7
  field :geom, type: Point, sphere: true
8
8
  field :area, type: Polygon, spatial: true
9
- field :m2, type: Fixnum
9
+ field :m2, type: Integer
10
10
 
11
11
  spatial_index :geom
12
12
  spatial_index :area
@@ -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
- default: -> { 1.day.ago.in_time_zone('Alaska') }
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, -> { 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
@@ -72,7 +72,7 @@ describe Mongoid::Geospatial do
72
72
  before do
73
73
  Alarm.create_indexes
74
74
  50.times do
75
- Alarm.create(spot: [rand(10) + 1, rand(10) + 1])
75
+ Alarm.create(spot: [rand(1..10), rand(1..10)])
76
76
  end
77
77
  end
78
78