mongoid_spacial 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.3
1
+ 0.2.4
@@ -37,7 +37,7 @@ module Mongoid #:nodoc:
37
37
 
38
38
  # set default opts
39
39
  opts[:skip] ||= 0
40
-
40
+
41
41
  if unit = Mongoid::Spacial.earth_radius[opts[:unit]]
42
42
  opts[:unit] = (opts[:spherical]) ? unit : unit * Mongoid::Spacial::RAD_PER_DEG
43
43
  end
@@ -47,13 +47,14 @@ module Mongoid #:nodoc:
47
47
  end
48
48
 
49
49
  opts[:distance_multiplier] = opts[:unit] if opts[:unit].kind_of?(Numeric)
50
-
50
+
51
51
  # setup paging.
52
52
  if opts.has_key?(:page)
53
53
  opts[:page] ||= 1
54
54
  opts[:paginator] ||= Mongoid::Spacial.paginator()
55
55
 
56
- opts[:per_page] ||= case opts[:paginator]
56
+ if opts[:per_page].blank?
57
+ opts[:per_page] = case opts[:paginator]
57
58
  when :will_paginate
58
59
  @document.per_page
59
60
  when :kaminari
@@ -61,6 +62,8 @@ module Mongoid #:nodoc:
61
62
  else
62
63
  Mongoid::Spacial.default_per_page
63
64
  end
65
+ end
66
+
64
67
  end
65
68
  opts[:query] = create_geo_near_query(center,opts)
66
69
  results = klass.db.command(opts[:query])
@@ -78,11 +81,11 @@ module Mongoid #:nodoc:
78
81
 
79
82
  # create limit and use skip
80
83
  if opts[:num]
81
- query['num'] = opts[:skip] + opts[:num].to_i
84
+ query['num'] = opts[:skip].to_i + opts[:num].to_i
82
85
  elsif opts[:limit]
83
- query['num'] = opts[:skip] + opts[:limit]
86
+ query['num'] = opts[:skip].to_i + opts[:limit].to_i
84
87
  elsif opts[:page]
85
- query['num'] = opts[:skip] +(opts[:page] * opts[:per_page])
88
+ query['num'] = opts[:skip].to_i + (opts[:page].to_i * opts[:per_page].to_i)
86
89
  end
87
90
 
88
91
  # allow the use of complex werieis
@@ -93,8 +96,8 @@ module Mongoid #:nodoc:
93
96
  end
94
97
 
95
98
  if opts[:max_distance]
96
- query['maxDistance'] = opts[:max_distance]
97
- query['maxDistance'] = query['maxDistance']/opts[:unit] if opts[:unit]
99
+ query['maxDistance'] = opts[:max_distance].to_f
100
+ query['maxDistance'] = query['maxDistance']/opts[:unit].to_f if opts[:unit]
98
101
  end
99
102
 
100
103
  if klass.db.connection.server_version >= '1.7'
@@ -102,7 +105,7 @@ module Mongoid #:nodoc:
102
105
 
103
106
  # mongodb < 1.7 returns degrees but with earth flat. in Mongodb 1.7 you can set sphere and let mongodb calculate the distance in Miles or KM
104
107
  # for mongodb < 1.7 we need to run Haversine first before calculating degrees to Km or Miles. See below.
105
- query['distanceMultiplier'] = opts[:distance_multiplier] if opts[:distance_multiplier]
108
+ query['distanceMultiplier'] = opts[:distance_multiplier].to_f if opts[:distance_multiplier]
106
109
  end
107
110
  query
108
111
  end
@@ -5,38 +5,41 @@ module Mongoid #:nodoc:
5
5
  # NearSpecial criterion is used when performing #near with symbols to get
6
6
  # get a shorthand syntax for where clauses.
7
7
  #
8
- # @example Conversion of a simple to complex criterion.
8
+ # @example Coninputersion of a simple to complex criterion.
9
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
- # Convert input to query for near or nearSphere
14
+ # Coninputert input to query for near or nearSphere
15
15
  #
16
16
  # @example
17
17
  # near = NearSpacial.new(:key => :field, :operator => "near")
18
- # near.to_mongo_query({:point => [:50,50], :max_distance => 5, :unit => :km}) => { '$near : [50,50]' , '$maxDistance' : 5 }
18
+ # near.to_mongo_query({:point => [:50,50], :max => 5, :unit => :km}) => { '$near : [50,50]' , '$maxDistance' : 5 }
19
19
  #
20
- # @param [Hash,Array] v input to conver to query
21
- def to_mongo_query(v)
22
- if v.kind_of? Hash
23
- v[:point] = v[:point].to_lng_lat if v[:point].respond_to?(:to_lng_lat)
24
- query = {"$#{operator}" => v[:point] }
25
- if v[:max]
26
- if unit = Mongoid::Spacial.earth_radius[v[:unit]]
20
+ # @param [Hash,Array] input input to coninputer to query
21
+ def to_mongo_query(input)
22
+ if input.kind_of?(Hash)
23
+ raise ':point required to make valid query' unless input[:point]
24
+ input[:point] = input[:point].to_lng_lat if input[:point].respond_to?(:to_lng_lat)
25
+ query = {"$#{operator}" => input[:point] }
26
+ if input[:max]
27
+ query['$maxDistance'] = input[:max].to_f
28
+
29
+ if unit = Mongoid::Spacial.earth_radius[input[:unit]]
27
30
  unit *= Mongoid::Spacial::RAD_PER_DEG unless operator =~ /sphere/i
