Sha256: e8cd253051bd17a690b273bde5ca298ffd24ae8767bf45045a61d82ab75197eb

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

require 'builder'

module Arbetsformedlingen
  PacketSchema = Dry::Validation.Form do
    configure do
      config.type_specs = true
      config.messages_file = File.expand_path('../../../../config/locales/errors.yml', __FILE__)

      predicates(Predicates)
    end

    required(:active, Types::Bool).filled
    required(:job_id, Types::StrippedString).filled
    required(:id, Types::StrippedString).filled
    required(:number_to_fill, Types::Int).filled(gt?: 0)
    required(:occupation, Types::Occupation).filled(:occupation_code?)
  end

  class Packet < Model
    DEFAULT_PACKET_ID = 1

    def initialize(attributes:, publication:, document:, position:)
      hash = attributes
      @publication = publication
      @document = document
      @position = position
      id = hash.fetch(:id, DEFAULT_PACKET_ID)
      super(PacketSchema.call(hash.merge(id: id)))
    end

    def to_h
      hash = super
      hash[:publication] = @publication.to_h
      hash[:document] = @document.to_h
      hash[:position] = @position.to_h
      hash
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
arbetsformedlingen-0.2.0 lib/arbetsformedlingen/models/packet.rb
arbetsformedlingen-0.1.2 lib/arbetsformedlingen/models/packet.rb
arbetsformedlingen-0.1.1 lib/arbetsformedlingen/models/packet.rb