mongoid_geo 0.1.0 → 0.1.1

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.
@@ -44,7 +44,7 @@ module Mongoid #:nodoc:
44
44
  when Hash
45
45
  [v[:center], v[:radius]]
46
46
  else
47
- v.respond_to?(:center) ? [v.center, v.radius] : raise("Can't extract box from: #{v}, must have :center and :radius methods or hash keys")
47
+ v.respond_to?(:center) ? [v.center, v.radius] : raise("Can't extract box from: #{v}, must have :center and :radius methods or equivalent hash keys in Hash")
48
48
  end
49
49
  end
50
50
 
@@ -53,7 +53,7 @@ module Mongoid #:nodoc:
53
53
  when Hash
54
54
  [v[:lower_left], v[:upper_right]]
55
55
  else
56
- 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 hash keys")
56
+ 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
57
  end
58
58
  end
59
59
  end
@@ -18,7 +18,8 @@ module Mongoid #:nodoc:
18
18
  @op_b = opts[:op_b]
19
19
  end
20
20
 
21
- def make_hash v
21
+ def make_hash v
22
+ v = extract_nearMax(v) if !v.kind_of?(Array) && op_b =~ /max/i
22
23
  {"$#{op_a}" => v.first, "$#{op_b}" => v.last }
23
24
  end
24
25
 
@@ -34,6 +35,20 @@ module Mongoid #:nodoc:
34
35
  return false unless other.is_a?(self.class)
35
36
  self.op_a == other.op_a && self.op_b == other.op_b && self.key == other.key
36
37
  end
38
+
39
+ protected
40
+
41
+ protected
42
+
43
+ def extract_nearMax(v)
44
+ case v
45
+ when Hash
46
+ [v[:point], v[:distance]]
47
+ else
48
+ v.respond_to?(:point) ? [v.point, v.distance] : raise("Can't extract nearMax values from: #{v}, must have :point and :maxDistance methods or equivalent hash keys in Hash")
49
+ end
50
+ end
51
+
37
52
  end
38
53
  end
39
54
  end
@@ -5,11 +5,14 @@ module Mongoid #:nodoc
5
5
  def create_accessors(name, meth, options = {})
6
6
  generated_field_methods.module_eval do
7
7
  define_method(meth) { read_attribute(name) }
8
- options
9
8
  define_method("#{meth}=") do |value|
10
- value = if options[:type] == Array && options[:geo]
11
- value.kind_of?(String) ? value.split(",") : value
12
- end.map(&:to_f)
9
+ if options[:type] == Array && options[:geo]
10
+ value = case value
11
+ when String then value.split(",").map(&:to_f)
12
+ when Array then value.map(&:to_f)
13
+ else value
14
+ end
15
+ end
13
16
  write_attribute(name, value)
14
17
  end
15
18
  define_method("#{meth}?") do
@@ -16,8 +16,16 @@ module Mongoid #:nodoc:
16
16
  Criterion::Complex.new(:operator => 'nearSphere', :key => self)
17
17
  end
18
18
 
19
- def nearMax calc = :flat
20
- Criterion::TwinOperators.new(:op_a => get_op(calc, 'near'), :op_b => get_op(calc, 'maxDistance'), :key => self)
19
+ def nearMax *calcs
20
+ calcs = (!calcs || calcs.empty?) ? [:flat] : calcs
21
+ case calcs.size
22
+ when 1
23
+ Criterion::TwinOperators.new(:op_a => get_op(calcs.first, 'near'), :op_b => get_op(calcs.first, 'maxDistance'), :key => self)
24
+ when 2
25
+ Criterion::TwinOperators.new(:op_a => get_op(calcs.first, 'near'), :op_b => get_op(calcs.last, 'maxDistance'), :key => self)
26
+ else
27
+ raise "method nearMax takes one or two symbols as arguments, each symbol must be either :flat or :sphere"
28
+ end
21
29
  end
22
30
 
23
31
  def withinBox calc = :flat
metadata CHANGED
@@ -1,19 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_geo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease: !!null
6
6
  platform: ruby
7
- authors: []
7
+ authors:
8
+ - Kristian Mandrup
8
9
  autorequire: !!null
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
  date: 2011-02-05 00:00:00.000000000 +01:00
12
13
  default_executable: !!null
13
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: &2156773960 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *2156773960
14
26
  - !ruby/object:Gem::Dependency
15
27
  name: mongoid
16
- requirement: &2159951560 !ruby/object:Gem::Requirement
28
+ requirement: &2156773320 !ruby/object:Gem::Requirement
17
29
  none: false
18
30
  requirements:
19
31
  - - ! '>='
@@ -21,10 +33,10 @@ dependencies:
21
33
  version: 2.0.0.rc.6
22
34
  type: :runtime
23
35
  prerelease: false
24
- version_requirements: *2159951560
36
+ version_requirements: *2156773320
25
37
  - !ruby/object:Gem::Dependency
26
38
  name: bson_ext
27
- requirement: &2159950420 !ruby/object:Gem::Requirement
39
+ requirement: &2156772700 !ruby/object:Gem::Requirement
28
40
  none: false
29
41
  requirements:
30
42
  - - ! '>='
@@ -32,9 +44,10 @@ dependencies:
32
44
  version: 1.1.6
33
45
  type: :runtime
34
46
  prerelease: false
35
- version_requirements: *2159950420
47
+ version_requirements: *2156772700
36
48
  description: Geo spatial extension on Mongoid 2, to add more geo-spatial capabilities
37
- email: !!null
49
+ email:
50
+ - kmandrup@gmail.com
38
51
  executables: []
39
52
  extensions: []
40
53
  extra_rdoc_files: []
@@ -70,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
83
  requirements:
71
84
  - - ! '>='
72
85
  - !ruby/object:Gem::Version
73
- version: '0'
86
+ version: 1.3.6
74
87
  requirements: []
75
88
  rubyforge_project: !!null
76
89
  rubygems_version: 1.5.0