module NormalizeCountry

Constants

Countries
VERSION

Attributes

to[W]

Public Class Methods

convert(name, options = {}) click to toggle source
# File lib/normalize_country.rb, line 20
def convert(name, options = {})
  country = country_for(name)
  return unless country
  country[ options[:to] || to ]
end
formats() click to toggle source
# File lib/normalize_country.rb, line 16
def formats
  @formats ||= Countries.values.flat_map(&:formats).uniq #  format might not be defined for all countries
end
to() click to toggle source
# File lib/normalize_country.rb, line 12
def to
  @to ||= :iso_name
end
to_a(name = to) click to toggle source
# File lib/normalize_country.rb, line 26
def to_a(name = to)
  return [] if Countries.values.find { |c| c[name] }.nil?   # format might not be defined for all countries
  Countries.values.uniq.map { |c| c[name] }.compact.sort { |a, b| a <=> b }
end
to_h(key, options = {}) click to toggle source
# File lib/normalize_country.rb, line 31
def to_h(key, options = {})
  value = options[:to] || to
  countries = Countries.values
  return {} unless [ key, value ].all? { |v| countries.first[v] }

  hash = {}
  countries.each { |c| hash[ c[key] ] = c[value] }
  hash
end

Private Class Methods

country_for(name) click to toggle source
# File lib/normalize_country.rb, line 42
def country_for(name)
  name = name.to_s.downcase.strip.squeeze(" ")
  return if name.empty?
  Countries[name.to_sym]
end