Sha256: 66cf5c35bf4100638aa87d20dd4a892746b5bde7551d2b991c3b6f274f6b044a

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module CompanyNumber
  class Dictionary
    attr_reader :country_codes_metadata, :default_hash

    def self.default_dictionary_path
      File.join(File.dirname(__FILE__), '../../config/dictionary.yml')
    end

    def initialize(country_codes_metadata = {})
      @country_codes_metadata = country_codes_metadata
      @default_hash           = load_default_hash

      validate_country_codes_metadata
    end

    def values
      @_values ||= fetch_values
    end

    private

    def fetch_values
      transformed_hash =
        @default_hash.merge(@country_codes_metadata) do |country_code, _, value|
          @default_hash[country_code.downcase].merge(value)
        end

      transformed_hash.reject do |country_code, _metadata|
        CompanyNumber.configuration.excluded_countries.include?(country_code)
      end
    end

    def load_default_hash
      YAML.safe_load(
        File.read(self.class.default_dictionary_path),
        symbolize_names: true
      )
    end

    def validate_country_codes_metadata
      Validation.check_object_class(@country_codes_metadata, [Hash])

      @country_codes_metadata.each do |country_code, metadata|
        Validation.check_string_format(country_code, /^[A-Za-z]{2}$/)
        Validation.check_country_code_metadata(metadata)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
company_number-0.1.2 lib/company_number/dictionary.rb