class GouvCalendarCompilator::France::NationalDaysOffDataCompilator

Data compiler class for national days off

Public Class Methods

new() click to toggle source

Initializer for France National Days off Compiler class.

# File lib/gouv_calendar_compilator/data_compilators/france/national_days_off_data_compilator.rb, line 13
def initialize
  @data_fetcher = ::GouvCalendarCompilator::DataFetcher.new
end

Public Instance Methods

compile(calendar) click to toggle source

This is the method you want to call to compile the sources into a proper dataset. It returns a ruby Array of Hashes

@param calendar Array<Hash> : previously formatted has of School holidays

# File lib/gouv_calendar_compilator/data_compilators/france/national_days_off_data_compilator.rb, line 21
def compile(calendar)
  @data_fetcher.fetch_national_days_off_data.each do |zone_specific_ndo_data|
    calendar = ndo_parse_data_region(
      zone_specific_ndo_data[:data],
      zone_specific_ndo_data[:zones],
      ::GouvCalendarCompilator::DataCompilator.sort_calendar_data(calendar)
    )
  end

  calendar
end

Private Instance Methods

determines_what_to_keep(splitted_vacation, period, day_off_date) click to toggle source

Determines what parts of the splitted vacation to keep to replace current vacation

  • if vacation is 1 day and day off is the first and the last day of it, then keep only the day_off

  • if day off is first day of holiday, then do not keep the first part of the splitted vacation

  • if day off is last day of holiday, then do not keep the last part of the splitted vacation

# File lib/gouv_calendar_compilator/data_compilators/france/national_days_off_data_compilator.rb, line 108
def determines_what_to_keep(splitted_vacation, period, day_off_date)
  # inserting national day off
  elements_to_add = [splitted_vacation[1]]
  return elements_to_add if period[:start] == day_off_date.to_s && period[:end] == (day_off_date + 1).to_s

  if period[:start] == day_off_date.to_s
    # inserting last part of the splitted vacation
    elements_to_add << splitted_vacation[2]
  elsif period[:end] == (day_off_date + 1).to_s
    # inserting first part of the splitted vacation
    elements_to_add << splitted_vacation[0]
  else
    # inserting first & last part of the splitted vacation
    elements_to_add << splitted_vacation[0]
    elements_to_add << splitted_vacation[2]
  end

  elements_to_add
end
ndo_insert_day_off_date_in_calendar(day_off_date, zones, calendar) click to toggle source

checks if the given date already exists in the generated calendar for the given zones

# File lib/gouv_calendar_compilator/data_compilators/france/national_days_off_data_compilator.rb, line 69
def ndo_insert_day_off_date_in_calendar(day_off_date, zones, calendar)
  zones.each do |zone_name|
    included_in_vacation = false
    calendar[zone_name].each_with_index do |vacation_period, index|
      start_date = ::Date.parse(vacation_period[:start])
      end_date = ::Date.parse(vacation_period[:end])
      date_range = (start_date...end_date)
      unless date_range.include?(day_off_date) &&
             vacation_period[:coefficient] != ::GouvCalendarCompilator::NATIONAL_DAY_OFF

        next
      end

      included_in_vacation = true
      splitted_vacation = ndo_split_holidays_period_insert_day_off(calendar[zone_name][index], day_off_date)

      keeped_periods = determines_what_to_keep(splitted_vacation, calendar[zone_name][index], day_off_date)
      # remove the current vacation before adding the correct parts of the splitted_vacation
      calendar[zone_name].delete(calendar[zone_name][index])
      calendar[zone_name].push(*keeped_periods)
    end

    # if day off not in the middle of a vacation period, simply insert it inside the dataset
    next if included_in_vacation

    calendar[zone_name] << {
      start: day_off_date.to_s,
      end: (day_off_date + 1).to_s,
      coefficient: ::GouvCalendarCompilator::NATIONAL_DAY_OFF
    }
  end
  calendar
end
ndo_parse_data_region(ndo_dataset, targeted_zones, existing_calendar) click to toggle source

NATIONAL DAYS OFF DEDICATED FUNCTIONS

# File lib/gouv_calendar_compilator/data_compilators/france/national_days_off_data_compilator.rb, line 36
def ndo_parse_data_region(ndo_dataset, targeted_zones, existing_calendar)
  ndo_dataset.each_key do |day_off|
    existing_calendar = ndo_insert_day_off_date_in_calendar(
      ::Date.parse(day_off),
      targeted_zones,
      existing_calendar
    )
  end
  existing_calendar
end
ndo_split_holidays_period_insert_day_off(holidays_period, day_off_date) click to toggle source

Splits the periods of time when a national day off falls into a school holiday period

# File lib/gouv_calendar_compilator/data_compilators/france/national_days_off_data_compilator.rb, line 48
def ndo_split_holidays_period_insert_day_off(holidays_period, day_off_date)
  [
    {
      start: holidays_period[:start],
      end: day_off_date.to_s,
      coefficient: ::GouvCalendarCompilator::SCHOOL_HOLIDAYS
    },
    {
      start: day_off_date.to_s,
      end: (day_off_date + 1).to_s,
      coefficient: ::GouvCalendarCompilator::NATIONAL_DAY_OFF
    },
    {
      start: (day_off_date + 1).to_s,
      end: ::Date.parse(holidays_period[:end]).to_s,
      coefficient: ::GouvCalendarCompilator::SCHOOL_HOLIDAYS
    }
  ]
end