Permalink
Browse files

tests work woot

  • Loading branch information...
1 parent 47aae15 commit 375312d1682f38b4ab7d619eeee3163d138a1bee @ryanong committed Jun 22, 2011
Showing with 472 additions and 396 deletions.
  1. +1 −0 .gitignore
  2. +14 −0 Gemfile
  3. +3 −3 README.md
  4. +0 −19 README.rdoc
  5. +0 −8 lib/mongoid/criterion.rb
  6. BIN lib/mongoid/extentions/symbol/.inflections.rb.swo
  7. +0 −27 lib/mongoid/spacial/core_ext.rb
  8. +11 −9 lib/mongoid_spacial.rb
  9. +8 −8 lib/{mongoid → mongoid_spacial}/contexts/mongo.rb
  10. 0 lib/{mongoid → mongoid_spacial}/criteria.rb
  11. +8 −0 lib/mongoid_spacial/criterion.rb
  12. 0 lib/{mongoid → mongoid_spacial}/criterion/complex.rb
  13. +14 −0 lib/mongoid_spacial/criterion/inclusion.rb
  14. +1 −1 lib/{mongoid → mongoid_spacial}/criterion/near_spacial.rb
  15. +19 −12 lib/{mongoid → mongoid_spacial}/criterion/within_spacial.rb
  16. 0 lib/{mongoid → mongoid_spacial}/extentions/hash/criteria_helpers.rb
  17. 0 lib/{mongoid → mongoid_spacial}/extentions/symbol/inflections.rb
  18. +14 −12 lib/{mongoid → mongoid_spacial}/field_option.rb
  19. 0 lib/{mongoid → mongoid_spacial}/finders.rb
  20. +13 −6 lib/{mongoid → mongoid_spacial}/spacial.rb
  21. +27 −0 lib/mongoid_spacial/spacial/core_ext.rb
  22. +8 −5 lib/{mongoid → mongoid_spacial}/spacial/document.rb
  23. 0 lib/{mongoid → mongoid_spacial}/spacial/formulas.rb
  24. +101 −0 spec/functional/mongoid/contexts/mongo_spec.rb
  25. +118 −264 spec/functional/mongoid/criterion/inclusion_spec.rb
  26. +4 −2 spec/models/bar.rb
  27. +0 −11 spec/models/image.rb
  28. +0 −7 spec/models/movie.rb
  29. +1 −1 spec/spec_helper.rb
  30. +1 −1 spec/support/authentication.rb
  31. +15 −0 spec/unit/mongoid_spacial/criterion/complex_spec.rb
  32. 0 spec/unit/mongoid_spacial/criterion/inclusion_spec.rb
  33. +39 −0 spec/unit/mongoid_spacial/criterion/near_spacial_spec.rb
  34. +52 −0 spec/unit/mongoid_spacial/criterion/within_spacial_spec.rb
