mongoid_spacial 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -22,17 +22,15 @@ class River
22
22
  field :length, type: Integer
23
23
  field :average_discharge, type: Integer
24
24
  field :source, type: Array, spacial: true
25
- # if you want something besides the defaults {bit: 24, min: -180, max: 180} just set index to the options on the index
26
- # field :source, type: Array, spacial: true
27
- # try not set index for this field manually as we record what spacial fields are index for some handy fields later
28
25
 
29
26
  # set return_array to true if you do not want a hash returned all the time
30
- field :mouth, type: Array, spacial: {lat: 'latitude', lng: 'longitude', return_array: true }
27
+ field :mouth, type: Array, spacial: {lat: :latitude, lng: :longitude, return_array: true }
31
28
 
32
29
  # simplified spacial indexing
33
- # you can only index one
30
+ # you can only index one point in mongodb version below 1.9
31
+ # if you want something besides the defaults {bit: 24, min: -180, max: 180} just set index to the options on the index
34
32
  spacial_index :source
35
- # alternative
33
+
36
34
  end
37
35
  ```
38
36
 
@@ -57,16 +55,18 @@ hudson = River.create(
57
55
  # when setting array LONGITUDE MUST BE FIRST LATITUDE MUST BE SECOND
58
56
  # source: [-73.935833,44.106667],
59
57
  # but we can use hash in any order,
60
- # the default keys for latitude and longitude are 'lat' and 'lng' respectively
61
- source: {'lat' => 44.106667, 'lng' => -73.935833},
62
- # remember keys must be strings
63
- mouth: {'latitude' => 40.703056, 'longitude' => -74.026667}
58
+ # the default keys for latitude and longitude are :lat and :lng respectively
59
+ source: {:lat => 44.106667, :lng => -73.935833},
60
+ mouth: {:latitude => 40.703056, :longitude => -74.026667}
64
61
  )
65
62
 
66
63
  # now to access this spacial information we can now do this
67
- hudson.source #=> {'lng' => -73.935833, 'lat' => 44.106667}
64
+ hudson.source #=> {:lng => -73.935833, :lat => 44.106667}
68
65
  hudson.mouth #=> [-74.026667, 40.703056] # notice how this returned as a lng,lat array because return_array was true
69
66
  # notice how the order of lng and lat were switched. it will always come out like this when using spacial.
67
+ # Also adds a handy distance function
68
+ hudson.distance_from(:source, [-74,40], :mi)
69
+
70
70
  ```
71
71
  Mongoid Geo has extended all built in spacial symbol extentions
72
72
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.2
@@ -42,6 +42,10 @@ module Mongoid #:nodoc:
42
42
  opts[:unit] = (opts[:spherical]) ? unit : unit * Mongoid::Spacial::RAD_PER_DEG
43
43
  end
44
44
 
45
+ if unit = Mongoid::Spacial.earth_radius[opts[:distance_multiplier]]
46
+ opts[:distance_multiplier] = (opts[:spherical]) ? unit : unit * Mongoid::Spacial::RAD_PER_DEG
47
+ end
48
+
45
49
  opts[:distance_multiplier] = opts[:unit] if opts[:unit].kind_of?(Numeric)
46
50
 
47
51
  # setup paging.
@@ -3,14 +3,14 @@ module Mongoid #:nodoc:
3
3
  module Criterion #:nodoc:
4
4
 
5
5
  # NearSpecial criterion is used when performing #near with symbols to get
6
- # get a shorthand syntax for where clauses.
6
+ # get a shorthand syntax for where clauses.
7
7
  #
8
8
  # @example Conversion of a simple to complex criterion.
9
- # { :field => { "$nearSphere" => => [20,30]}, '$maxDistance' => 5 }
9
+ # { :field => { "$nearSphere" => => [20,30]}, '$maxDistance' => 5 }
10
10
  # becomes:
