mongoid_spacial 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -2
- data/README.md +7 -6
- data/VERSION +1 -1
- data/lib/mongoid_spacial/contexts/mongo.rb +11 -9
- data/lib/mongoid_spacial/spacial.rb +4 -1
- data/lib/mongoid_spacial/spacial/geo_near.rb +15 -13
- data/mongoid_spacial.gemspec +5 -5
- data/spec/functional/mongoid/contexts/mongo_spec.rb +56 -27
- metadata +9 -9
data/Gemfile
CHANGED
@@ -6,11 +6,11 @@ source "http://rubygems.org"
|
|
6
6
|
# Add dependencies to develop your gem here.
|
7
7
|
# Include everything needed to run rake, tests, features, etc.
|
8
8
|
gem 'mongoid'
|
9
|
-
gem "bson", '>= 1.3', :platforms => [:jruby] # for non jruby apps, require bson_ext in your Gemfile to boost performance
|
10
|
-
gem "bson_ext", '>= 1.3', :platforms => [:mri]
|
11
9
|
gem 'activesupport'
|
12
10
|
|
13
11
|
group :development do
|
12
|
+
gem "bson", '>= 1.3', :platforms => [:jruby] # for non jruby apps, require bson_ext in your Gemfile to boost performance
|
13
|
+
gem "bson_ext", '>= 1.3', :platforms => [:mri]
|
14
14
|
gem "rspec", "~> 2.3.0"
|
15
15
|
gem "yard", "~> 0.6.0"
|
16
16
|
gem "bundler", "~> 1.0.0"
|
@@ -25,4 +25,6 @@ end
|
|
25
25
|
|
26
26
|
group :test do
|
27
27
|
gem 'mocha'
|
28
|
+
gem 'will_paginate'
|
29
|
+
gem 'kaminari'
|
28
30
|
end
|
data/README.md
CHANGED
@@ -99,14 +99,15 @@ River.where(:name=>'hudson').geo_near({:lat => 40.73083, :lng => -73.99756})
|
|
99
99
|
# :max\_distance - Integer
|
100
100
|
# :distance\_multiplier - Integer
|
101
101
|
# :spherical - true - To enable spherical calculations
|
102
|
-
|
103
|
-
|
102
|
+
# :page - pagination will be enabled if set to any variable including nil, pagination will not be enabled if either :per\_page or :paginator is set
|
103
|
+
# :per\_page
|
104
|
+
# :paginator - Choose which paginator to use. [default :arrary]
|
105
|
+
# Prefered method to set is Mongoid::Spacial.paginator=:array
|
106
|
+
# Available Paginators [:kaminari, :will\_paginate, :array]
|
107
|
+
# The only thing this does really is configure default per\_page so it is only kind of useful
|
108
|
+
River.geo_near([-73.99756,40.73083], :max_distance => 4, :unit => :mi, :spherical => true, :page => 1)
|
104
109
|
```
|
105
110
|
|
106
|
-
ToDo
|
107
|
-
-----------
|
108
|
-
1. Add pagination
|
109
|
-
|
110
111
|
Thanks
|
111
112
|
-----------
|
112
113
|
* Thanks to Kristian Mandrup for creating the base of the gem and a few of the tests
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
@@ -39,17 +39,19 @@ module Mongoid #:nodoc:
|
|
39
39
|
if distance_multiplier = Mongoid::Spacial.earth_radius[opts.delete(:unit)]
|
40
40
|
opts[:distance_multiplier] = distance_multiplier
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
|
+
# setup paging.
|
43
44
|
if opts.has_key?(:page)
|
44
45
|
opts[:page] ||= 1
|
45
|
-
opts[:paginator] ||= Mongoid::Spacial.paginator
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
46
|
+
opts[:paginator] ||= Mongoid::Spacial.paginator()
|
47
|
+
|
48
|
+
if opts[:paginator] == :will_paginate
|
49
|
+
opts[:per_page] ||= klass.per_page
|
50
|
+
elsif opts[:paginator] == :kaminari
|
51
|
+
opts[:per_page] ||= Kaminari.config.default_per_page
|
52
|
+
else
|
53
|
+
opts[:per_page] ||= Mongoid::Spacial.default_per_page
|
54
|
+
end
|
53
55
|
end
|
54
56
|
query = create_geo_near_query(center,opts)
|
55
57
|
results = klass.db.command(query)
|
@@ -5,7 +5,9 @@ module Mongoid
|
|
5
5
|
|
6
6
|
def initialize(klass,results,opts = {})
|
7
7
|
raise "class must include Mongoid::Spacial::Document" unless klass.respond_to?(:spacial_fields_indexed)
|
8
|
-
@klass
|
8
|
+
@klass = klass
|
9
|
+
@opts = opts
|
10
|
+
@stats = results['stats']
|
9
11
|
|
10
12
|
@_original_array = results['results'].collect do |result|
|
11
13
|
res = Mongoid::Factory.from_db(klass, result.delete('obj'))
|
@@ -18,29 +20,29 @@ module Mongoid
|
|
18
20
|
res.geo[key.snakecase.to_sym] = value
|
19
21
|
end
|
20
22
|
# dist_options[:formula] = opts[:formula] if opts[:formula]
|
21
|
-
opts[:calculate] = klass.spacial_fields_indexed if klass.spacial_fields_indexed.kind_of?(Array) && opts[:calculate] == true
|
22
|
-
if opts[:calculate]
|
23
|
-
opts[:calculate] = [opts[:calculate]] unless opts[:calculate].kind_of? Array
|
24
|
-
opts[:calculate] = opts[:calculate].map(&:to_sym) & geo_fields
|
23
|
+
@opts[:calculate] = klass.spacial_fields_indexed if klass.spacial_fields_indexed.kind_of?(Array) && @opts[:calculate] == true
|
24
|
+
if @opts[:calculate]
|
25
|
+
@opts[:calculate] = [@opts[:calculate]] unless @opts[:calculate].kind_of? Array
|
26
|
+
@opts[:calculate] = @opts[:calculate].map(&:to_sym) & geo_fields
|
25
27
|
if klass.spacial_fields_indexed.kind_of?(Array) && klass.spacial_fields_indexed.size == 1
|
26
28
|
primary = klass.spacial_fields_indexed.first
|
27
29
|
end
|
28
|
-
opts[:calculate].each do |key|
|
30
|
+
@opts[:calculate].each do |key|
|
29
31
|
key = (key.to_s+'_distance').to_sym
|
30
|
-
res.geo[key] = res.distance_from(key,center, opts[:distance_multiplier])
|
32
|
+
res.geo[key] = res.distance_from(key,center, @opts[:distance_multiplier])
|
31
33
|
res.geo[:distance] = res.geo[key] if primary && key == primary
|
32
34
|
end
|
33
35
|
end
|
34
36
|
res
|
35
37
|
end
|
36
38
|
|
37
|
-
if opts[:page]
|
38
|
-
start = (opts[:page]-1)
|
39
|
-
super(@_original_array[start, opts[:per_page]])
|
40
|
-
elsif opts[:skip] && @_original_array.size > opts[:skip]
|
41
|
-
super(@_original_array[opts[:skip]..-1])
|
39
|
+
if @opts[:page]
|
40
|
+
start = (@opts[:page]-1)*@opts[:per_page] # assuming current_page is 1 based.
|
41
|
+
super(@_original_array[start, @opts[:per_page]] || [])
|
42
|
+
elsif @opts[:skip] && @_original_array.size > @opts[:skip]
|
43
|
+
super(@_original_array[@opts[:skip]..-1] || [])
|
42
44
|
else
|
43
|
-
super(@_original_array)
|
45
|
+
super(@_original_array || [])
|
44
46
|
end
|
45
47
|
end
|
46
48
|
|
data/mongoid_spacial.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid_spacial}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = [%q{Ryan Ong}]
|
@@ -134,9 +134,9 @@ Gem::Specification.new do |s|
|
|
134
134
|
|
135
135
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
136
136
|
s.add_runtime_dependency(%q<mongoid>, [">= 0"])
|
137
|
-
s.add_runtime_dependency(%q<bson>, [">= 1.3"])
|
138
|
-
s.add_runtime_dependency(%q<bson_ext>, [">= 1.3"])
|
139
137
|
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
138
|
+
s.add_development_dependency(%q<bson>, [">= 1.3"])
|
139
|
+
s.add_development_dependency(%q<bson_ext>, [">= 1.3"])
|
140
140
|
s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
|
141
141
|
s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
|
142
142
|
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
@@ -146,9 +146,9 @@ Gem::Specification.new do |s|
|
|
146
146
|
s.add_development_dependency(%q<ruby-debug19>, [">= 0"])
|
147
147
|
else
|
148
148
|
s.add_dependency(%q<mongoid>, [">= 0"])
|
149
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
149
150
|
s.add_dependency(%q<bson>, [">= 1.3"])
|
150
151
|
s.add_dependency(%q<bson_ext>, [">= 1.3"])
|
151
|
-
s.add_dependency(%q<activesupport>, [">= 0"])
|
152
152
|
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
153
153
|
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
154
154
|
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
@@ -159,9 +159,9 @@ Gem::Specification.new do |s|
|
|
159
159
|
end
|
160
160
|
else
|
161
161
|
s.add_dependency(%q<mongoid>, [">= 0"])
|
162
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
162
163
|
s.add_dependency(%q<bson>, [">= 1.3"])
|
163
164
|
s.add_dependency(%q<bson_ext>, [">= 1.3"])
|
164
|
-
s.add_dependency(%q<activesupport>, [">= 0"])
|
165
165
|
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
166
166
|
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
167
167
|
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
@@ -20,48 +20,50 @@ describe Mongoid::Contexts::Mongo do
|
|
20
20
|
Bar.geo_near(lax.location, :spherical => true).should == [lax, jfk]
|
21
21
|
Bar.geo_near(jfk.location, :spherical => true).should == [jfk, lax]
|
22
22
|
end
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
context 'option' do
|
24
|
+
context ':num' do
|
25
|
+
it "should limit number of results to 1" do
|
26
|
+
Bar.geo_near(jfk.location, :num => 1).size.should == 1
|
27
|
+
end
|
27
28
|
end
|
28
|
-
end
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
30
|
+
context ':maxDistance' do
|
31
|
+
it "should get 1 item" do
|
32
|
+
Bar.geo_near(lax.location, :spherical => true, :max_distance => 2465/Mongoid::Spacial.earth_radius[:mi]).size.should == 1
|
33
|
+
end
|
34
|
+
it "should get 2 items" do
|
35
|
+
Bar.geo_near(lax.location, :spherical => true, :max_distance => 2480/Mongoid::Spacial.earth_radius[:mi]).size.should == 2
|
36
|
+
end
|
37
37
|
|
38
|
-
|
38
|
+
end
|
39
39
|
|
40
|
-
|
41
|
-
|
40
|
+
context ':distance_multiplier' do
|
41
|
+
it "should multiply returned distance with multiplier" do
|
42
42
|
Bar.geo_near(lax.location, :spherical => true, :distance_multiplier=> Mongoid::Spacial.earth_radius[:mi]).second.geo[:distance].to_i.should be_within(1).of(2469)
|
43
|
+
end
|
43
44
|
end
|
44
|
-
end
|
45
45
|
|
46
|
-
|
47
|
-
|
46
|
+
context ':unit' do
|
47
|
+
it "should multiply returned distance with multiplier" do
|
48
48
|
Bar.geo_near(lax.location, :spherical => true, :unit => :mi).second.geo[:distance].to_i.should be_within(1).of(2469)
|
49
|
-
|
49
|
+
end
|
50
50
|
|
51
|
-
|
51
|
+
it "should convert max_distance to radians with unit" do
|
52
52
|
Bar.geo_near(lax.location, :spherical => true, :max_distance => 2465, :unit => :mi).size.should == 1
|
53
|
-
|
53
|
+
end
|
54
54
|
|
55
|
-
|
55
|
+
end
|
56
56
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
57
|
+
context ':query' do
|
58
|
+
it "should filter using extra query option" do
|
59
|
+
# two record in the collection, only one's name is Munich
|
60
|
+
Bar.geo_near(jfk.location, :query => {:name => jfk.name}).should == [jfk]
|
61
|
+
end
|
61
62
|
end
|
63
|
+
|
62
64
|
end
|
63
65
|
|
64
|
-
|
66
|
+
context 'criteria chaining' do
|
65
67
|
it "should filter by where" do
|
66
68
|
Bar.where(:name => jfk.name).geo_near(jfk.location).should == [jfk]
|
67
69
|
Bar.any_of({:name => jfk.name},{:name => lax.name}).geo_near(jfk.location).should == [jfk,lax]
|
@@ -78,5 +80,32 @@ describe Mongoid::Contexts::Mongo do
|
|
78
80
|
end
|
79
81
|
|
80
82
|
end
|
83
|
+
|
84
|
+
context ':page' do
|
85
|
+
before(:all) do
|
86
|
+
Bar.delete_all
|
87
|
+
Bar.create_indexes
|
88
|
+
|
89
|
+
100.times do
|
90
|
+
Bar.create({:location => [rand(360)-180,rand(360)-180]})
|
91
|
+
end
|
92
|
+
end
|
93
|
+
context ":paginator :array" do
|
94
|
+
[nil,1,2,3,4].each do |page|
|
95
|
+
it "page=#{page} should have 25" do
|
96
|
+
Bar.geo_near([1,1], :page => page).size.should == 25
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
it "page=20 should have 0" do
|
101
|
+
Bar.geo_near([1,1], :page => 20).size.should == 0
|
102
|
+
end
|
103
|
+
|
104
|
+
it "per_page=5" do
|
105
|
+
Bar.geo_near([1,1], :page => 1, :per_page => 5).size.should == 5
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
81
110
|
end
|
82
111
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: mongoid_spacial
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Ryan Ong
|
@@ -24,36 +24,36 @@ dependencies:
|
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: *id001
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
|
-
name:
|
27
|
+
name: activesupport
|
28
28
|
requirement: &id002 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: "
|
33
|
+
version: "0"
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: *id002
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
|
-
name:
|
38
|
+
name: bson
|
39
39
|
requirement: &id003 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ">="
|
43
43
|
- !ruby/object:Gem::Version
|
44
44
|
version: "1.3"
|
45
|
-
type: :
|
45
|
+
type: :development
|
46
46
|
prerelease: false
|
47
47
|
version_requirements: *id003
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
|
-
name:
|
49
|
+
name: bson_ext
|
50
50
|
requirement: &id004 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ">="
|
54
54
|
- !ruby/object:Gem::Version
|
55
|
-
version: "
|
56
|
-
type: :
|
55
|
+
version: "1.3"
|
56
|
+
type: :development
|
57
57
|
prerelease: false
|
58
58
|
version_requirements: *id004
|
59
59
|
- !ruby/object:Gem::Dependency
|
@@ -261,7 +261,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
261
261
|
requirements:
|
262
262
|
- - ">="
|
263
263
|
- !ruby/object:Gem::Version
|
264
|
-
hash:
|
264
|
+
hash: 3825965172872748297
|
265
265
|
segments:
|
266
266
|
- 0
|
267
267
|
version: "0"
|