Class: SiriusApi::SemesterWeek

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/sirius_api/semester_week.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(semester, periods, start_date, teaching_week = nil) ⇒ SemesterWeek

Returns a new instance of SemesterWeek.

Parameters:

  • semester (FacultySemester)

    (see #semester)

  • periods (Array<SemesterPeriod>)

    (see #periods)

  • start_date (Date)

    (see #start_date)

  • teaching_week (Fixnum, nil) (defaults to: nil)

    (see #teaching_week)


105
106
107
108
109
110
# File 'lib/sirius_api/semester_week.rb', line 105

def initialize(semester, periods, start_date, teaching_week = nil)
  @semester = semester
  @periods = periods.freeze
  @start_date = start_date.freeze
  @teaching_week = teaching_week
end

Instance Attribute Details

#cweekFixnum (readonly)

Returns the calendar week number per ISO 8601 (1-53).

Returns:

  • (Fixnum)

    the calendar week number per ISO 8601 (1-53).


84
# File 'lib/sirius_api/semester_week.rb', line 84

def_delegators :@start_date, :cweek, :cwyear, :year

#cwyearFixnum (readonly)

Returns the calendar week based year per ISO 8601.

Returns:

  • (Fixnum)

    the calendar week based year per ISO 8601.


84
# File 'lib/sirius_api/semester_week.rb', line 84

def_delegators :@start_date, :cweek, :cwyear, :year

#periodsArray<SemesterPeriod> (readonly)

Returns an array of semester periods included or intersecting this week.

Returns:

  • (Array<SemesterPeriod>)

    an array of semester periods included or intersecting this week.


88
89
90
# File 'lib/sirius_api/semester_week.rb', line 88

def periods
  @periods
end

#semesterFacultySemester (readonly)

Returns the semester that contains this week.

Returns:


91
92
93
# File 'lib/sirius_api/semester_week.rb', line 91

def semester
  @semester
end

#start_dateDate (readonly)

Returns the first day (monday) of this week.

Returns:

  • (Date)

    the first day (monday) of this week.


94
95
96
# File 'lib/sirius_api/semester_week.rb', line 94

def start_date
  @start_date
end

#teaching_weekFixnum? (readonly)

Returns an ordinal number of the teaching week within the semester, or nil if this week isn't inside a regular teaching period.

Returns:

  • (Fixnum, nil)

    an ordinal number of the teaching week within the semester, or nil if this week isn't inside a regular teaching period.


98
99
100
# File 'lib/sirius_api/semester_week.rb', line 98

def teaching_week
  @teaching_week
end

Class Method Details

.resolve_weeks(semester, from: nil, to: nil) ⇒ Array<SemesterWeek>

Generates SiriusApi::SemesterWeeks for the given FacultySemester.

Parameters:

  • semester (FacultySemester)
  • from (Date, nil) (defaults to: nil)

    date of the first week to return (will be rounded to the beginning of the week).

  • to (Date, nil) (defaults to: nil)

    date of the last week to retun (will be rounded to the beginning of the week).

Returns:

  • (Array<SemesterWeek>)

    an array of semester weeks sorted by date.


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sirius_api/semester_week.rb', line 25

def resolve_weeks(semester, from: nil, to: nil)
  from_date = from.start_of_week if from
  to_date = to.end_of_week if to

  periods_by_week = semester.semester_periods
    .sort_by(&:starts_at)
    .flat_map { |per| index_period_by_weeks per }
    .then_if(to_date) { |ary| ary.take_while { |date, _| date <= to_date } }
    .map_by { |date, per| [date, per] }

  periods_by_week
    .map { |date, pers| SemesterWeek.new(semester, pers, date) }
    .sort_by(&:start_date)
    .tap { |weeks| add_teaching_week_nums! weeks }
    .drop_while { |week| week.start_date < from_date if from_date }
end

Instance Method Details

#daysArray<SemesterDay>

Returns an array of semester days inside this week, sorted by date from Monday to Sunday. The array may contain nil values.

Returns:

  • (Array<SemesterDay>)

    an array of semester days inside this week, sorted by date from Monday to Sunday. The array may contain nil values.


115
116
117
118
119
120
121
# File 'lib/sirius_api/semester_week.rb', line 115

def days
  @days ||= @start_date.upto(end_date).map { |date|
    @periods.find { |p| p.include? date }.then do |p|
      SemesterDay.new(p, date, teaching_week)
    end
  }.freeze
end

#end_dateDate

Returns the last day (sunday) of this week.

Returns:

  • (Date)

    the last day (sunday) of this week.


124
125
126
# File 'lib/sirius_api/semester_week.rb', line 124

def end_date
  @start_date + 6
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)

157
158
159
160
161
# File 'lib/sirius_api/semester_week.rb', line 157

def eql?(other)
  %w[class periods semester start_date teaching_week].all? do |att|
    other.send(att) == self.send(att)
  end
end

#period_typesArray<Symbol>

Returns a set of regular period types within this week.

Returns:

  • (Array<Symbol>)

    a set of regular period types within this week.


129
130
131
# File 'lib/sirius_api/semester_week.rb', line 129

def period_types
  @period_types ||= @periods.reject(&:irregular?).map(&:type).uniq.freeze
end

#teaching?Boolean

Returns true if this week includes a regular teaching period, false otherwise.

Returns:

  • (Boolean)

    true if this week includes a regular teaching period, false otherwise.


136
137
138
# File 'lib/sirius_api/semester_week.rb', line 136

def teaching?
  period_types.include? 'teaching'
end

#week_parityString?

Returns a parity of this week within a regular teaching semester period. If there's no such period, the method returns nil.

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

Returns:

  • (String, nil)

    'even', 'odd', or nil


149
150
151
152
153
154
155
# File 'lib/sirius_api/semester_week.rb', line 149

def week_parity
  return @week_parity if defined? @week_parity

  @week_parity = @periods
    .find { |p| p.regular? && p.teaching? }
    .then { |p| p.week_parity(@start_date) }
end