From 565de2aa58f53e5986042bb8d26ad2c93b63a468 Mon Sep 17 00:00:00 2001 From: Alex Reisner <alex@alexreisner.com> Date: Mon, 21 Mar 2011 18:19:00 -0400 Subject: [PATCH] Add deprecation warnings. ...for Result instance methods mistakenly called on an array. --- lib/geocoder.rb | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/geocoder.rb b/lib/geocoder.rb index 3920fa57..b9306ccd 100644 --- a/lib/geocoder.rb +++ b/lib/geocoder.rb @@ -10,9 +10,27 @@ module Geocoder # Search for information about an address or a set of coordinates. # def search(*args) - return [] if blank_query?(args[0]) - ip = (args.size == 1 and ip_address?(args.first)) - lookup(ip).search(*args) + if blank_query?(args[0]) + results = [] + else + ip = (args.size == 1 and ip_address?(args.first)) + results = lookup(ip).search(*args) + end + results.instance_eval do + def warn_search_deprecation(attr) + warn "DEPRECATION WARNING: Geocoder.search now returns an array of Geocoder::Result objects. " + + "Calling '%s' directly on the returned array will cause an exception in Geocoder v1.0." % attr + end + + def coordinates; warn_search_deprecation('coordinates'); first.coordinates if first; end + def latitude; warn_search_deprecation('latitude'); first.latitude if first; end + def longitude; warn_search_deprecation('longitude'); first.longitude if first; end + def address; warn_search_deprecation('address'); first.address if first; end + def city; warn_search_deprecation('city'); first.city if first; end + def country; warn_search_deprecation('country'); first.country if first; end + def country_code; warn_search_deprecation('country_code'); first.country_code if first; end + end + return results end ## -- GitLab