Sha256: 27ecbc01aa7eb014cb75e2112f93dc0e53412f6e18c3224386a121f4a2e4974c

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module Arbetsformedlingen
  CompanySchema = Dry::Validation.Params do
    configure do
      config.type_specs = true
      config.messages_file = File.expand_path('../../../config/locales/errors.yml', __dir__)

      predicates(Predicates)
    end

    required(:name, Types::StrippedString).filled
    required(:cin, Types::CIN).filled(:str?, :cin?)

    optional(:description, Types::StrippedString).filled

    required(:address).schema do
      required(:municipality, Types::Municipality).filled(:municipality?)
      required(:country_code, Types::Country).filled(:str?, :country_code?)
      required(:street, Types::StrippedString).filled
      required(:city, Types::StrippedString).filled
      required(:zip, Types::Zip).filled(:str?, :zip?)
    end
  end

  class Company < Model
    def initialize(hash)
      super(CompanySchema.call(hash))
    end

    def to_h
      hash = super
      address = hash.fetch(:address)
      hash[:address][:full_address] = [
        address.fetch(:street),
        address.fetch(:zip),
        address.fetch(:city),
      ].join(', ')
      hash[:cin_arbetsformedlingen] = cin_arbetsformedlingen(hash.fetch(:cin))
      hash
    end

    # Formats a Company Identification Number the way Arbetsformedlingen likes it
    def cin_arbetsformedlingen(cin)
      String.new(cin.dup).
        delete('-').
        insert(6, '-').
        insert(0, '46-')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
arbetsformedlingen-0.7.0 lib/arbetsformedlingen/models/company.rb
arbetsformedlingen-0.6.0 lib/arbetsformedlingen/models/company.rb