Class: SemesterPeriod

Inherits:
Sequel::Model
  • Object
show all
Defined in:
app/models/semester_period.rb

Instance Method Summary collapse

Instance Method Details

#first_day_overrideObject


13
14
15
# File 'app/models/semester_period.rb', line 13

def first_day_override
  Day.from_numeric(super) if super
end

#first_day_override=(new_override) ⇒ Object


17
18
19
20
21
22
23
# File 'app/models/semester_period.rb', line 17

def first_day_override=(new_override)
  if new_override
    super Day.to_numeric(new_override)
  else
    super
  end
end

#include?(date) ⇒ Boolean

Returns whether the given date is inside this period.

Returns:

  • (Boolean)

    whether the given date is inside this period.


40
41
42
# File 'app/models/semester_period.rb', line 40

def include?(date)
  date >= starts_at && date <= ends_at
end

#regular?Boolean

Returns:

  • (Boolean)

35
36
37
# File 'app/models/semester_period.rb', line 35

def regular?
  !irregular
end

#teaching?Boolean

Returns:

  • (Boolean)

9
10
11
# File 'app/models/semester_period.rb', line 9

def teaching?
  type == 'teaching'
end

#validateObject


25
26
27
28
29
30
31
32
33
# File 'app/models/semester_period.rb', line 25

def validate
  super
  if starts_at > ends_at
    errors.add(:starts_at, 'cannot be after ending')
  end
  if teaching? && first_week_parity.nil?
    errors.add(:first_week_parity, 'cannot be null for teaching period')
  end
end

#week_parity(date) ⇒ String?

Resolves a teaching week parity for the given date within this semester period. If this period is not a teaching period, the method returns nil.

The calculation is based on difference between the period's start date and and the given date, shifted by the period's first_week_parity.

Parameters:

  • date (Date)

Returns:

  • (String, nil)

    'even', 'odd', or nil

Raises:

  • (ArgumentError)

    if the date's week is not within this period.


55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/semester_period.rb', line 55

def week_parity(date)
  return unless teaching?

  if date < starts_at.start_of_week || date > ends_at.end_of_week then
    fail ArgumentError, "The date's week is not within this period"
  end

  weeks_since_start = ((date.start_of_week - starts_at.start_of_week) / 7).abs.floor
  first_parity = first_week_parity == 'even' ? 0 : 1
  parity = (weeks_since_start + first_parity) % 2

  parity == 0 ? 'even' : 'odd'
end