From 1c306367588f61b418a0f41f049f73cfd9f94897 Mon Sep 17 00:00:00 2001 From: tunamonster <davmassoud@gmail.com> Date: Wed, 9 Nov 2016 23:58:36 +0630 Subject: [PATCH] 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. --- lib/geocoder/calculations.rb | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/lib/geocoder/calculations.rb b/lib/geocoder/calculations.rb index c1eb9525..ed9f89c2 100644 --- a/lib/geocoder/calculations.rb +++ b/lib/geocoder/calculations.rb @@ -38,12 +38,7 @@ module Geocoder # 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 + args.all? { |a| a.is_a? Numeric and !a.to_f.nan? } end ## @@ -407,13 +402,8 @@ module Geocoder def extract_coordinates(point) case point when Array - if point.size == 2 - lat, lon = point - 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 + if point.size == 2 and coordinates_present?(*point) + return point.map {|coords| coords.to_f} end when String point = Geocoder.coordinates(point) and return point -- GitLab