Class: Arbetsformedlingen::Position

Inherits:
Model
  • Object
show all
Defined in:
lib/arbetsformedlingen/models/position.rb

Instance Attribute Summary

Attributes inherited from Model

#schema

Instance Method Summary collapse

Methods inherited from Model

#errors, from_schema, #valid?

Constructor Details

#initialize(attributes:, company:, schedule:, salary:, qualifications:, application_method:) ⇒ Position

rubocop:disable Metrics/LineLength



25
26
27
28
29
30
31
32
33
34
# File 'lib/arbetsformedlingen/models/position.rb', line 25

def initialize(attributes:, company:, schedule:, salary:, qualifications:, application_method:) # rubocop:disable Metrics/LineLength
  hash = attributes
  @company = company
  @schedule = schedule
  @salary = salary
  @qualifications = qualifications
  @application_method = application_method

  super(PositionSchema.call(hash))
end

Instance Method Details

#build_full_address(address) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/arbetsformedlingen/models/position.rb', line 48

def build_full_address(address)
  return unless address.key?(:street) || address.key?(:zip) || address.key?(:city)

  [
    address.fetch(:street),
    address.fetch(:zip),
    address[:city],
  ].compact.join(', ')
end

#to_hObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/arbetsformedlingen/models/position.rb', line 36

def to_h
  hash = super
  hash[:company] = @company.to_h
  hash[:schedule] = @schedule.to_h
  hash[:salary] = @salary.to_h
  hash[:qualifications] = @qualifications.map(&:to_h)
  hash[:application_method] = @application_method.to_h
  full_address = build_full_address(hash.fetch(:address))
  hash[:address][:full_address] = full_address if full_address
  hash
end