Sha256: 14fcdb7b5c21f46fd9458ab8801df0b829dd876828d331b3ac042985b01c69a3

Contents?: true

Size: 686 Bytes

Versions: 7

Compression:

Stored size: 686 Bytes

Contents

require 'csv'

module Arbetsformedlingen
  class OccupationCode
    CODE_MAP = CSV.read(
      File.expand_path('../../../../data/occupation-codes.csv', __FILE__)
    ).to_h.invert.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.map { |name, _code| [name, name] }
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
arbetsformedlingen-0.5.0 lib/arbetsformedlingen/codes/occupation_code.rb
arbetsformedlingen-0.4.1 lib/arbetsformedlingen/codes/occupation_code.rb
arbetsformedlingen-0.4.0 lib/arbetsformedlingen/codes/occupation_code.rb
arbetsformedlingen-0.3.0 lib/arbetsformedlingen/codes/occupation_code.rb
arbetsformedlingen-0.2.0 lib/arbetsformedlingen/codes/occupation_code.rb
arbetsformedlingen-0.1.2 lib/arbetsformedlingen/codes/occupation_code.rb
arbetsformedlingen-0.1.1 lib/arbetsformedlingen/codes/occupation_code.rb