Skip to content
Snippets Groups Projects
Commit fbaa0273 authored by Alex Reisner's avatar Alex Reisner
Browse files

Improve checking for presence of coordinates.

This fixes issue #244.
parent f7fbf231
No related branches found
No related tags found
No related merge requests found
......@@ -24,6 +24,18 @@ module Geocoder
# Not a number constant
NAN = defined?(::Float::NAN) ? ::Float::NAN : 0 / 0.0
##
# Returns true if all given arguments are valid latitude/longitude values.
#
def coordinates_present?(*args)
args.each do |a|
# note that Float::NAN != Float::NAN
# still, this could probably be improved:
return false if (!a.is_a?(Numeric) or a.to_s == "NaN")
end
true
end
##
# Distance spanned by one degree of latitude in the given units.
#
......
......@@ -33,7 +33,7 @@ module Geocoder::Store
#
scope :near, lambda{ |location, *args|
latitude, longitude = Geocoder::Calculations.extract_coordinates(location)
if latitude and longitude and ![latitude, longitude].include?(Geocoder::Calculations::NAN)
if Geocoder::Calculations.coordinates_present?(latitude, longitude)
near_scope_options(latitude, longitude, *args)
else
where(false_condition) # no results if no lat/lon given
......@@ -68,7 +68,9 @@ module Geocoder::Store
def distance_from_sql(location, *args)
latitude, longitude = Geocoder::Calculations.extract_coordinates(location)
distance_from_sql_options(latitude, longitude, *args) if latitude and longitude
if Geocoder::Calculations.coordinates_present?(latitude, longitude)
distance_from_sql_options(latitude, longitude, *args)
end
end
private # ----------------------------------------------------------------
......
......@@ -178,5 +178,11 @@ class CalculationsTest < Test::Unit::TestCase
assert_in_delta 1.0 / 3, result.first, 1E-5
assert_in_delta 2.0 / 3, result.last, 1E-5
end
end
def test_coordinates_present
assert Geocoder::Calculations.coordinates_present?(3.23)
assert !Geocoder::Calculations.coordinates_present?(nil)
assert !Geocoder::Calculations.coordinates_present?(Geocoder::Calculations::NAN)
assert !Geocoder::Calculations.coordinates_present?(3.23, nil)
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment