Class: UKAcademicCalendar::Term

Inherits:
Object
  • Object
show all
Extended by:
AbstractClass, Forwardable
Defined in:
lib/uk_academic_calendar/term.rb

Overview

Abstract class representing blueprint behaviour for academic terms, bounded by writeable start and end dates

Direct Known Subclasses

AutumnTerm, SpringTerm, SummerTerm

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(academic_year) ⇒ UKAcademicCalendar::AutumnTerm, ...

Parameters:



27
28
29
30
31
32
# File 'lib/uk_academic_calendar/term.rb', line 27

def initialize(academic_year)
  @academic_year = academic_year
  @season = extract_season_from_class_name
  @start_date = nominal_start_date
  @end_date = nominal_end_date
end

Instance Attribute Details

#academic_yearInteger (readonly)

Returns the calendar year within which the term’s academic year starts in.

Returns:

  • (Integer)

    the calendar year within which the term’s academic year starts in



35
36
37
# File 'lib/uk_academic_calendar/term.rb', line 35

def academic_year
  @academic_year
end

#end_dateDate #end_date=(value) ⇒ Date

Sets value to @end_date, first going through validation

Parameters:

  • val (#to_date, nil)

    the end date of the term. If nil given, falls back to #nominal_end_date

Returns:

  • (Date)

    assigned date value

Raises:



51
52
53
# File 'lib/uk_academic_calendar/term.rb', line 51

def end_date
  @end_date
end

#seasonSymbol (readonly)

Returns the name of the season the term spans.

Returns:

  • (Symbol)

    the name of the season the term spans



53
54
55
# File 'lib/uk_academic_calendar/term.rb', line 53

def season
  @season
end

#start_dateDate #start_date=(value) ⇒ Date

Sets value to @start_date, first going through validation

Parameters:

  • val (#to_date, nil)

    the start date of the term. If nil given, falls back to #nominal_start_date

Returns:

  • (Date)

    assigned date value

Raises:



43
44
45
# File 'lib/uk_academic_calendar/term.rb', line 43

def start_date
  @start_date
end

Instance Method Details

#all_datesSortedSet<Date>

Returns sorted set of dates making up the term.

Returns:

  • (SortedSet<Date>)

    sorted set of dates making up the term



94
95
96
# File 'lib/uk_academic_calendar/term.rb', line 94

def all_dates
  SortedSet.new(to_range.to_a)
end

#eachObject

Forwards to Range

See Also:

  • Range#each


23
# File 'lib/uk_academic_calendar/term.rb', line 23

def_delegators :to_range, :include?, :each

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

Returns true if self and other are of the same class, academic_year, and have eql start and end dates. Otherwise returns false.

Parameters:

  • other (#hash)

    any object that responds to ‘#hash`

Returns:

  • (Boolean)

    true if self and other are of the same class, academic_year, and have eql start and end dates. Otherwise returns false.



63
64
65
# File 'lib/uk_academic_calendar/term.rb', line 63

def eql?(other)
  hash == other.hash
end

#hashInteger

Returns the integer hash value for self.

Returns:

  • (Integer)

    the integer hash value for self



56
57
58
# File 'lib/uk_academic_calendar/term.rb', line 56

def hash
  [self.class, academic_year, start_date, end_date].hash
end

#include?Object

Forwards to Range

See Also:

  • Range#include?


23
# File 'lib/uk_academic_calendar/term.rb', line 23

def_delegators :to_range, :include?, :each

#inspectObject



76
77
78
# File 'lib/uk_academic_calendar/term.rb', line 76

def inspect
  to_s
end

#to_rangeRange

Returns range bounded by start and end dates, end inclusive.

Returns:

  • (Range)

    range bounded by start and end dates, end inclusive



89
90
91
# File 'lib/uk_academic_calendar/term.rb', line 89

def to_range
  start_date..end_date
end

#to_sString

Returns prettified string describing the term, e.g. “Summer 2023/2024”.

Examples:

term = UKAcademicCalendar::SummerTerm 2023
term.to_s #=> "Summer 2023/2024"

Returns:

  • (String)

    prettified string describing the term, e.g. “Summer 2023/2024”



72
73
74
# File 'lib/uk_academic_calendar/term.rb', line 72

def to_s
  "#{season.to_s.titleize} #{academic_year.to_s(slash_succ: true)}"
end