Sha256: 02eeb68a7e2a31720e8e87f6c4686fff4da709be50552c48236ec0db233c1a11

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 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(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 omocode(indexes, translation)
        chars = tax_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

2 entries across 2 versions & 1 rubygems

Version Path
itax_code-0.4.1 lib/itax_code/omocode.rb
itax_code-0.4.0 lib/itax_code/omocode.rb