mongoid_geo 0.1.2 → 0.1.3

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.
data/README.textile CHANGED
@@ -105,7 +105,7 @@ h3. nearSphere
105
105
 
106
106
  h3. nearMax
107
107
 
108
- Find all addresses near a point using spherical distance calculation
108
+ Find points near a given point within a maximum distance
109
109
 
110
110
  <pre>
111
111
  base.where(:locations.nearMax => [[ 72, -44 ], 5])
@@ -113,6 +113,40 @@ Find all addresses near a point using spherical distance calculation
113
113
 
114
114
  base.where(:locations.nearMax(:sphere) => [[ 72, -44 ], 5])
115
115
  # => { $nearSphere: [50, 40] , $maxDistanceSphere: 3 }
116
+
117
+ base.where(:locations.nearMax(:sphere, :flat) => [[ 72, -44 ], 5])
118
+ # => { $nearSphere: [50, 40] , $maxDistance: 3 }
119
+ </pre>
120
+
121
+ You can also use a Hash to define the nearMax
122
+
123
+ <pre>
124
+ places.where(:location.nearMax => {:point => [ 72, -44 ], :distance => 5})
125
+ </pre>
126
+
127
+ Or use an Object (which must have the methods #point and #distance that return the point and max distance from that point)
128
+
129
+ <pre>
130
+ near_max_ = (Struct.new :point, :distance).new
131
+ near_max.point = [50, 40]
132
+ near_max.distance = [30,55]
133
+
134
+ places.where(:location.nearMax => near_max)
135
+ </pre>
136
+
137
+ Note: For the points, you can also use a hash or an object with the methods/keys, either :lat, lng or :latitude, :longitude
138
+
139
+ Example:
140
+
141
+ <pre>
142
+ center = (Struct.new :lat, :lng).new
143
+ center.lat = 72
144
+ center.lng = -44
145
+ places.where(:location.withinCenter => [center, radius])
146
+
147
+ # OR
148
+
149
+ places.where(:location.withinCenter => [{:lat => 72, :lng => -44}, radius])
116
150
  </pre>
117
151
 
118
152
  h3. withinBox
@@ -120,24 +154,29 @@ h3. withinBox
120
154
  <pre>
121
155
  box = [[50, 40], [30,55]]
122
156
  base.where(:locations.withinBox => box)
123
- # => locations: {"$within" : {"$box" : box}
157
+ # => locations: {"$within" : {"$box" : [[50, 40], [30,55]]}
124
158
 
125
159
  base.where(:locations.withinBox(:sphere) => box)
126
- # => locations: {"$within" : {"$boxSphere" : box}
160
+ # => locations: {"$within" : {"$boxSphere" : [[50, 40], [30,55]]}
127
161
  </pre>
128
162
 
129
163
  You can also use a Hash to define the box
130
164
 
131
165
  <pre>
132
166
  places.where(:location.withinBox => {:lower_left => [50, 40], :upper_right => [30,55]})
167
+
168
+ # or mix and match
169
+
170
+ places.where(:location.withinBox => {:lower_left => {:lat => 50, :lng => 40}, :upper_right => [30,55] } )
133
171
  </pre>
134
172
 
135
173
  Or use an Object (which must have the methods #lower_left and #upper_right that return the points of the bounding box)
136
174
 
137
175
  <pre>
138
176
  box = (Struct.new :lower_left, :upper_right).new
139
- box.lower_left = [50, 40]
140
- box.upper_right = [30,55]
177
+ box.lower_left = [50, 40]
178
+ box.upper_right = [30, 55]
179
+
141
180
  places.where(:location.withinBox => box)
142
181
  </pre>
143
182
 
@@ -146,15 +185,15 @@ h3. withinCenter
146
185
  <pre>
147
186
  center = [50, 40]
148
187
  radius = 4
149
-
150
- # places: {"$within" : {"$centerSphere" : [center, radius]}
151
- places.where(:location.withinCenter(:sphere) => [center, radius])
152
188
 
153
- # places: {"$within" : {"$center" : [center, radius]}
154
189
  places.where(:location.withinCenter => [center, radius])
190
+ # => places: {"$within" : {"$center" : [[50, 40], 4]}
191
+
192
+ places.where(:location.withinCenter(:sphere) => [center, radius])
193
+ # => places: {"$within" : {"$centerSphere" : [[50, 40], 4]}
155
194
  </pre>
156
195
 
157
- You can also use a Hash to define the box
196
+ You can also use a Hash to define the circle
158
197
 
159
198
  <pre>
160
199
  places.where(:location.withinCenter => {:center => [50, 40], :radius => 4})
@@ -166,5 +205,6 @@ Or use an Object (which must have the methods #lower_left and #upper_right that
166
205
  circle = (Struct.new :center, :radius).new
167
206
  circle.center = [50, 40]
168
207
  circle.radius = 4
208
+
169
209
  places.where(:location.withinCenter => circle)
170
210
  </pre>
@@ -18,10 +18,14 @@ module Mongoid #:nodoc:
18
18
  @outer_op = opts[:outer_op]
19
19
  end
20
20
 
21
- def make_hash v
22
- v = extract_box(v) if !v.kind_of?(Array) && operator =~ /box/
21
+ def make_hash v
22
+ if operator =~ /box/
23
+ v = extract_box(v) if !v.kind_of?(Array)
24
+ v = [to_points(v.first), to_points(v.last)]
25
+ end
26
+
23
27
  v = extract_circle(v) if !v.kind_of?(Array) && operator =~ /center/
24
- {"$#{outer_op}" => {"$#{operator}" => v } }
28
+ {"$#{outer_op}" => {"$#{operator}" => to_points(v) } }
25
29
  end
26
30
 
27
31
  def hash
@@ -39,6 +43,10 @@ module Mongoid #:nodoc:
39
43
 
40
44
  protected
41
45
 
46
+ def to_points v
47
+ v.extend(Mongoid::Geo::Point).to_points
48
+ end
49
+
42
50
  def extract_circle(v)
43
51
  case v
44
52
  when Hash
@@ -49,12 +57,12 @@ module Mongoid #:nodoc:
49
57
  end
50
58
 
51
59
  def extract_box v
52
- case v
60
+ box = case v
53
61
  when Hash
54
62
  [v[:lower_left], v[:upper_right]]
55
63
  else
56
64
  v.respond_to?(:lower_left) ? [v.lower_left, v.upper_right] : raise("Can't extract box from: #{v}, must have :lower_left and :upper_right methods or equivalent hash keys in Hash")
57
- end
65
+ end
58
66
  end
59
67
  end
60
68
  end
@@ -20,7 +20,7 @@ module Mongoid #:nodoc:
20
20
 
21
21
  def make_hash v
22
22
  v = extract_nearMax(v) if !v.kind_of?(Array) && op_b =~ /max/i
23
- {"$#{op_a}" => v.first, "$#{op_b}" => v.last }
23
+ {"$#{op_a}" => to_points(v.first), "$#{op_b}" => to_points(v.last) }
24
24
  end
25
25
 
26
26
  def hash
@@ -35,10 +35,13 @@ module Mongoid #:nodoc:
35
35
  return false unless other.is_a?(self.class)
36
36
  self.op_a == other.op_a && self.op_b == other.op_b && self.key == other.key
37
37
  end
38
-
38
+
39
39
  protected
40
40
 
41
- protected
41
+ def to_points v
42
+ return v if v.kind_of? Fixnum
43
+ v.extend(Mongoid::Geo::Point).to_points
44
+ end
42
45
 
43
46
  def extract_nearMax(v)
44
47
  case v
@@ -0,0 +1,18 @@
1
+ module Mongoid::Geo
2
+ module Point
3
+ # convert hash or object to [x, y] of floats
4
+ def to_points
5
+ case self
6
+ when Hash
7
+ return [self[:lat], self[:lng]] if self[:lat]
8
+ return [self[:latitude], self[:longitude]] if self[:latitude]
9
+ raise "Hash must contain either :lat, :lng or :latitude, :longitude keys to be converted to a geo point"
10
+ else
11
+ return [self.lat, self.lng] if self.respond_to? :lat
12
+ return [self.latitude, self.longitude] if self.respond_to? :latitude
13
+ self
14
+ # raise 'Object must contain either #lat, #lng or #latitude, #longitude methods to be converted to a geo point'
15
+ end
16
+ end
17
+ end
18
+ end
data/lib/mongoid/geo.rb CHANGED
@@ -1,3 +1,9 @@
1
+ module Mongoid
2
+ module Geo
3
+ end
4
+ end
5
+
6
+ require 'mongoid/geo/point'
1
7
  require 'mongoid/geo/criteria'
2
8
  require 'mongoid/geo/fields'
3
9
  require 'mongoid/geo/index'
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.1.2
4
+ version: 0.1.3
5
5
  prerelease: !!null
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ default_executable: !!null
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: &2156734180 !ruby/object:Gem::Requirement
17
+ requirement: &2156985600 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2156734180
25
+ version_requirements: *2156985600
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: mongoid
28
- requirement: &2156733620 !ruby/object:Gem::Requirement
28
+ requirement: &2156984940 !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: *2156733620
36
+ version_requirements: *2156984940
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: bson_ext
39
- requirement: &2156733100 !ruby/object:Gem::Requirement
39
+ requirement: &2156984320 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,7 +44,7 @@ dependencies:
44
44
  version: 1.1.6
45
45
  type: :runtime
46
46
  prerelease: false
47
- version_requirements: *2156733100
47
+ version_requirements: *2156984320
48
48
  description: Geo spatial extension on Mongoid 2, to add more geo-spatial capabilities
49
49
  email:
50
50
  - kmandrup@gmail.com
@@ -61,6 +61,7 @@ files:
61
61
  - lib/mongoid/geo/fields.rb
62
62
  - lib/mongoid/geo/index.rb
63
63
  - lib/mongoid/geo/inflections.rb
64
+ - lib/mongoid/geo/point.rb
64
65
  - lib/mongoid/geo.rb
65
66
  - lib/mongoid_geo.rb
66
67
  - MIT-LICENSE