Permalink
Browse files

more clean up

  • Loading branch information...
1 parent e25f26e commit 91185bab3d6aa188cca24c45c1045ad21a509371 @nofxx nofxx committed Sep 11, 2012
@@ -1,20 +1,16 @@
module Mongoid
module Geospatial
class LineString
- # See http://mongoid.org/en/mongoid/docs/upgrading.html
def mongoize
to_a
end
class << self
def demongoize(object)
- RGeo::Geographic.spherical_factory.line_string *object
+ RGeo::Geographic.spherical_factory.line_string object
end
- # def evolve(object)
- # { "$gte" => object.first, "$lte" => object.last }
- # end
end
end
end
@@ -1,15 +1,9 @@
module Mongoid
module Geospatial
class Point
- # See http://mongoid.org/en/mongoid/docs/upgrading.html
def mongoize
- self.respond_to?(:x) ? [x, y] : self
- # if object.respond_to? :x
- # { "x" => object.x, "y" => object.y }
- # else
- # { "x" => object[0], "y" => object[1] }
- # end
+ [x, y]
end
class << self
@@ -21,13 +15,18 @@ def demongoize(object)
end
def mongoize(object)
- object.respond_to?(:x) ? [object.x, object.y] : object
+ #return new.mongoize if object.respond_to?(:x)
+ case object
+ when Point then object.mongoize
+ when Hash then [object[:x], object[:y]]
+ else object
+ end
end
# Converts the object that was supplied to a criteria and converts it
# into a database friendly form.
def evolve(object)
- object.respond_to?(:x) ? [object.x, object.y] : object
+ object.respond_to?(:x) ? object.mongoize : object
end
end
@@ -1,7 +1,6 @@
module Mongoid
module Geospatial
class Polygon
- # See http://mongoid.org/en/mongoid/docs/upgrading.html
def mongoize
self #.flatten
@@ -54,37 +54,9 @@ module ClassMethods #:nodoc:
def spatial_index name, options = {}
self.spatial_fields_indexed << name
- if Mongoid::VERSION =~ /3.0/
- index({name => '2d'}, options)
- else
- index [[name, '2d']], options
- end
+ index({name => '2d'}, options)
end
end
- # def distance(p2, opts = {})
- # p1 = self.send(key)
- # Mongoid::Geospatial.distance(p1, p2, opts)
- # end
-
-
-
- # def self.distance(p1,p2,opts = {})
- # opts[:formula] ||= (opts[:spherical]) ? @@spherical_distance_formula : :pythagorean_theorem
- # p1 = p1.to_lng_lat if p1.respond_to?(:to_lng_lat)
- # p2 = p2.to_lng_lat if p2.respond_to?(:to_lng_lat)
-
- # rads = Formulas.send(opts[:formula], p1, p2)
-
- # if unit = earth_radius[opts[:unit]]
- # opts[:unit] = (rads.instance_variable_get("@radian")) ? unit : unit * RAD_PER_DEG
- # end
-
- # rads *= opts[:unit].to_f if opts[:unit]
- # rads
-
- # end
-
-
end
end
@@ -0,0 +1,34 @@
+require "spec_helper"
+
+describe Mongoid::Geospatial::Point do
+
+ describe "in use" do
+
+ it "should mongoize array" do
+ bar = Bar.new(location: [10, -9])
+ bar.location.class.should eql(RGeo::Geographic::SphericalPointImpl)
+ bar.location.x.should be_within(0.1).of(10)
+ bar.location.y.should be_within(0.1).of(-9)
+ end
+
+ it "should mongoize hash" do
+ bar = Bar.new(location: {x: 10, y: -9})
+ bar.location.class.should eql(RGeo::Geographic::SphericalPointImpl)
+ bar.location.x.should be_within(0.1).of(10)
+ bar.location.y.should be_within(0.1).of(-9)
+ end
+
+ describe "instantiated" do
+
+ let(:bar) { Bar.create!(name: 'Vitinho', location: [10,10]) }
+
+ it "should demongoize to rgeo" do
+ bar.location.class.should eql(RGeo::Geographic::SphericalPointImpl)
+ end
+
+ end
+
+ end
+
+
+end
@@ -0,0 +1,6 @@
+require "spec_helper"
+
+describe Mongoid::Geospatial::Polygon do
+
+
+end

0 comments on commit 91185ba

Please sign in to comment.