Module: ItaxCode::Utils
- Defined in:
- lib/itax_code/utils.rb
Class Method Summary collapse
- .blank?(obj) ⇒ Boolean
- .cities ⇒ Object
- .countries ⇒ Object
- .encode_cin(code) ⇒ Object
- .extract_consonants(str) ⇒ Object
- .extract_vowels(str) ⇒ Object
- .months ⇒ Object
- .omocodia_decode(val) ⇒ Object
- .omocodia_encode(val) ⇒ Object
- .omocodia_indexes ⇒ Object
- .omocodia_indexes_combos ⇒ Object
- .present?(obj) ⇒ Boolean
- .slugged(string) ⇒ Object
- .tax_code_sections_regex ⇒ Object
- .transliterate(string) ⇒ Object
Class Method Details
.blank?(obj) ⇒ Boolean
9 10 11 |
# File 'lib/itax_code/utils.rb', line 9 def blank?(obj) obj.respond_to?(:empty?) ? !!obj.empty? : !obj end |
.cities ⇒ Object
76 77 78 |
# File 'lib/itax_code/utils.rb', line 76 def cities CSV.foreach("#{__dir__}/data/cities.csv", headers: true) end |
.countries ⇒ Object
80 81 82 |
# File 'lib/itax_code/utils.rb', line 80 def countries CSV.foreach("#{__dir__}/data/countries.csv", headers: true) end |
.encode_cin(code) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/itax_code/utils.rb', line 66 def encode_cin(code) cin_tot = 0 code[0..14].each_char.with_index do |char, i| cin_tot += cin[char][((i + 1) % 2).to_i] end cin_remainders[cin_tot % 26] end |
.extract_consonants(str) ⇒ Object
58 59 60 |
# File 'lib/itax_code/utils.rb', line 58 def extract_consonants(str) str.select { |c| consonants.include? c }.join end |
.extract_vowels(str) ⇒ Object
62 63 64 |
# File 'lib/itax_code/utils.rb', line 62 def extract_vowels(str) str.select { |c| vowels.include? c }.join end |
.months ⇒ Object
36 37 38 |
# File 'lib/itax_code/utils.rb', line 36 def months %w[A B C D E H L M P R S T] end |
.omocodia_decode(val) ⇒ Object
54 55 56 |
# File 'lib/itax_code/utils.rb', line 54 def omocodia_decode(val) val.tr omocodia_letters, omocodia_digits end |
.omocodia_encode(val) ⇒ Object
50 51 52 |
# File 'lib/itax_code/utils.rb', line 50 def omocodia_encode(val) val.tr omocodia_digits, omocodia_letters end |
.omocodia_indexes ⇒ Object
40 41 42 |
# File 'lib/itax_code/utils.rb', line 40 def omocodia_indexes [6, 7, 9, 10, 12, 13, 14].reverse end |
.omocodia_indexes_combos ⇒ Object
44 45 46 47 48 |
# File 'lib/itax_code/utils.rb', line 44 def omocodia_indexes_combos (1..omocodia_indexes.size).flat_map do |index| omocodia_indexes.combination(index).to_a end end |
.present?(obj) ⇒ Boolean
13 14 15 |
# File 'lib/itax_code/utils.rb', line 13 def present?(obj) !blank?(obj) end |
.slugged(string) ⇒ Object
17 18 19 20 |
# File 'lib/itax_code/utils.rb', line 17 def slugged(string) transliterated = transliterate(string.downcase.strip) transliterated.gsub(/[^\w-]+/, "-").gsub(/-{2,}/, "-").gsub(/^-+|-+$/, "") end |
.tax_code_sections_regex ⇒ Object
29 30 31 32 33 34 |
# File 'lib/itax_code/utils.rb', line 29 def tax_code_sections_regex /^([A-Z]{3})([A-Z]{3}) (([A-Z\d]{2})([ABCDEHLMPRST]{1})([A-Z\d]{2})) ([A-Z]{1}[A-Z\d]{3}) ([A-Z]{1})$/x end |
.transliterate(string) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/itax_code/utils.rb', line 22 def transliterate(string) return string if string.ascii_only? transliterator = Transliterator.new transliterator.transliterate(string.unicode_normalize(:nfc)) end |