Skip to content

Commit

Permalink
specs will now actually run but massively fail
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanong committed Jun 17, 2011
1 parent 7648bf1 commit 9bc87fd
Show file tree
Hide file tree
Showing 21 changed files with 94 additions and 106 deletions.
22 changes: 8 additions & 14 deletions lib/mongoid/contexts/geo_mongo.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
module Mongoid #:nodoc:
module Contexts #:nodoc:
class Mongo
def geo_near(center, location_attribute, args = {})
Mongoid::Geo.config do |c|
c.server_version ||= klass.connection.server_version
end

center = (center.respond_to?(location_attribute)) ? center.send(location_attribute) : center

def geo_near(center, args = {})
# minimum query
query = {
:geoNear => klass.to_s.tableize,
Expand All @@ -18,19 +12,19 @@ def geo_near(center, location_attribute, args = {})
# account for skip
if args[:num]
query[:num] = args[:num].to_i
elsif self.options[:limit] > 0
elsif self.options[:limit].kind_of?(Numeric) && self.options[:limit] > 0
query[:num] = (self.options[:skip] || 0) + self.options[:limit]
end


if args[:query]
query[:query] = args[:query]
elsif self.selector
elsif self.selector != {}
query[:query] = self.selector
end

if klass.connection.server_version >= 1.7
query["spherical"] = args[:spherical] if args[:spherical]
if klass.db.connection.server_version >= '1.7'
query["spherical"] = true if args[:spherical]

# mongodb < 1.7 returns degrees but with earth flat. in Mongodb 1.7 you can set sphere and let mongodb calculate the distance in Miles or KM
# for mongodb < 1.7 we need to run Haversine first before calculating degrees to Km or Miles. See below.
Expand All @@ -48,7 +42,7 @@ def geo_near(center, location_attribute, args = {})
# camel case is awkward in ruby when using variables...
res.from_point = result['fromPoint'] || center
res.from_hash = result['fromHash'] if result['fromHash']
if klass.connection.server_version >= 1.7
if klass.db.connection.server_version >= '1.7'
res.distance = result['dis'].to_f
else
dist_options = {}
Expand All @@ -61,8 +55,8 @@ def geo_near(center, location_attribute, args = {})
else
rows = []
end
if rows.size < self.options[:skip]
rows[self.options[:skip]..rows.size]
if self.options[:skip] && rows.size > self.options[:skip]
rows[self.options[:skip]..rows.size-1]
else
rows
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/criterion/complex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Criterion #:nodoc:
# <tt> { :field.lt => "value }</tt>
class Complex

def to_query v
def to_mongo_query v
{"$#{operator}" => v}
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/criterion/nested_operators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(opts = {})
# the command hash sent to the mongo DB to be executed
# Determines if the operator is some kind of 'box' or 'center' command
# Rhe operator will use a different type of array for each command type
def to_query v
def to_mongo_query v
query(v).to_query_hash
end

Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/criterion/twin_operators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(options = {})
@op_b = options[:op_b]
end

def to_query v
def to_mongo_query v
query(v).to_mongo_query
end

Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/extensions/hash/criteria_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ module CriteriaHelpers #:nodoc:
def expand_complex_criteria
hsh = {}
each_pair do |k,v|
if k.respond_to?(:key) && k.respond_to?(:to_query)
if k.respond_to?(:key) && k.respond_to?(:to_mongo_query)
hsh[k.key] ||= {}
hsh[k.key].merge!(k.to_query(v))
hsh[k.key].merge!(k.to_mongo_query(v))
else
hsh[k] = v
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/extensions/symbol/inflections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def nested_operators options
end

def get_op calc, operator
if calc.to_sym == :sphere && Mongoid::Geo::Config.server_version >= 1.7
if calc.to_sym == :sphere && Mongoid.master.connection.server_version >= '1.7'
"#{operator}Sphere"
else
operator
Expand Down
12 changes: 11 additions & 1 deletion lib/mongoid/geo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@
require 'mongoid/geo/queries'

module Mongoid
module Geo
module Geo
LNG_LAT = {
:lng => 0,
:long => 0,
:longitude => 0,
:lat => 1,
:latitude => 1,
}
# autoload :Config, 'mongoid/geo/config'
# autoload :Formula, 'mongoid/geo/formula'
# autoload :Unit, 'mongoid/geo/unit'
def self.config &block
yield Config if block
Config
Expand Down
42 changes: 2 additions & 40 deletions lib/mongoid/geo/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,6 @@ module Mongoid
module Geo
module Config
class << self
attr_accessor :server_version

def enable_extensions! *names
names = names.flatten.uniq
names = supported_extensions if names == [:all]
names.each {|name| enable_extension! name }
end

def enable_extension! name
case name.to_sym
when :geo_point
require 'mongoid/geo/extensions/geo_point'
when :geo_vectors, :geo_vector
require 'mongoid/geo/extensions/geo_vectors'
require 'mongoid/geo/extensions/geo_point'
end
end

def radian_multiplier
{
:feet => 364491.8,
Expand All @@ -39,19 +21,6 @@ def distance_calculator= clazz
@distance_calculator = clazz
end

def server_version
@server_version ||= 1.8
end

def coord_mode
@coord_mode ||= default_coord_mode
end

def coord_mode= coord_mode
raise "Coordinate mode must be one of: #{supported_coord_modes}, was: #{coord_mode}" unless supported_coord_modes.include?(coord_mode)
@coord_mode = coord_mode
end

def distance_formula
@distance_formula ||= default_distance_formula
end
Expand Down Expand Up @@ -81,10 +50,6 @@ def default_units
:kms
end

def default_coord_mode
:lng_lat
end

def default_distance_formula
:sphere
end
Expand All @@ -96,11 +61,8 @@ def supported_distance_formulas
def supported_units
[:kms, :miles]
end

def supported_coord_modes
[:lat_lng, :lng_lat]
end

end
end
end
end
end
6 changes: 3 additions & 3 deletions lib/mongoid/geo/core_ext.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module String
class String
def to_lng_lat
self.split(',').map(&:strip).map(&:to_f)
end
end

module Array
class Array
def to_lng_lat
self[0..1].map(&:to_f)
end
end

module Hash
class Hash
def to_lng_lat
[to_lng, to_lat]
end
Expand Down
6 changes: 3 additions & 3 deletions lib/mongoid/geo/queries.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
require 'mongoid/geo/box_query'
require 'mongoid/geo/circle_query'
require 'mongoid/geo/distance_query'
require 'mongoid/geo/queries/box_query'
require 'mongoid/geo/queries/circle_query'
require 'mongoid/geo/queries/distance_query'
5 changes: 3 additions & 2 deletions lib/mongoid/geo/queries/base_query.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Mongoid
module BaseQuery
module Geo
class BaseQuery
attr_reader :value

def initialize value
Expand All @@ -21,4 +22,4 @@ def to_point v
end
end
end
end
end
18 changes: 14 additions & 4 deletions lib/mongoid/geo_field_option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
# handlers when that option is provided.

Mongoid::Field.option :geo do |model,field,options|
model.index [[ field, Mongo::GEO2D ]], :min => -180, :max => 180
options = {} unless options.kind_of?(Hash)
model.index([[ field.name, Mongo::GEO2D ]]) unless options[:index] == false
model.class_eval {
options = {} unless options.kind_of?(Hash)
lat_meth = options[:lat] || "#{field}_lat"
lng_meth = options[:lng] || "#{field}_lng"
attr_accessor :from_point, :from_hash, :distance
lat_meth = options[:lat] || "#{field.name}_lat"
lng_meth = options[:lng] || "#{field.name}_lng"

define_method(lng_meth) { read_attribute(field)[0] }
define_method(lat_meth) { read_attribute(field)[1] }
Expand All @@ -23,5 +24,14 @@
write_attribute(field, [nil,nil]) if read_attribute(field).empty?
send(field)[1] = value
end

define_method field.name do |*args|
if args.size == 2
[read_attribute(field.name)[Mongoid::Geo::LNG_LAT[args[0]]],read_attribute(field.name)[Mongoid::Geo::LNG_LAT[args[1]]]]
else
read_attribute(field.name)
end
end

}
end
4 changes: 1 addition & 3 deletions lib/mongoid/geo_finders.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Mongoid #:nodoc:
module Finders
def geo_near(*args)
criteria.send(:geo_near, *args)
end
delegate :geo_near, :to => :criteria
end
end
2 changes: 1 addition & 1 deletion lib/mongoid/indexes/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Mongoid #:nodoc
module Indexes #:nodoc
module ClassMethods #:nodoc
def geo_index name, options = {}
index [[ name, Mongo::GEO2D ]], :min => -180, :max => 180
index [[ name, Mongo::GEO2D ]]
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/models/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Address
field :post_code
field :location, :type => Array, :geo => true

field :pos, :type => Array, :geo => {:lat => :latitude, :lng => :longitude}
field :pos, :type => Array, :geo => {:lat => :latitude, :lng => :longitude, :index => false}

# key :street
end
4 changes: 1 addition & 3 deletions spec/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ class Person
include Mongoid::Document

field :name
embeds_many :addresses

index :addresses
index :name
end
end
Loading

1 comment on commit 9bc87fd

@kristianmandrup
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent :)

Please sign in to comment.