Class: HrLite::WorkingCalendar

Inherits:
Object
  • Object
show all
Defined in:
app/services/hr_lite/working_calendar.rb

Overview

The single source of day classification. Preloads holidays and the weekend policy once per instance; every other service (leave counting, day status, attendance summary) delegates here.

Instance Method Summary collapse

Constructor Details

#initialize(range) ⇒ WorkingCalendar

Returns a new instance of WorkingCalendar.



6
7
8
9
10
# File 'app/services/hr_lite/working_calendar.rb', line 6

def initialize(range)
  @range = range
  @holiday_dates = Holiday.dates_for(range)
  @policy = Setting.instance.weekend_policy
end

Instance Method Details

#holiday?(date) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'app/services/hr_lite/working_calendar.rb', line 12

def holiday?(date)
  @holiday_dates.include?(date)
end

#weekend?(date) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
# File 'app/services/hr_lite/working_calendar.rb', line 16

def weekend?(date)
  case @policy
  when "sun_only"
    date.sunday?
  when "second_fourth_sat_sun"
    date.sunday? || (date.saturday? && [ 2, 4 ].include?(week_of_month(date)))
  else # sat_sun
    date.saturday? || date.sunday?
  end
end

#working_day?(date) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'app/services/hr_lite/working_calendar.rb', line 27

def working_day?(date)
  !holiday?(date) && !weekend?(date)
end

#working_days_in(range) ⇒ Object



31
32
33
# File 'app/services/hr_lite/working_calendar.rb', line 31

def working_days_in(range)
  range.count { |date| working_day?(date) }
end