Sha256: 59dc3f3ce155f0af346f2d2d6a7e04c93e26dc09f9ea482174cc57e891613695

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

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

      predicates(Predicates)
    end

    required(:summary, Types::StrippedString).filled
    required(:full_time, Types::Strict::Bool).filled
    required(:position_duration_code, Types::PositionDuration).filled
    required(:start_date, Types::StrippedString).filled(:yyyy_mm_dd?)

    optional(:end_date, Types::StrippedString)
  end

  class Schedule < Model
    DURATION_MAP = {
      regular: -1,
      from_0_days_to_10_days: 8,
      from_11_days_to_3_months: 7,
      from_june_to_august: 4, # summer months (June to August)
      from_3_months_to_6_months: 3,
      from_6_months_to_longer: 2
    }.freeze

    def initialize(hash)
      data = hash.dup
      duration_code = duration_code(data[:start_date], data[:end_date])
      data[:position_duration_code] = duration_code
      data[:full_time] = duration_code == -1
      super(ScheduleSchema.call(data))
    end

    private

    def duration_code(start_date, end_date)
      return DURATION_MAP.fetch(:regular) if end_date.nil?

      days = end_date - start_date

      return DURATION_MAP.fetch(:from_0_days_to_10_days) if days <= 10
      return DURATION_MAP.fetch(:from_11_days_to_3_months) if days > 10 && days < 92
      return DURATION_MAP.fetch(:from_june_to_august) if summer_job?(start_date, end_date)
      return DURATION_MAP.fetch(:from_3_months_to_6_months) if days >= 92 && days < 183

      DURATION_MAP.fetch(:from_6_months_to_longer)
    end

    def summer_job?(start_date, end_date)
      start_date.month == 6 && start_date.day == 1 &&
        end_date.month == 8 && end_date.day == 31
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
arbetsformedlingen-0.5.0 lib/arbetsformedlingen/models/schedule.rb