Module: CountryStateSelect::CountryMetadata
- Defined in:
- lib/country_state_select/country_metadata.rb
Overview
Optional label decoration: flag emoji and/or international dial code. Both read from a small bundled table so the gem stays dependency-free; a country missing from the table (rare, mostly disputed territories) simply renders without the decoration instead of raising.
Constant Summary collapse
- DIAL_CODES =
YAML.safe_load_file( File.('data/dial_codes.yml', __dir__) ).freeze
Class Method Summary collapse
- .decorate(code, name) ⇒ Object
-
.flag(code) ⇒ Object
Converts a two-letter ISO code to its flag emoji via the Unicode "Regional Indicator Symbol" trick (each letter A-Z maps to U+1F1E6..).
Class Method Details
.decorate(code, name) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/country_state_select/country_metadata.rb', line 13 def self.decorate(code, name) label = name label = "#{flag(code)} #{label}" if CountryStateSelect.configuration.flags && flag(code) label = "#{label} (+#{DIAL_CODES[code.to_s]})" if CountryStateSelect.configuration.dial_codes && DIAL_CODES[code.to_s] label end |
.flag(code) ⇒ Object
Converts a two-letter ISO code to its flag emoji via the Unicode "Regional Indicator Symbol" trick (each letter A-Z maps to U+1F1E6..).
22 23 24 25 26 27 |
# File 'lib/country_state_select/country_metadata.rb', line 22 def self.flag(code) chars = code.to_s.upcase.chars return nil unless chars.length == 2 && chars.all? { |c| c.match?(/[A-Z]/) } chars.map { |c| [0x1F1E6 + (c.ord - 'A'.ord)].pack('U') }.join end |