Module: NormalizeCountry
- Defined in:
- lib/normalize_country.rb
Defined Under Namespace
Classes: Country
Constant Summary collapse
- VERSION =
"0.3.3"- Countries =
{}
Class Attribute Summary collapse
Class Method Summary collapse
- .convert(name, options = {}) ⇒ Object
- .formats ⇒ Object
- .to_a(name = to) ⇒ Object
- .to_h(key, options = {}) ⇒ Object
Class Attribute Details
.to ⇒ Object
12 13 14 |
# File 'lib/normalize_country.rb', line 12 def to @to ||= :iso_name end |
Class Method Details
.convert(name, options = {}) ⇒ Object
20 21 22 23 24 |
# File 'lib/normalize_country.rb', line 20 def convert(name, = {}) country = country_for(name) return unless country country[ [:to] || to ] end |
.formats ⇒ Object
16 17 18 |
# 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_a(name = to) ⇒ Object
26 27 28 29 |
# 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 = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/normalize_country.rb', line 31 def to_h(key, = {}) value = [: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 |