From 3636c08e8ab359e712c550a071de8e73b2c75354 Mon Sep 17 00:00:00 2001 From: Dave Worth Date: Sat, 4 May 2013 23:13:23 -0400 Subject: [PATCH] extract city class for #city_parts --- lib/indirizzo/address.rb | 11 +---------- lib/indirizzo/city.rb | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 lib/indirizzo/city.rb diff --git a/lib/indirizzo/address.rb b/lib/indirizzo/address.rb index 77874a6..30fb016 100644 --- a/lib/indirizzo/address.rb +++ b/lib/indirizzo/address.rb @@ -241,16 +241,7 @@ def remove_noise_words(strings) end def city_parts - strings = [] - @city.map do |string| - tokens = string.split(" ") - strings |= (0...tokens.length).to_a.reverse.map {|i| - (i...tokens.length).map {|j| tokens[i..j].join(" ")}}.flatten - end - # Don't return strings that consist solely of abbreviations. - # NOTE: Is this a micro-optimization that has edge cases that will break? - # Answer: Yes, it breaks on "Prairie" - strings.reject { |s| Std_Abbr.key?(s) }.uniq + City.city_parts(@city) end def city= (strings) diff --git a/lib/indirizzo/city.rb b/lib/indirizzo/city.rb new file mode 100644 index 0000000..a70717f --- /dev/null +++ b/lib/indirizzo/city.rb @@ -0,0 +1,17 @@ +module Indirizzo + class City + def self.city_parts(city) + strings = [] + city.map do |string| + tokens = string.split(" ") + strings |= (0...tokens.length).to_a.reverse.map do |i| + (i...tokens.length).map {|j| tokens[i..j].join(" ")} + end.flatten + end + # Don't return strings that consist solely of abbreviations. + # NOTE: Is this a micro-optimization that has edge cases that will break? + # Answer: Yes, it breaks on "Prairie" + strings.reject { |s| Std_Abbr.key?(s) }.uniq + end + end +end