require 'csv'

module Arbetsformedlingen
  class MunicipalityCode
    CODE_MAP = CSV.read(
      File.expand_path('../../../../data/municipality-codes.csv', __FILE__)
    ).to_h.freeze
    CODES_MAP_INVERTED = CODE_MAP.invert.freeze

    def self.to_code(name)
      normalized = normalize(name)
      CODE_MAP.fetch(normalized) do
        normalized if CODES_MAP_INVERTED[normalized]
      end
    end

    def self.valid?(name)
      !to_code(name).nil?
    end

    def self.normalize(name)
      name.to_s.strip
    end

    def self.to_form_array
      CODE_MAP.to_a
    end
  end
end

require 'csv'

module Arbetsformedlingen
  class MunicipalityCode
    CODE_MAP = CSV.read(
      File.expand_path('../../../../data/municipality-codes.csv', __FILE__)
    ).to_h.freeze
    CODES_MAP_INVERTED = CODE_MAP.invert.freeze

    def self.to_code(name)
      normalized = normalize(name)
      CODE_MAP.fetch(normalized) do
        normalized if CODES_MAP_INVERTED[normalized]
      end
    end

    def self.valid?(name)
      !to_code(name).nil?
    end

    def self.normalize(name)
      name.to_s.strip
    end

    def self.to_form_array(name_only: false)
      return CODE_MAP.to_a unless name_only

      CODE_MAP.to_a.map { |name| [name, name] }
    end
  end
end

Add Municipality::to_form_array method name_only option
Add Municipality::to_form_array method name_only option
