Permalink
Browse files

Added SwissMatch::ZipCodes#autocompleted_names and #names_for_select.

  • Loading branch information...
awd-switzerland committed Apr 4, 2013
1 parent 9b0dddd commit 5682e5300e080808e39817b8c908277bccea7c1f
Showing with 28 additions and 0 deletions.
  1. +28 −0 lib/swissmatch/zipcodes.rb
View
@@ -3,6 +3,7 @@
require 'autocompletion'
+require 'unicode'
@@ -175,6 +176,33 @@ def autocomplete(string)
ZipCodes.new(@autocomplete.complete(*words))
end
+ # @return [Array<String>]
+ # An array of ZipCode names which match the given string in an autocompletion.
+ # Sorted alphabetically (Umlaut-aware)
+ def autocompleted_names(string)
+ name_dc = Unicode.downcase(name)
+ base = autocomplete(name)
+ names = base.flat_map { |zip_code|
+ zip_code.reverse_name_transliteration_map.select { |transliterated_name, real_names|
+ Unicode.downcase(transliterated_name[0, len]) == name_dc
+ }.values
+ end
+
+ names.uniq.sort(&Unicode.method(:strcmp))
+ end
+
+ # @return [Array<String>]
+ # An array of ZipCode names suitable for presentation of a select.
+ def names_for_select(language=nil)
+ if language
+ names = base.flat_map { |zip_code| [zip_code.name, zip_code.suggested_name(I18n.language)] }
+ else
+ names = base.map(&:name)
+ end
+
+ names.uniq.sort(&Unicode.method(:strcmp))
+ end
+
# @return [SwissMatch::ZipCodes]
# A SwissMatch::ZipCodes collection with zip codes of type 10 and 20.
def residential

0 comments on commit 5682e53

Please sign in to comment.