Class: Arbetsformedlingen::OccupationCode

Inherits:
Object
  • Object
show all
Defined in:
lib/arbetsformedlingen/codes/occupation_code.rb

Constant Summary collapse

DATA_PATH =
File.expand_path('../../../data/occupation-codes.csv', __dir__)
CODE_MAP =
CSV.read(DATA_PATH).to_h.invert.freeze
CODES_MAP_INVERTED =
CODE_MAP.invert.freeze

Class Method Summary collapse

Class Method Details

.normalize(name) ⇒ Object



22
23
24
# File 'lib/arbetsformedlingen/codes/occupation_code.rb', line 22

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

.to_code(name) ⇒ Object



11
12
13
14
15
16
# File 'lib/arbetsformedlingen/codes/occupation_code.rb', line 11

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

.to_form_array(name_only: false) ⇒ Object



26
27
28
29
30
# File 'lib/arbetsformedlingen/codes/occupation_code.rb', line 26

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

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

.valid?(name) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/arbetsformedlingen/codes/occupation_code.rb', line 18

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