Skip to content

Commit

Permalink
refactor!: remove Validator and merge its logic into Parser (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
matteoredz authored Oct 1, 2023
1 parent 997f8b0 commit 348bdd0
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 85 deletions.
6 changes: 4 additions & 2 deletions lib/itax_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require "itax_code/utils"
require "itax_code/encoder"
require "itax_code/parser"
require "itax_code/validator"

module ItaxCode
Error = Class.new(StandardError)
Expand Down Expand Up @@ -42,7 +41,10 @@ def decode(tax_code)
#
# @return [Boolean]
def valid?(tax_code)
Validator.new(tax_code).valid?
decode(tax_code)
true
rescue Parser::Error
false
end
end
end
10 changes: 6 additions & 4 deletions lib/itax_code/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ class Parser
InvalidControlInternalNumberError = Class.new(Error)
InvalidTaxCodeError = Class.new(Error)

LENGTH = 16

# @param [String] tax_code
# @param [Utils] utils
def initialize(tax_code, utils = Utils.new)
@tax_code = tax_code&.upcase
raise NoTaxCodeError if @tax_code.blank?
raise InvalidTaxCodeError unless Validator.standard_length?(@tax_code)
@utils = utils

@utils = utils
raise InvalidControlInternalNumberError if raw[:cin] != @utils.encode_cin(tax_code)
raise NoTaxCodeError if @tax_code.blank?
raise InvalidTaxCodeError if @tax_code.length != LENGTH
raise InvalidControlInternalNumberError if raw[:cin] != @utils.encode_cin(@tax_code)
end

# Decodes the tax code into its components.
Expand Down
47 changes: 0 additions & 47 deletions lib/itax_code/validator.rb

This file was deleted.

31 changes: 0 additions & 31 deletions test/itax_code/validator_test.rb

This file was deleted.

6 changes: 5 additions & 1 deletion test/itax_code_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ class ItaxCodeTest < ActiveSupport::TestCase
assert_instance_of Hash, klass.decode("RSSMRA80A41F205B")
end

test "#valid?" do
test "#valid? is truthy when the tax code can be decoded" do
assert klass.valid?("RSSMRA80A10F205Z")
end

test "#valid? is falsy when the parser cannot decode the given tax code" do
assert_not klass.valid?("WRONG")
end

private

def klass
Expand Down

0 comments on commit 348bdd0

Please sign in to comment.