11
11
  # { :field.near(:sphere) => {:point => [20,30], :max => 5, :unit => :km} }
12
12
  class NearSpacial < Complex
13
-
13
+
14
14
  # Convert input to query for near or nearSphere
15
15
  #
16
16
  # @example
@@ -23,8 +23,10 @@ module Mongoid #:nodoc:
23
23
  v[:point] = v[:point].to_lng_lat if v[:point].respond_to?(:to_lng_lat)
24
24
  query = {"$#{operator}" => v[:point] }
25
25
  if v[:max]
26
- unit = Mongoid::Spacial.earth_radius[v[:unit]]
27
- query['$maxDistance'] = (unit) ? v[:max]/unit : v[:max]
26
+ if unit = Mongoid::Spacial.earth_radius[v[:unit]]
27
+ unit = (operator =~ /sphere/i) ? unit : unit * Mongoid::Spacial::RAD_PER_DEG
28
+ query['$maxDistance'] = v[:max]/unit
29
+ end
28
30
  end
29
31
  query
30
32
  elsif v.kind_of? Array
@@ -32,8 +32,10 @@ module Mongoid #:nodoc:
32
32
  if input[:point]
33
33
  input[:point] = input[:point].to_lng_lat if input[:point].respond_to?(:to_lng_lat)
34
34
  if input[:max]
35
- unit = Mongoid::Spacial.earth_radius[input[:unit]]
36
- input[:max] = input[:max]/unit if unit
35
+ if unit = Mongoid::Spacial.earth_radius[input[:unit]]
36
+ unit = (operator =~ /sphere/i) ? unit : unit * Mongoid::Spacial::RAD_PER_DEG
37
+ input[:max] = input[:max]/unit
38
+ end
37
39
  input = [input[:point],input[:max]]
38
40
  else
39
41
  input = input[:point]
@@ -20,7 +20,11 @@ module Mongoid
20
20
  LAT_SYMBOLS = [:y, :lat, :latitude]
21
21
 
22
22
  def self.distance(p1,p2,unit = nil, formula = nil)
23
- formula ||= self.distance_formula
23
+ formula ||= @@distance_formula
24
+
25
+ p1 = p1.to_lng_lat if p1.respond_to?(:to_lng_lat)
26
+ p1 = p1.to_lng_lat if p1.respond_to?(:to_lng_lat)
27
+
24
28
  unit = earth_radius[unit] if unit.kind_of?(Symbol) && earth_radius[unit]
25
29
  rads = Formulas.send(formula, p1, p2)
26
30
  (unit.kind_of?(Numeric)) ? unit*rads : rads
@@ -21,9 +21,9 @@ module Mongoid
21
21
  end
22
22
 
23
23
  module InstanceMethods #:nodoc:
24
- def distance_from(key,center, unit = nil, formula = nil)
25
- loc = res.send(key)
26
- Mongoid::Spacial.distance(center, loc, unit, formula = nil)
24
+ def distance_from(key,p2, unit = nil, formula = nil)
25
+ p1 = res.send(key)
26
+ Mongoid::Spacial.distance(p1, p2, unit, formula = nil)
27
27
  end
28
28
  end
29
29
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid_spacial}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.2"
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}]
12
- s.date = %q{2011-06-23}
12
+ s.date = %q{2011-06-24}
13
13
  s.description = %q{A Mongoid Extention that simplifies and adds support for MongoDB Geo Spacial Calculations.}
14
14
  s.email = %q{ryanong@gmail.com}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mongoid_spacial
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.0
5
+ version: 0.2.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ryan Ong
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-23 00:00:00 Z
13
+ date: 2011-06-24 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mongoid
@@ -262,7 +262,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
262
262
  requirements:
263
263
  - - ">="
264
264
  - !ruby/object:Gem::Version
265
- hash: -931697683295411699
265
+ hash: -35115700109095116
266
266
  segments:
267
267
  - 0
268
268
  version: "0"