Module: CountryCurrency

Defined in:
lib/country_currency/continent.rb,
lib/country_currency/code.rb,
lib/country_currency/calling.rb,
lib/country_currency/version.rb,
lib/country_currency/iso_4217.rb,
lib/country_currency/iso_3166_1.rb,
lib/country_currency/iso_13616_1.rb,
lib/country_currency/country_currency.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Code, UnknownCodeError

Constant Summary collapse

VERSION =
'1.0.0'.freeze
DEFAULT_FALLBACK =
lambda { |error| raise UnknownCodeError, error }

Class Method Summary collapse

Class Method Details

.allObject



10
11
12
# File 'lib/country_currency/country_currency.rb', line 10

def all
  Code.all
end

.find(code, &fallback) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/country_currency/country_currency.rb', line 16

def find(code, &fallback)
  fallback ||= DEFAULT_FALLBACK
  code     = code.to_s.upcase
  instance = nil

  if code.match?(/^\d{2}$/)
    code = "0#{code}" # Make numeric codes three digits
  end

  instance = if code.match?(/^\d{3}$/)
               all.find { |c| c.numeric == code }
             elsif code.match?(/^[A-Z]{2}$/)
               all.find { |c| c.alpha2 == code }
             elsif code.match?(/^[A-Z]{3}$/)
               all.find { |c| c.alpha3 == code }
             else
               all.find { |c| c.name.casecmp?(code) }
             end

  return fallback.call "No ISO 3166-1 code could be found for '#{code}'." if instance.nil?

  instance
end

.find_by_currency(code, &fallback) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/country_currency/country_currency.rb', line 40

def find_by_currency(code, &fallback)
  # Only use for currency data
  fallback ||= DEFAULT_FALLBACK

  code = code.to_s.upcase
  instance = all.select { |c| c.currency == code }.first

  return fallback.call "No ISO 3166-1 codes could be found searching with currency '#{code}'." if instance.nil?

  instance
end

.for_select(*args) ⇒ Object



6
7
8
# File 'lib/country_currency/country_currency.rb', line 6

def for_select(*args)
  Code.for_select(*args)
end

.search_by_calling(code, &fallback) ⇒ Object

Alias of search_by_calling_code



77
78
79
# File 'lib/country_currency/country_currency.rb', line 77

def search_by_calling(code, &fallback) # Alias of search_by_calling_code
  search_by_calling_code code, &fallback
end

.search_by_calling_code(code, &fallback) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/country_currency/country_currency.rb', line 66

def search_by_calling_code(code, &fallback)
  fallback ||= DEFAULT_FALLBACK

  code = "+#{code.to_i}" # Normalize code
  instances = all.select { |c| c.calling == code }

  return fallback.call "No ISO 3166-1 codes could be found searching with calling code '#{code}'." if instances.empty?

  instances
end

.search_by_currency(code, &fallback) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/country_currency/country_currency.rb', line 81

def search_by_currency(code, &fallback)
  fallback ||= DEFAULT_FALLBACK

  code = code.to_s.upcase
  instances = all.select { |c|
    c.currencies.select { |currency|
      currency != code
    }.empty?
  }

  return fallback.call "No ISO 3166-1 codes could be found searching with currency '#{code}'." if instances.empty?

  instances
end

.search_by_iban(code, &fallback) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/country_currency/country_currency.rb', line 96

def search_by_iban(code, &fallback)
  fallback ||= DEFAULT_FALLBACK

  code = code.to_s.upcase
  instances = all.select { |c| c.iban == code }

  return fallback.call "No ISO 3166-1 codes could be found searching with IBAN '#{code}'." if instances.empty?

  instances
end

.search_by_name(str, &fallback) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/country_currency/country_currency.rb', line 53

def search_by_name(str, &fallback)
  fallback ||= DEFAULT_FALLBACK

  instances = all.select { |c| c.name.to_s.upcase == str.to_s.upcase }
  instances = all.select { |c| c.name.to_s.match(/^#{Regexp.escape(str)}/i) } if instances.empty?
  instances = all.select { |c| c.name.to_s.match(/#{Regexp.escape(str)}/i) } if instances.empty?
  instances = all.select { |c| word_set(c.name) == word_set(str) } if instances.empty?

  return fallback.call "No ISO 3166-1 codes could be found searching with name '#{str}'." if instances.empty?

  instances
end