28
- query['$maxDistance'] = v[:max]/unit
29
- else
30
- query['$maxDistance'] = v[:max]
31
+ input[:unit] = unit
31
32
  end
33
+
34
+ query['$maxDistance'] = query['$maxDistance']/input[:unit].to_f if input[:unit]
32
35
  end
33
36
  query
34
- elsif v.kind_of? Array
35
- if v.first.kind_of? Numeric
36
- {"$#{operator}" => v }
37
+ elsif input.kind_of? Array
38
+ if input.first.kind_of? Numeric
39
+ {"$#{operator}" => input }
37
40
  else
38
- v[0] = v[0].to_lng_lat if v[0].respond_to?(:to_lng_lat)
39
- {"$#{operator}" => v[0], '$maxDistance' => v[1] }
41
+ input[0] = input[0].to_lng_lat if input[0].respond_to?(:to_lng_lat)
42
+ {"$#{operator}" => input[0], '$maxDistance' => input[1] }
40
43
  end
41
44
  end
42
45
  end
@@ -28,20 +28,22 @@ module Mongoid #:nodoc:
28
28
  end
29
29
  elsif ['center','centerSphere'].index(@operator)
30
30
 
31
- if input.kind_of? Hash
32
- if input[:point]
33
- input[:point] = input[:point].to_lng_lat if input[:point].respond_to?(:to_lng_lat)
34
- if input[:max]
35
- if unit = Mongoid::Spacial.earth_radius[input[:unit]]
36
- unit *= Mongoid::Spacial::RAD_PER_DEG unless operator =~ /sphere/i
37
- input[:max] = input[:max]/unit
38
- end
39
- input = [input[:point],input[:max]]
40
- else
41
- input = input[:point]
31
+ if input.kind_of?(Hash) || input.kind_of?(ActiveSupport::OrderedHash)
32
+ raise ':point required to make valid query' unless input[:point]
33
+ input[:point] = input[:point].to_lng_lat if input[:point].respond_to?(:to_lng_lat)
34
+ if input[:max]
35
+ input[:max] = input[:max].to_f
36
+
37
+ if unit = Mongoid::Spacial.earth_radius[input[:unit]]
38
+ unit *= Mongoid::Spacial::RAD_PER_DEG unless operator =~ /sphere/i
39
+ input[:unit] = unit
42
40
  end
43
- elsif RUBY_VERSION.to_f > 1.9
44
- input = input.values
41
+
42
+ input[:max] = input[:max]/input[:unit].to_f if input[:unit]
43
+
44
+ input = [input[:point],input[:max]]
45
+ else
46
+ input = input[:point]
45
47
  end
46
48
  end
47
49
 
@@ -2,7 +2,7 @@ class Array
2
2
  def to_lng_lat
3
3
  self[0..1].map(&:to_f)
4
4
  end
5
- end
5
+ end
6
6
 
7
7
  class Hash
8
8
  def to_lng_lat
@@ -12,16 +12,16 @@ class Hash
12
12
 
13
13
  def to_lat
14
14
  v = (Mongoid::Spacial.lat_symbols & self.keys).first
15
- return self[v] if !v.nil? && self[v]
15
+ return self[v].to_f if !v.nil? && self[v]
16
16
  raise "Hash must contain #{Mongoid::Spacial.lat_symbols.inspect} if ruby version is less than 1.9" if RUBY_VERSION.to_f < 1.9
17
- raise "Hash cannot contain #{Mongoid::Spacial.lng_symbols.inspect} as the second item if there is no #{Mongoid::Spacial.lat_symbols.inspect}" if Mongoid::Spacial.lng_symbols.index(self.keys[1])
18
- self.values[1]
17
+ raise "Hash cannot contain #{Mongoid::Spacial.lng_symbols.inspect} as the second item if there is no #{Mongoid::Spacial.lat_symbols.inspect}" if Mongoid::Spacial.lng_symbols.index(self.keys[1])
18
+ self.values[1].to_f
19
19
  end
20
20
 
21
21
  def to_lng
22
22
  v = (Mongoid::Spacial.lng_symbols & self.keys).first
23
- return self[v] if !v.nil? && self[v]
23
+ return self[v].to_f if !v.nil? && self[v]
24
24
  raise "Hash cannot contain #{Mongoid::Spacial.lat_symbols.inspect} as the first item if there is no #{Mongoid::Spacial.lng_symbols.inspect}" if Mongoid::Spacial.lat_symbols.index(self.keys[0])
25
- self.values[0]
25
+ self.values[0].to_f
26
26
  end
27
- end
27
+ end
@@ -1,6 +1,6 @@
1
1
  module Mongoid
2
2
  module Spacial
3
- module Document
3
+ module Document
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  included do
@@ -22,7 +22,7 @@ module Mongoid
22
22
 
23
23
  module InstanceMethods #:nodoc:
24
24
  def distance_from(key,p2, unit = nil, formula = nil)
25
- p1 = res.send(key)
25
+ p1 = self.send(key)
26
26
  Mongoid::Spacial.distance(p1, p2, unit, formula = nil)
27
27
  end
28
28
  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.3"
8
+ s.version = "0.2.4"
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-24}
12
+ s.date = %q{2011-06-28}
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.3
5
+ version: 0.2.4
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-24 00:00:00 Z
13
+ date: 2011-06-28 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: -1587512017172586641
265
+ hash: -3361697488491906909
266
266
  segments:
267
267
  - 0
268
268
  version: "0"