Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve omocodes algo #16

Merged
merged 3 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require:
- rubocop-rake

AllCops:
NewCops: enable
TargetRubyVersion: 2.5

Bundler/OrderedGems:
Expand Down Expand Up @@ -42,9 +43,6 @@ Metrics/MethodLength:
Style/Documentation:
Enabled: false

Style/FrozenStringLiteralComment:
Enabled: false

Style/StringLiterals:
Enabled: true
EnforcedStyle: double_quotes
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source "https://rubygems.org"

gemspec
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rake/testtask"

Expand Down
1 change: 1 addition & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "itax_code"
Expand Down
3 changes: 3 additions & 0 deletions itax_code.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative "lib/itax_code/version"

Gem::Specification.new do |spec|
Expand All @@ -23,4 +25,5 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_dependency "activesupport"
spec.metadata["rubygems_mfa_required"] = "true"
end
2 changes: 2 additions & 0 deletions lib/itax_code.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "active_support/all"

require "itax_code/version"
Expand Down
6 changes: 3 additions & 3 deletions lib/itax_code/encoder.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ItaxCode
# Handles the tax code generation logic.
#
Expand Down Expand Up @@ -60,9 +62,7 @@ def encode_name
consonants = utils.extract_consonants chars
vowels = utils.extract_vowels chars

if consonants.length > 3
consonants = consonants.chars.values_at(0, 2..consonants.chars.size).join
end
consonants = consonants.chars.values_at(0, 2..consonants.size).join if consonants.length > 3

"#{consonants[0..2]}#{vowels[0..2]}XXX"[0..2].upcase
end
Expand Down
35 changes: 23 additions & 12 deletions lib/itax_code/omocode.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ItaxCode
class Omocode
attr_reader :tax_code, :utils
Expand All @@ -11,26 +13,35 @@ def initialize(tax_code, utils = Utils.new)
@utils = utils
end

# Computes the omocodes from a given tax_code.
# Computes the omocodes from a given tax_code by first identifying the original
# tax_code and then appending all the omocodes.
#
# @return [Array]
def list
chars = tax_code[0..14].chars
(omocodes(:decode, chars) + omocodes(:encode, chars)).uniq
def omocodes
[original_omocode] + utils.omocodia_indexes_combos.map do |combo|
omocode(combo, ->(char) { utils.omocodia_encode(char) })
end
end

# The original omocode is the one that have all the omocody indexes decoded
# as number, and from which any of its omocodes are generated.
#
# @return [String]
def original_omocode
omocode(utils.omocodia_indexes, ->(char) { utils.omocodia_decode(char) })
end

private

def omocodes(action, chars)
utils.omocodia_subs_indexes.reverse.map do |i|
chars[i] = utils.public_send("omocodia_#{action}".to_sym, chars[i])
omocode(chars)
def omocode(indexes, translation)
chars = tax_code[0..14].chars

indexes.each do |index|
chars[index] = translation.call(chars[index])
end
end

def omocode(chars)
code = chars.join
code + utils.encode_cin(code)
omocode = chars.join
omocode + utils.encode_cin(omocode)
end
end
end
4 changes: 3 additions & 1 deletion lib/itax_code/parser.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "itax_code/omocode"

module ItaxCode
Expand Down Expand Up @@ -30,7 +32,7 @@ def decode
gender: gender,
birthdate: birthdate,
birthplace: birthplace,
omocodes: Omocode.new(tax_code).list,
omocodes: Omocode.new(tax_code).omocodes,
raw: raw
}
end
Expand Down
34 changes: 17 additions & 17 deletions lib/itax_code/utils.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "csv"

module ItaxCode
Expand All @@ -10,9 +12,7 @@ def regex
end

def slugged(str, separator = "-")
str.gsub!(/\s*@\s*/, " at ")
str.gsub!(/\s*&\s*/, " and ")
str.parameterize(separator: separator)
str.gsub(/\s*@\s*/, " at ").gsub(/\s*&\s*/, " and ").parameterize(separator: separator)
end

def months
Expand All @@ -29,23 +29,17 @@ def vowels

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

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

Expand All @@ -68,8 +62,14 @@ def omocodia_letters
omocodia.values.join
end

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

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

def omocodia_encode(val)
Expand Down
2 changes: 2 additions & 0 deletions lib/itax_code/validator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ItaxCode
# Handles the validation logic.
#
Expand Down
4 changes: 3 additions & 1 deletion lib/itax_code/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ItaxCode
VERSION = "0.3.0".freeze
VERSION = "0.3.0"
end
6 changes: 3 additions & 3 deletions rakelib/cities.rake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "csv"
require "net/http"
require "tempfile"
Expand Down Expand Up @@ -25,9 +27,7 @@ namespace :cities do
end
end

File.open("lib/itax_code/data/cities.csv", "w") do |output_file|
output_file.write(output_string)
end
File.write("lib/itax_code/data/cities.csv", output_string)
ensure
tempfile.close
tempfile.unlink
Expand Down
3 changes: 3 additions & 0 deletions test/itax_code/encoder_test.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# frozen_string_literal: true

require "test_helper"

module ItaxCode
class EncoderTest < ActiveSupport::TestCase
test "public interface" do
instance_methods = Encoder.instance_methods - Object.instance_methods

assert_equal %i[encode], instance_methods
end

Expand Down
Loading
Loading