Skip to content

Commit

Permalink
#1 try remove and load again to test
Browse files Browse the repository at this point in the history
  • Loading branch information
nofxx committed Sep 22, 2012
1 parent 0f510b5 commit d931dab
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
4 changes: 4 additions & 0 deletions lib/mongoid_geospatial/wrappers/georuby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def to_geo
GeoRuby::SimpleFeatures::Point.xy(x, y)
end

def distance other
to_geo.spherical_distance(other.to_geo)
end

def self.mongoize(obj)
case obj
when GeoRuby::SimpleFeatures::Point then [obj.x, obj.y]
Expand Down
5 changes: 4 additions & 1 deletion lib/mongoid_geospatial/wrappers/rgeo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ module Mongoid
module Geospatial

class Point
delegate :distance, :to => :to_geo

def to_geo
RGeo::Geographic.spherical_factory.point x, y
end

def distance other
to_geo.distance other.to_geo
end

def self.mongoize(obj)
case obj
when RGeo::Geographic::SphericalPointImpl then [obj.x, obj.y]
Expand Down
14 changes: 10 additions & 4 deletions spec/mongoid_geospatial/wrappers/georuby_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
require "spec_helper"
Mongoid::Geospatial::Point = Class.new

Mongoid::Geospatial.send(:remove_const, 'Point')
load "#{File.dirname(__FILE__)}/../../../lib/mongoid_geospatial/fields/point.rb"

describe Mongoid::Geospatial::Point do

before do
Object.send(:remove_const, 'Place')
load "#{File.dirname(__FILE__)}/../../models/place.rb"
end

it "should not inferfer with mongoid" do
Place.create!(name: "Moe's")
Place.count.should eql(1)
end

it "should not respond to distance before loading external" do
it "should not respond to distance before loading external gem" do
bar = Place.create!(location: [5,5])
bar.location.should_not respond_to(:distance)
end
Expand All @@ -18,8 +24,8 @@
describe "queryable" do

before do
Place.create_indexes
Mongoid::Geospatial.use_georuby
Place.create_indexes
end

describe "(de)mongoize" do
Expand Down Expand Up @@ -48,7 +54,7 @@
it "should calculate 3d distances by default" do
bar = Place.create! location: [-73.77694444, 40.63861111 ]
bar2 = Place.create! location: [-118.40, 33.94] #,:unit=>:mi, :spherical => true)
bar.location.distance(bar2.location.to_geo).to_i.should be_within(1).of(3978262)
bar.location.distance(bar2.location).to_i.should be_within(1).of(3973808)
end

end
Expand Down
11 changes: 9 additions & 2 deletions spec/mongoid_geospatial/wrappers/rgeo_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
require "spec_helper"

Mongoid::Geospatial.send(:remove_const, 'Point')
load "#{File.dirname(__FILE__)}/../../../lib/mongoid_geospatial/fields/point.rb"

describe Mongoid::Geospatial::Point do
before do
Object.send(:remove_const, 'Bar')
load "#{File.dirname(__FILE__)}/../../models/bar.rb"
end

it "should not inferfer with mongoid" do
Bar.create!(name: "Moe's")
Expand All @@ -16,8 +23,8 @@
describe "queryable" do

before do
Bar.create_indexes
Mongoid::Geospatial.use_rgeo
Bar.create_indexes
end

describe "(de)mongoize" do
Expand Down Expand Up @@ -46,7 +53,7 @@
it "should calculate 3d distances by default" do
bar = Bar.create! location: [-73.77694444, 40.63861111 ]
bar2 = Bar.create! location: [-118.40, 33.94] #,:unit=>:mi, :spherical => true)
bar.location.distance(bar2.location.to_geo).to_i.should be_within(1).of(3978262)
bar.location.distance(bar2.location).to_i.should be_within(1).of(3978262)
end

end
Expand Down

0 comments on commit d931dab

Please sign in to comment.