Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

last fixes, implement nearby

  • Loading branch information...
commit 285b42985b0d697108b60e6f426e9d18887de9c6 1 parent 9a4b7d1
@nofxx nofxx authored
View
4 lib/mongoid_geospatial/fields/geometry_field.rb
@@ -25,8 +25,8 @@ def radius r = 1
[center, r]
end
- def radius_sphere r = 1
- [center, r.to_f/Mongoid::Geospatial.earth_radius[:km]]
+ def radius_sphere r = 1, unit = :km
+ radius r.to_f/Mongoid::Geospatial.earth_radius[unit]
end
View
6 lib/mongoid_geospatial/fields/point.rb
@@ -1,6 +1,7 @@
module Mongoid
module Geospatial
class Point
+ include Enumerable
attr_accessor :x, :y
def initialize(x=nil, y=nil)
@@ -18,6 +19,11 @@ def [](args)
mongoize[args]
end
+ def each
+ yield x
+ yield y
+ end
+
def to_hsh xl = :x, yl = :y
{xl => x, yl => y}
end
View
11 lib/mongoid_geospatial/geospatial.rb
@@ -26,7 +26,7 @@ module Geospatial
@@earth_radius = EARTH_RADIUS.dup
included do
- attr_accessor :geo
+ # attr_accessor :geo
cattr_accessor :spatial_fields, :spatial_fields_indexed
@@spatial_fields = []
@@spatial_fields_indexed = []
@@ -51,6 +51,15 @@ def spatial_index name, options = {}
index({name => '2d'}, options)
end
+ def spatial_scope field, opts = {}
+ self.singleton_class.class_eval do
+ # define_method(:close) do |args|
+ define_method(:nearby) do |args|
+ queryable.where(field.near_sphere => args)
+ end
+ end
+ end
+
end
end
View
2  spec/models/bar.rb
@@ -8,4 +8,6 @@ class Bar
has_one :rating, :as => :ratable
spatial_index :location
+ spatial_scope :location
+
end
View
7 spec/mongoid_geospatial/field_option_spec.rb
@@ -3,9 +3,14 @@
describe Mongoid::Fields do
context "spatial" do
+ before do
+ Bar.create_indexes
+ end
it "should set some class methods" do
- Bar.near_location([1,1]).should eq([])
+ far = Bar.create!(name: "Far", location: [7,7])
+ near = Bar.create!(name: "Near", location: [2,2])
+ Bar.nearby([1,1]).should eq([near, far])
end
end
View
11 spec/mongoid_geospatial/fields/box_spec.rb
@@ -0,0 +1,11 @@
+require "spec_helper"
+
+describe Mongoid::Geospatial::Box do
+
+ it "should work" do
+ alarm = Alarm.new(area: [[1,2], [3,4]])
+ alarm.area.should be_a Mongoid::Geospatial::Box
+ end
+
+
+end
View
29 spec/mongoid_geospatial/fields/point_spec.rb
@@ -12,6 +12,13 @@
bar.location.should be_nil
end
+ it "should set point to nil" do
+ bar = Bar.create!(name: "Moe's", location: [1, 1])
+ bar.location = nil
+ bar.location.should be_nil
+ bar.save.should be_true
+ end
+
describe "methods" do
let(:bar) { Bar.create!(location: [3,2]) }
@@ -36,6 +43,14 @@
bar.location.radius_sphere[1].should be_within(0.0001).of(0.00015)
end
+ it "should have a radius sphere helper in meters" do
+ bar.location.radius_sphere(1000, :m)[1].should be_within(0.0001).of(0.00015)
+ end
+
+ it "should have a radius sphere helper in miles" do
+ bar.location.radius_sphere(1, :mi)[1].should be_within(0.0001).of(0.00025)
+ end
+
end
describe "queryable" do
@@ -66,6 +81,15 @@
Bar.where(:location.near => jim.location).should == [ paris, prague, berlin ]
end
+ it "returns the documents sorted closest to furthest" do
+ Bar.near(location: jim.location).should == [ paris, prague, berlin ]
+ end
+
+ it "returns the documents sorted closest to furthest sphere" do
+ person = Person.new(:location => [ 41.23, 2.9 ])
+ Bar.near_sphere(location: jim.location).should == [ paris, prague, berlin ]
+ end
+
it "returns the documents sorted closest to furthest sphere" do
person = Person.new(:location => [ 41.23, 2.9 ])
Bar.where(:location.near_sphere => jim.location).should == [ paris, prague, berlin ]
@@ -113,6 +137,11 @@
it "returns the documents within a center_sphere" do
Bar.where(:location.within_spherical_circle => [elvis.location, 0.5]).to_a.should include(mile9)
end
+
+ it "returns the documents within a box" do
+ Bar.within_box(location: [elvis.location, elvis.location.map(&:ceil)]).to_a.should == [ mile3 ]
+ end
+
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.