Skip to content
Snippets Groups Projects
Commit 1c306367 authored by tunamonster's avatar tunamonster Committed by Alex Reisner
Browse files

Refactor calculations (#1116)

* Refactor calculations

Refactored calculations::extract_coordinates and calculations::coordinates_present?

coordinates_present?
Checks whether an argument matches validating criteria (numeric, not NaN), rather than returning false when matching invalidating criteria.

Also replaced .to_s == NaN with .to_f..nan? 

extract_coordinates 
Now calls coordinates_present? to validate the array, instead of comparing four booleans and initialising extra vars.

* Removed .map and ternary from coordinates_present?

Updated pull request according to Lime's suggestions.
parent 36778dfc
No related branches found
No related tags found
No related merge requests found
...@@ -38,12 +38,7 @@ module Geocoder ...@@ -38,12 +38,7 @@ module Geocoder
# Returns true if all given arguments are valid latitude/longitude values. # Returns true if all given arguments are valid latitude/longitude values.
# #
def coordinates_present?(*args) def coordinates_present?(*args)
args.each do |a| args.all? { |a| a.is_a? Numeric and !a.to_f.nan? }
# 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 end
## ##
...@@ -407,13 +402,8 @@ module Geocoder ...@@ -407,13 +402,8 @@ module Geocoder
def extract_coordinates(point) def extract_coordinates(point)
case point case point
when Array when Array
if point.size == 2 if point.size == 2 and coordinates_present?(*point)
lat, lon = point return point.map {|coords| coords.to_f}
if !lat.nil? && lat.respond_to?(:to_f) and
!lon.nil? && lon.respond_to?(:to_f)
then
return [ lat.to_f, lon.to_f ]
end
end end
when String when String
point = Geocoder.coordinates(point) and return point point = Geocoder.coordinates(point) and return point
......
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