Class: ItaxCode::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/itax_code/utils.rb

Instance Method Summary collapse

Instance Method Details

#blank?(obj) ⇒ Boolean

Returns:

  • (Boolean)


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

def blank?(obj)
  obj.respond_to?(:empty?) ? !!obj.empty? : !obj
end

#cin ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/itax_code/utils.rb', line 47

def cin
  {
    "0" => [0, 1],
    "1" => [1, 0],
    "2" => [2, 5],
    "3" => [3, 7],
    "4" => [4, 9],
    "5" => [5, 13],
    "6" => [6, 15],
    "7" => [7, 17],
    "8" => [8, 19],
    "9" => [9, 21],
    "A" => [0, 1],
    "B" => [1, 0],
    "C" => [2, 5],
    "D" => [3, 7],
    "E" => [4, 9],
    "F" => [5, 13],
    "G" => [6, 15],
    "H" => [7, 17],
    "I" => [8, 19],
    "J" => [9, 21],
    "K" => [10, 2],
    "L" => [11, 4],
    "M" => [12, 18],
    "N" => [13, 20],
    "O" => [14, 11],
    "P" => [15, 3],
    "Q" => [16, 6],
    "R" => [17, 8],
    "S" => [18, 12],
    "T" => [19, 14],
    "U" => [20, 16],
    "V" => [21, 10],
    "W" => [22, 22],
    "X" => [23, 25],
    "Y" => [24, 24],
    "Z" => [25, 23]
  }
end

#cin_remainders ⇒ Object



88
89
90
# File 'lib/itax_code/utils.rb', line 88

def cin_remainders
  ("A".."Z").to_a
end

#cities ⇒ Object



143
144
145
# File 'lib/itax_code/utils.rb', line 143

def cities
  CSV.foreach("#{__dir__}/data/cities.csv", headers: true)
end

#consonants ⇒ Object



39
40
41
# File 'lib/itax_code/utils.rb', line 39

def consonants
  %w[b c d f g h j k l m n p q r s t v w x y z]
end

#countries ⇒ Object



147
148
149
# File 'lib/itax_code/utils.rb', line 147

def countries
  CSV.foreach("#{__dir__}/data/countries.csv", headers: true)
end

#encode_cin(code) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/itax_code/utils.rb', line 133

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



125
126
127
# File 'lib/itax_code/utils.rb', line 125

def extract_consonants(str)
  str.select { |c| consonants.include? c }.join
end

#extract_vowels(str) ⇒ Object



129
130
131
# File 'lib/itax_code/utils.rb', line 129

def extract_vowels(str)
  str.select { |c| vowels.include? c }.join
end

#months ⇒ Object



35
36
37
# File 'lib/itax_code/utils.rb', line 35

def months
  %w[A B C D E H L M P R S T]
end

#omocodia ⇒ Object



92
93
94
95
96
97
# File 'lib/itax_code/utils.rb', line 92

def omocodia
  {
    "0": "L", "1": "M", "2": "N", "3": "P", "4": "Q",
    "5": "R", "6": "S", "7": "T", "8": "U", "9": "V"
  }
end

#omocodia_decode(val) ⇒ Object



121
122
123
# File 'lib/itax_code/utils.rb', line 121

def omocodia_decode(val)
  val.tr omocodia_letters, omocodia_digits
end

#omocodia_digits ⇒ Object



99
100
101
# File 'lib/itax_code/utils.rb', line 99

def omocodia_digits
  omocodia.keys.join
end

#omocodia_encode(val) ⇒ Object



117
118
119
# File 'lib/itax_code/utils.rb', line 117

def omocodia_encode(val)
  val.tr omocodia_digits, omocodia_letters
end

#omocodia_indexes ⇒ Object



107
108
109
# File 'lib/itax_code/utils.rb', line 107

def omocodia_indexes
  [6, 7, 9, 10, 12, 13, 14].reverse
end

#omocodia_indexes_combos ⇒ Object



111
112
113
114
115
# File 'lib/itax_code/utils.rb', line 111

def omocodia_indexes_combos
  (1..omocodia_indexes.size).flat_map do |index|
    omocodia_indexes.combination(index).to_a
  end
end

#omocodia_letters ⇒ Object



103
104
105
# File 'lib/itax_code/utils.rb', line 103

def omocodia_letters
  omocodia.values.join
end

#present?(obj) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/itax_code/utils.rb', line 12

def present?(obj)
  !blank?(obj)
end

#slugged(string) ⇒ Object



16
17
18
19
# File 'lib/itax_code/utils.rb', line 16

def slugged(string)
  transliterated = transliterate(string.downcase.strip)
  transliterated.gsub(/[^\w-]+/, "-").gsub(/-{2,}/, "-").gsub(/^-+|-+$/, "")
end

#tax_code_sections_regex ⇒ Object



28
29
30
31
32
33
# File 'lib/itax_code/utils.rb', line 28

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



21
22
23
24
25
26
# File 'lib/itax_code/utils.rb', line 21

def transliterate(string)
  return string if string.ascii_only?

  transliterator = Transliterator.new
  transliterator.transliterate(string.unicode_normalize(:nfc))
end

#vowels ⇒ Object



43
44
45
# File 'lib/itax_code/utils.rb', line 43

def vowels
  %w[a e i o u]
end