Permalink
Browse files

delegate methods

  • Loading branch information...
1 parent d931dab commit 7b70da7086e978167880a9d0acc9f9420a33d72c @nofxx nofxx committed Sep 22, 2012
@@ -4,7 +4,8 @@
require 'mongoid_geospatial/geospatial'
require 'mongoid_geospatial/extensions/core_ext'
require 'mongoid_geospatial/extensions/rgeo_spherical_point_impl'
-require 'mongoid_geospatial/field_option'
+require 'mongoid_geospatial/helpers/spatial'
+require 'mongoid_geospatial/helpers/delegate'
require 'mongoid_geospatial/fields/geometry_field'
@@ -0,0 +1,24 @@
+Mongoid::Fields.option :delegate do |model, field, options|
+ options = {} unless options.kind_of?(Hash)
+ x_meth = options[:x] || :x
+ y_meth = options[:y] || :y
+
+ model.instance_eval do
+ define_method x_meth do self[field.name][0]; end
+ define_method y_meth do self[field.name][1]; end
+
+ define_method "#{x_meth}=" do |arg|
+ self[field.name][0] = arg
+ end
+
+ define_method "#{y_meth}=" do |arg|
+ self[field.name][1] = arg
+ end
+
+ # model.class_eval do
+ # define_method "distance_from_#{field.name}" do |*args|
+ # self.distance_from(field.name, *args)
+ # end
+ # end
+ end
+end
@@ -1,5 +1,3 @@
-#require 'ostruct'
-
Mongoid::Fields.option :spatial do |model,field,options|
options = {} unless options.kind_of?(Hash)
# x_meth = options[:x] || :x
View
@@ -0,0 +1,11 @@
+class Bus
+ include Mongoid::Document
+ include Mongoid::Geospatial
+
+ field :plates, :type => String
+ field :location, :type => Point, :delegate => true
+
+ spatial_index :location
+ spatial_scope :location
+
+end
@@ -1,21 +0,0 @@
-require "spec_helper"
-
-describe Mongoid::Fields do
-
- context "spatial" do
- before do
- Bar.create_indexes
- end
-
- it "should set some class methods" do
- 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
-
- context "geom" do
- end
-
-end
@@ -0,0 +1,45 @@
+require "spec_helper"
+
+describe Mongoid::Fields do
+
+ context "delegate" do
+ before do
+ Bus.create_indexes
+ end
+
+ context 'x, y helpers' do
+
+ let(:bus) { Bus.create!(name: "Far", location: [7,8]) }
+
+ it "should set instance method x" do
+ bus.x.should eq(7)
+ end
+
+ it "should set instance method y" do
+ bus.y.should eq(8)
+ end
+
+ it "should set instance method x=" do
+ bus.x = 9
+ bus.x.should eq(9)
+ end
+
+ it "should set instance method y=" do
+ bus.y = 9
+ bus.y.should eq(9)
+ end
+
+ end
+
+ it "should set instance method x and y" do
+ bus = Bus.create!(name: "B", location: [7,7])
+ bus.x = 9; bus.y = 9
+ bus.location.to_a.should eq([9,9])
+ end
+
+ end
+
+ context "geom" do
+ end
+
+end
@@ -0,0 +1,21 @@
+require "spec_helper"
+
+describe Mongoid::Fields do
+
+ context "spatial" do
+ before do
+ Bar.create_indexes
+ end
+
+ it "should set some class methods" do
+ 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
+
+ context "geom" do
+ end
+
+end
@@ -1,11 +1,11 @@
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
+ before(:all) do
+ Mongoid::Geospatial.send(:remove_const, 'Point')
+ load "#{File.dirname(__FILE__)}/../../../lib/mongoid_geospatial/fields/point.rb"
Object.send(:remove_const, 'Place')
load "#{File.dirname(__FILE__)}/../../models/place.rb"
end
@@ -1,10 +1,10 @@
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
+
+ before(:all) do
+ Mongoid::Geospatial.send(:remove_const, 'Point')
+ load "#{File.dirname(__FILE__)}/../../../lib/mongoid_geospatial/fields/point.rb"
Object.send(:remove_const, 'Bar')
load "#{File.dirname(__FILE__)}/../../models/bar.rb"
end

0 comments on commit 7b70da7

Please sign in to comment.