mongoid_geo 0.2.1.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.textile +9 -1
  2. data/lib/mongoid/geo/geo_near.rb +38 -10
  3. metadata +11 -11
data/README.textile CHANGED
@@ -138,9 +138,17 @@ end
138
138
  nearest_positions = Position.geoNear(another_address.location, :pos)
139
139
 
140
140
  # perform distance locations in Speherical mode inside Mongo DB (default is :plane)
141
- nearest_positions = Position.geoNear(another_address.location, :pos, :sphere)
141
+ nearest_positions = Position.geoNear(another_address.location, :pos, :mode => :sphere)
142
+
143
+ # other options supported are: :num, :maxDistance, distanceMultiplier, :query
144
+
142
145
  </pre>
143
146
 
147
+ # if you need to operate on the Mongoid models referenced by the query result, simply call #to_models on it
148
+ # Fx to get the city of the first model instance from the query result:
149
+
150
+ @nearest_city = Position.geoNear(another_address.location, :pos).to_models.first.city@
151
+
144
152
  h2. Mongoid Geo extra inclusions
145
153
 
146
154
  Find addresses near a point using spherical distance calculation
@@ -5,22 +5,48 @@ require 'mongoid/geo/haversine'
5
5
 
6
6
  module Mongoid
7
7
  module Geo
8
+ module Model
9
+ def to_model
10
+ clazz.where(:_id => _id).first
11
+ end
12
+ end
13
+
14
+ module Models
15
+ def to_models
16
+ map do |m|
17
+ m.to_model
18
+ end
19
+ end
20
+ end
21
+
8
22
  module Near
9
- def geoNear(center, location_attribute, mode = :plane)
23
+ def geoNear(center, location_attribute, options = {})
10
24
  center = center.respond_to?(:collection) ? center.send(location_attribute) : center
11
- query = create_query(self, center, mode)
12
- create_result(query_result(self, query, center, location_attribute))
25
+ query = create_query(self, center, options)
26
+ create_result(query_result(self, query, center, location_attribute)).extend(Mongoid::Geo::Models)
13
27
  end
14
28
 
15
29
  protected
16
30
 
17
- def create_query clazz, center, mode = :plane
18
- BSON::OrderedHash.new.tap do |query|
19
- query["geoNear"] = clazz.to_s.tableize
20
- query["near"] = center
31
+ def create_query clazz, center, options = {}
32
+ num = options[:num]
33
+ maxDistance = options[:maxDistance]
34
+ query = options[:query]
35
+ distanceMultiplier = options[:distanceMultiplier]
36
+ mode = options[:mode] || :plane
37
+
38
+ nq = BSON::OrderedHash.new.tap do |near_query|
39
+ near_query["geoNear"] = clazz.to_s.tableize
40
+ near_query["near"] = center
41
+ near_query["num"] = num if num
42
+ near_query["maxDistance"] = maxDistance if maxDistance
43
+ near_query["distanceMultiplier"] = distanceMultiplier if distanceMultiplier
44
+ near_query["query"] = query if query
45
+
21
46
  # works in mongodb 1.7 but still in beta and not supported by mongodb
22
- query["spherical"] = true if mode == :sphere
47
+ near_query["spherical"] = true if mode == :sphere
23
48
  end
49
+ nq
24
50
  end
25
51
 
26
52
  def query_result clazz, query, center, location_attribute
@@ -28,13 +54,15 @@ module Mongoid
28
54
  query_result = clazz.collection.db.command(query)['results'].sort_by do |r|
29
55
  loc = r['obj'][location_attribute.to_s]
30
56
  r['distance'] = Mongoid::Geo::Haversine.distance(lat, lon, loc[1], loc[0])
57
+ r['clazz'] = clazz
31
58
  end
32
59
  end
33
60
 
34
61
  def create_result query_result
35
62
  query_result.inject([]) do |result, qr|
36
- res = Hashie::Mash.new(qr['obj'].to_hash)
37
- res.dis = qr['distance']
63
+ res = Hashie::Mash.new(qr['obj'].to_hash).extend(Mongoid::Geo::Model)
64
+ res.clazz = qr['clazz']
65
+ res.distance = qr['distance']
38
66
  res._id = qr['obj']['_id'].to_s
39
67
  result.push(res)
40
68
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_geo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: &2154072780 !ruby/object:Gem::Requirement
17
+ requirement: &2153931280 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '2.4'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2154072780
25
+ version_requirements: *2153931280
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: mongoid
28
- requirement: &2154072100 !ruby/object:Gem::Requirement
28
+ requirement: &2153930800 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 2.0.0.rc.6
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *2154072100
36
+ version_requirements: *2153930800
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: bson_ext
39
- requirement: &2154071400 !ruby/object:Gem::Requirement
39
+ requirement: &2153930340 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 1.1.6
45
45
  type: :runtime
46
46
  prerelease: false
47
- version_requirements: *2154071400
47
+ version_requirements: *2153930340
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: active_support
50
- requirement: &2154070740 !ruby/object:Gem::Requirement
50
+ requirement: &2153929860 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: 3.0.4
56
56
  type: :runtime
57
57
  prerelease: false
58
- version_requirements: *2154070740
58
+ version_requirements: *2153929860
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: hashie
61
- requirement: &2154070040 !ruby/object:Gem::Requirement
61
+ requirement: &2153929280 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,7 +66,7 @@ dependencies:
66
66
  version: '1.0'
67
67
  type: :runtime
68
68
  prerelease: false
69
- version_requirements: *2154070040
69
+ version_requirements: *2153929280
70
70
  description: Geo spatial extension on Mongoid 2, to add more geo-spatial capabilities
71
71
  email:
72
72
  - kmandrup@gmail.com