Sha256: 8a36fd138802c1017d6bbac9aa267fecbdeb4a181783ecfc0b71587e86da48b8

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

module CompanyNumber
  class Number
    attr_reader :company_number, :country_code, :metadata

    def initialize(company_number, country_code = nil)
      check_param_type(company_number, [String])
      check_param_type(country_code, [NilClass, Symbol, String])

      @company_number = company_number
      @country_code   = country_code&.downcase&.to_sym
      @metadata       = CompanyNumber.dictionary[@country_code] || {}
    end

    def to_h
      {
        company_number: @company_number,
        country_code: @country_code,
        metadata: @metadata
      }
    end

    def to_s
      "#{@company_number} #{@country_code}".strip
    end

    def ==(other)
      self.class == other.class && other.to_s == to_s
    end

    def valid?
      !valid_country? || valid_for_country?(@country_code)
    end

    def valid_country?
      CompanyNumber.dictionary.keys.include?(@country_code)
    end

    def valid_for_country?(country_code)
      check_param_type(country_code, [Symbol])

      !!(@company_number =~ country_code_regexp(country_code))
    end

    def valid_countries
      return [] if !valid_country? && @country_code

      CompanyNumber
        .dictionary
        .keys
        .select { |country_code| valid_for_country?(country_code) }
    end

    private

    def country_code_regexp(country_code)
      regexp = CompanyNumber.dictionary.dig(country_code, :regexp)

      Regexp.new(regexp) unless regexp.nil?
    end

    def check_param_type(param, expected_classes = [])
      return if expected_classes.include?(param.class)

      raise ArgumentError,
            "Expect class of #{param} to be #{expected_classes.join(', ')}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
company_number-0.1.1 lib/company_number/number.rb