Sha256: fbbdec34ca841332c74b7fcfe002af8a8ba968deee5f2065b47b34d3dcb0553d
Contents?: true
Size: 1.68 KB
Versions: 2
Compression:
Stored size: 1.68 KB
Contents
# frozen_string_literal: true module MrCommon # Wrapper doing common tasks with the Carmen gem class Country class << self # @param top [Array<String>] a list of country codes that should be # promoted to the top of the list. # @return [Array<Array<String, String>>] an array of label-value pairs for # building country select options. def all(top = []) all_countries = label_value_pairs.dup top.reverse_each do |alpha2| all_countries = promote_one(all_countries, alpha2) end all_countries end # @return [Array<String>] two letter country codes in alphabetical order def codes Carmen::Country.all.map(&:code).sort end # @return [Array<String>] country names in alphabetical order def names Carmen::Country.all.map(&:name).sort end # @param code [String] a two letter country code # @return [String] the name of the country for that code def name_for(code) Carmen::Country.alpha_2_coded(code).name end private def promote_one(collection, promoted_alpha2) if found_index = alpha_2_index(collection, promoted_alpha2) collection.unshift(collection.delete_at(found_index)) else collection end end def alpha_2_index(collection, alpha_2) collection.find_index do |name_code_pair| name_code_pair.second == alpha_2.to_s.upcase end end def label_value_pairs @label_value_pairs ||= Carmen::Country.all.sort_by(&:name).map do |c| [c.name, c.code] end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
mr_common-2.1.0 | app/models/mr_common/country.rb |
mr_common-2.0.0 | app/models/mr_common/country.rb |