Sha256: f81ea79e060cf553f4add6e4ee3b40da328afb1a07b195adc2ca20a282abf5a6

Contents?: true

Size: 1.27 KB

Versions: 6

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module ItaxCode
  class Omocode
    attr_reader :tax_code, :utils

    # Creates a new instance for a given tax_code.
    #
    # @param [String] tax_code
    # @param [Utils]  utils
    def initialize(tax_code, utils = Utils.new)
      @tax_code = tax_code
      @utils    = utils
    end

    # Computes the omocodes from a given tax_code by first identifying the original
    # tax_code and then appending all the omocodes.
    #
    # @return [Array]
    def omocodes
      [original_omocode] + utils.omocodia_indexes_combos.map do |combo|
        omocode(original_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
      @original_omocode ||= omocode(
        tax_code, utils.omocodia_indexes, ->(char) { utils.omocodia_decode(char) }
      )
    end

    private

      def omocode(code, indexes, translation)
        chars = code[0..14].chars

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

        omocode = chars.join
        omocode + utils.encode_cin(omocode)
      end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
itax_code-2.0.3 lib/itax_code/omocode.rb
itax_code-2.0.2 lib/itax_code/omocode.rb
itax_code-2.0.1 lib/itax_code/omocode.rb
itax_code-2.0.0 lib/itax_code/omocode.rb
itax_code-1.0.1 lib/itax_code/omocode.rb
itax_code-1.0.0 lib/itax_code/omocode.rb