View
@@ -1,3 +1,4 @@
+Gemfile.lock
# rcov generated
coverage
View
@@ -5,10 +5,24 @@ source "http://rubygems.org"
# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
+gem 'mongoid'
+gem "bson", '>= 1.3', :platforms => [:jruby] # for non jruby apps, require bson_ext in your Gemfile to boost performance
+gem "bson_ext", '>= 1.3', :platforms => [:mri]
+gem 'activesupport'
+
group :development do
gem "rspec", "~> 2.3.0"
gem "yard", "~> 0.6.0"
gem "bundler", "~> 1.0.0"
gem "jeweler", "~> 1.6.2"
gem "rcov", ">= 0"
end
+
+group :test, :development do
+ gem 'linecache19'
+ gem 'ruby-debug19'
+end
+
+group :test do
+ gem 'mocha'
+end
View
@@ -16,15 +16,15 @@ Set up some slugs:
```ruby
class River
include Mongoid::Document
- include Mongoid::Geo::Document
+ include Mongoid::Spacial::Document
field :name, type: String
field :length, type: Integer
field :average_discharge, type: Integer
field :source, type: Array, spacial: true
# if you want something besides the defaults {bit: 24, min: -180, max: 180} just set index to the options on the index
# field :source, type: Array, spacial: true
- # try not set index for this field manually as we record what geo fields are index for some handy fields later
+ # try not set index for this field manually as we record what spacial fields are index for some handy fields later
# set return_array to true if you do not want a hash returned all the time
field :mouth, type: Array, spacial: {lat: 'latitude', lng: 'longitude', return_array: true }
@@ -106,7 +106,7 @@ ToDo
Thanks
-----------
-* Thanks to Kristian Mandrup for creating the base of the gem
+* Thanks to Kristian Mandrup for creating the base of the gem and a few of the tests
* Thanks to CarZen LLC. for letting me release the code we are using
Contributing to mongoid_ranges
View
@@ -1,19 +0,0 @@
-= mongoid_spacial
-
-Description goes here.
-
-== Contributing to mongoid_spacial
-
-* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
-* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
-* Fork the project
-* Start a feature/bugfix branch
-* Commit and push until you are happy with your contribution
-* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
-* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
-
-== Copyright
-
-Copyright (c) 2011 Ryan Ong. See LICENSE.txt for
-further details.
-
@@ -1,8 +0,0 @@
-require 'mongoid/criterion/complex'
-# encoding: utf-8
-module Mongoid #:nodoc:
- module Criterion #:nodoc:
- autoload :Near_Spacial, 'mongoid/criterion/near_spacial'
- autoload :Within_Spacial, 'mongoid/criterion/within_spacial'
- end
-end
Binary file not shown.
@@ -1,27 +0,0 @@
-class Array
- def to_lng_lat
- self[0..1].map(&:to_f)
- end
-end
-
-class Hash
- def to_lng_lat
- raise "Hash must have at least 2 items" if self.size < 2
- [to_lng, to_lat]
- end
-
- def to_lat
- v = (Mongoid::Spacial::LAT_SYMBOLS & self.keys).first
- return self[v.first] if !v.nil? && self[v.first]
- raise "Hash must contain #{Mongoid::Spacial::LAT_SYMBOLS.inspect} if ruby version is less than 1.9" if RUBY_VERSION.to_f < 1.9
- raise "Hash cannot contain #{Mongoid::Spacial::LNG_SYMBOLS.inspect} as the second item if there is no #{Mongoid::Spacial::LAT_SYMBOLS.inspect}" if Mongoid::Geo.lng_symbols.index(self.keys[1])
- self.values[1]
- end
-
- def to_lng
- v = (Mongoid::Spacial::LNG_SYMBOLS & self.keys).first
- return self[v.first] if !v.nil? && self[v.first]
- raise "Hash cannot contain #{Mongoid::Spacial::LAT_SYMBOLS.inspect} as the first item if there is no #{Mongoid::Spacial::LNG_SYMBOLS.inspect}" if Mongoid::Geo.lat_symbols.index(self.keys[0])
- self.values[0]
- end
-end
@@ -1,9 +1,11 @@
-require 'mongoid/contexts/mongo'
-require 'mongoid/criteria'
-require 'mongoid/criterion'
-
-require 'mongoid/extentions/hash/criteria_helpers'
-require 'mongoid/extentions/symbol/inflections'
-require 'mongoid/field_option'
-require 'mongoid/finders'
-require 'mongoid/spacial'
+require 'mongoid'
+require 'active_support/core_ext/string/inflections'
+require 'active_support/concern'
+require 'mongoid_spacial/contexts/mongo'
+require 'mongoid_spacial/criteria'
+require 'mongoid_spacial/criterion'
+require 'mongoid_spacial/extentions/hash/criteria_helpers'
+require 'mongoid_spacial/extentions/symbol/inflections'
+require 'mongoid_spacial/field_option'
+require 'mongoid_spacial/finders'
+require 'mongoid_spacial/spacial'
@@ -27,17 +27,17 @@ class Mongo #:nodoc:
# @options opts [Numeric] :max_distance The max distance of a row that should be returned in :unit(s)
# @options opts [Numeric, :km, :k, :mi, :ft] :unit automatically sets :distance_multiplier and converts :max_distance
# @options opts [true,false] :spherical Will determine the distance either by spherical calculation or flat calculation
- # @options opts [TrueClass,Array<Symbol>] :calculate Which extra fields to calculate distance for in ruby, if set to TrueClass it will calculate all geo fields
+ # @options opts [TrueClass,Array<Symbol>] :calculate Which extra fields to calculate distance for in ruby, if set to TrueClass it will calculate all spacial fields
#
# @return [ Array ] Sorted Rows
def geo_near(center, opts = {})
center = center.to_lng_lat if center.respond_to?(:to_lng_lat)
- if distance_multiplier = Mongoid::Spacial::EARTH_RADIUS[opts.delete(:unit)]
+ if distance_multiplier = Mongoid::Spacial.earth_radius[opts.delete(:unit)]
opts[:distance_multiplier] = distance_multiplier
end
- query = create_geo_near_query(opts)
+ query = create_geo_near_query(center,opts)
results = klass.db.command(query)
if results['results'].kind_of?(Array) && results['results'].size > 0
rows = results['results'].collect do |result|
@@ -51,12 +51,12 @@ def geo_near(center, opts = {})
res.geo[key.snakecase.to_sym] = value
end
# dist_options[:formula] = opts[:formula] if opts[:formula]
- opts[:calculate] = @@spacial_fields_indexed if opts[:calculate] == true
+ opts[:calculate] = klass.spacial_fields_indexed if klass.spacial_fields_indexed.kind_of?(Array) && opts[:calculate] == true
if opts[:calculate]
opts[:calculate] = [opts[:calculate]] unless opts[:calculate].kind_of? Array
opts[:calculate] = opts[:calculate].map(&:to_sym) & geo_fields
- if @@spacial_fields_indexed.size == 1
- primary = @@spacial_fields_indexed.first
+ if klass.spacial_fields_indexed.kind_of?(Array) && klass.spacial_fields_indexed.size == 1
+ primary = klass.spacial_fields_indexed.first
end
opts[:calculate].each do |key|
key = (key.to_s+'_distance').to_s
@@ -86,7 +86,7 @@ def geo_near(center, opts = {})
private
- def create_geo_near_query(opts)
+ def create_geo_near_query(center,opts)
# minimum query
query = {
:geoNear => klass.to_s.tableize,
@@ -102,7 +102,7 @@ def create_geo_near_query(opts)
# allow the use of complex werieis
if opts[:query]
- query['query'] = self.where(opts[:query]).selector
+ query['query'] = self.criteria.where(opts[:query]).selector
elsif self.selector != {}
query['query'] = self.selector
end
File renamed without changes.
@@ -0,0 +1,8 @@
+require 'mongoid_spacial/criterion/complex'
+# encoding: utf-8
+module Mongoid #:nodoc:
+ module Criterion #:nodoc:
+ autoload :NearSpacial, 'mongoid_spacial/criterion/near_spacial'
+ autoload :WithinSpacial, 'mongoid_spacial/criterion/within_spacial'
+ end
+end
@@ -0,0 +1,14 @@
+# encoding: utf-8
+module Mongoid #:nodoc:
+ module Criterion #:nodoc:
+ module Inclusion
+ def near(attributes = {})
+ update_selector(attributes, "$near")
+ end
+
+ def near_sphere(attributes = {})
+ update_selector(attributes, "$near")
+ end
+ end
+ end
+end
@@ -23,7 +23,7 @@ def to_mongo_query(v)
v[:point] = v[:point].to_lng_lat if v[:point].respond_to?(:to_lng_lat)
query = {"$#{operator}" => v[:point] }
if v[:max]
- unit = Mongoid::Spacial::EARTH_RADIUS[v[:unit]]
+ unit = Mongoid::Spacial.earth_radius[v[:unit]]
query['$maxDistance'] = (unit) ? v[:max]/unit : v[:max]
end
query
@@ -22,27 +22,34 @@ def to_mongo_query(input)
if ['box','polygon'].index(@operator)
input = input.values if input.kind_of?(Hash)
if input.respond_to?(:map)
- input.map{ |v| (v.respond_to?(:to_lng_lat)) ? v.to_lng_lat : v }
+ input.map!{ |v| (v.respond_to?(:to_lng_lat)) ? v.to_lng_lat : v }
else
input
end
elsif ['center','centerSphere'].index(@operator)
+
+ if input.kind_of? Hash
+ if input[:point]
+ input[:point] = input[:point].to_lng_lat if input[:point].respond_to?(:to_lng_lat)
+ if input[:max]
+ unit = Mongoid::Spacial.earth_radius[input[:unit]]
+ input[:max] = input[:max]/unit if unit
+ input = [input[:point],input[:max]]
+ else
+ input = input[:point]
+ end
+ elsif RUBY_VERSION.to_f > 1.9
+ input = input.values
+ end
+ end
+
if input.kind_of? Array
input[0] = input[0].to_lng_lat if input[0].respond_to?(:to_lng_lat)
- input
- elsif v.kind_of? Hash
- input[:point] = input[:point].to_lng_lat if input[:point].respond_to?(:to_lng_lat)
- query = input[:point]
- if v[:max]
- unit = Mongoid::Spacial::EARTH_RADIUS[v[:unit]]
- v[:max] = v[:max]/unit if unit
- query = [query,v[:max]]
- end
- query
end
+
end
+ {'$within' => {"$#{@operator}"=>input} }
end
-
end
end
end
@@ -1,34 +1,36 @@
+require 'ostruct'
# Field changes to Fields from mongoid 2.0 to mongoid 2.1
field = (defined?(Mongoid::Field)) ? Mongoid::Field : Mongoid::Fields
field.option :spacial do |model,field,options|
options = {} unless options.kind_of?(Hash)
+ lat_meth = options[:lat] || "lat"
+ lng_meth = options[:lng] || "lng"
model.class_eval do
- @@spacial_fields << field.name.to_sym if @@spacial_fields.kind_of? Array
-
- lat_meth = options[:lat] || "lat"
- lng_meth = options[:lng] || "lng"
+ self.spacial_fields ||= []
+ self.spacial_fields << field.name.to_sym if self.spacial_fields.kind_of? Array
define_method "distance_from_#{field.name}" do |*args|
self.distance_from(field.name, *args)
end
define_method field.name do
- output = read_attribute(field.name) || [nil,nil]
+ output = self[field.name] || [nil,nil]
output = (options[:return_array]) ? lng_lat_a : {lng_meth => output[0], lat_meth => output[0]}
return options[:class].new(output) if options[:class]
output
end
-
+
define_method "#{field.name}=" do |arg|
- if arg.kind_of?(Hash)
- arg = write_attribute([arg[lng_meth], arg[lat_meth]])
+ if arg.kind_of?(Hash) && arg[lng_meth] && arg[lat_meth]
+ arg = [arg[lng_meth], arg[lat_meth]]
elsif arg.respond_to?(:to_lng_lat)
- arg = write_attribute(arg.to_lat_lng) if arg.respond_to?(:to_lat_lng)
- else
- write_attribute(arg)
+ arg = arg.to_lat_lng if arg.respond_to?(:to_lat_lng)
end
- (options[:return_array]) ? arg[0..1] : {lng_meth => arg[0], lat_meth => arg[0]}
+ self[field.name]=arg
+ return arg[0..1] if options[:return_array]
+ return h = {lng_meth => arg[0], lat_meth => arg[0]} if options[:class].blank?
+ options[:class].new(h)
end
end
end
File renamed without changes.
@@ -1,10 +1,9 @@
-require 'mongoid/spacial/core_ext'
+require 'mongoid_spacial/spacial/core_ext'
module Mongoid
module Spacial
-
- autoload :Formulas, 'mongoid/spacial/formulas'
- autoload :Document, 'mongoid/spacial/document'
-
+ autoload :Formulas, 'mongoid_spacial/spacial/formulas'
+ autoload :Document, 'mongoid_spacial/spacial/document'
+
EARTH_RADIUS_KM = 6371 # taken directly from mongodb
EARTH_RADIUS = {
@@ -19,10 +18,18 @@ module Spacial
def distance(p1,p2,unit = nil, formula = nil)
formula ||= self.distance_formula
- unit = EARTH_RADIUS[unit] if unit.kind_of?(Symbol) && EARTH_RADIUS[unit]
+ unit = earth_radius[unit] if unit.kind_of?(Symbol) && earth_radius[unit]
rads = Formulas.send(formula, p1, p2)
(unit.kind_of?(Numeric)) ? unit*rads : rads
end
+ mattr_accessor :lng_symbols
+ @@lng_symbols = LNG_SYMBOLS.dup
+
+ mattr_accessor :lat_symbols
+ @@lat_symbols = LAT_SYMBOLS.dup
+
+ mattr_accessor :earth_radius
+ @@earth_radius = EARTH_RADIUS.dup
mattr_accessor :distance_formula
@@distance_formula = :n_vector
@@ -0,0 +1,27 @@
+class Array
+ def to_lng_lat
+ self[0..1].map(&:to_f)
+ end
+end
+
+class Hash
+ def to_lng_lat
+ raise "Hash must have at least 2 items" if self.size < 2
+ [to_lng, to_lat]
+ end
+
+ def to_lat
+ v = (Mongoid::Spacial.lat_symbols & self.keys).first
+ return self[v] if !v.nil? && self[v]
+ raise "Hash must contain #{Mongoid::Spacial.lat_symbols.inspect} if ruby version is less than 1.9" if RUBY_VERSION.to_f < 1.9
+ raise "Hash cannot contain #{Mongoid::Spacial.lng_symbols.inspect} as the second item if there is no #{Mongoid::Spacial.lat_symbols.inspect}" if Mongoid::Geo.lng_symbols.index(self.keys[1])
+ self.values[1]
+ end
+
+ def to_lng
+ v = (Mongoid::Spacial.lng_symbols & self.keys).first
+ return self[v] if !v.nil? && self[v]
+ raise "Hash cannot contain #{Mongoid::Spacial.lat_symbols.inspect} as the first item if there is no #{Mongoid::Spacial.lng_symbols.inspect}" if Mongoid::Geo.lat_symbols.index(self.keys[0])
+ self.values[0]
+ end
+end
Oops, something went wrong.

0 comments on commit 375312d

Please sign in to comment.