Module: NormalizeCountry

Defined in:
lib/normalize_country.rb

Defined Under Namespace

Classes: Country

Constant Summary collapse

VERSION =
"0.2.2"
Countries =
{}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.toObject

Returns the value of attribute to.



8
9
10
# File 'lib/normalize_country.rb', line 8

def to
  @to
end

Class Method Details

.convert(name, options = {}) ⇒ Object



18
19
20
21
22
# File 'lib/normalize_country.rb', line 18

def convert(name, options = {})
  country = country_for(name)
  return unless country
  country[ options[:to] || to ]
end

.formatsObject



14
15
16
# File 'lib/normalize_country.rb', line 14

def formats
  @formats ||= Countries.values.flat_map(&:formats).uniq #  format might not be defined for all countries
end

.to_a(name = to) ⇒ Object



24
25
26
27
# File 'lib/normalize_country.rb', line 24

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 = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/normalize_country.rb', line 29

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