Class: OSU::Term

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/osu_term.rb

Overview

A simple helper class to turn strms into readable human names. It can also determine if an strm took place during quarters, semesters, or during the quarter-to-semester transition. It can also calculate the next strm.

Constant Summary collapse

QUARTER_TO_SEMESTER_STRM =

this strm was used to to transition from quarters to semesters

'1123'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strm:) ⇒ OsuTerm

Initializes a new OsuTerm based on the strm passed in.

The osu term’s strm number. This number has several requirements:

  1. The strm must be a string that’s 4 digits in length.

  2. The first character must be 0 or 1. 0 = 1900-1999, 1 = 2000 and beyond

  3. strm’s 2nd and 3rd char represents last two digits of the year

  4. the strm’s last digit:

    • quarter: 0 (winter), 2 (Spring), 4 (Summer), or 8 (Autumn)

    • semester: 2 (Spring), 4 (Summer), or 8 (Autumn)

For example, ‘1152’ stands for “Spring 2015”. 1 = 2000 15 = 2015 2 = Spring

Parameters:

  • args (Hash)

    A hash of arguments to pass into the constructor.

Raises:

  • (ArgumentError)

    Raised when the strm is malformed.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/osu_term.rb', line 35

def initialize(strm:)
  strm = strm.to_s
  raise ArgumentError, 'strm must be 4 digits in length' unless FOUR_DIGITS.match?(strm)
  raise ArgumentError, 'strm must start with 0 or 1' unless CENTURIES.match?(strm)

  @strm = strm

  if strm < QUARTER_TO_SEMESTER_STRM
    extend(Quarter)
    raise ArgumentError, 'strm must end in 0, 2, 4, or 8' unless Quarter::SEASONS.match?(strm)
  elsif strm == QUARTER_TO_SEMESTER_STRM
    extend(TransitionTerm)
  else
    extend(Semester)
    raise ArgumentError, 'strm must end in 2, 4, or 8' unless Semester::SEASONS.match?(strm)
  end
end

Instance Attribute Details

#strmObject (readonly)

Returns the value of attribute strm.



10
11
12
# File 'lib/osu_term.rb', line 10

def strm
  @strm
end

Instance Method Details

#<=>(other) ⇒ Object



81
82
83
84
85
# File 'lib/osu_term.rb', line 81

def <=>(other)
  strm <=> other.strm
rescue NoMethodError
  nil
end

#abbr_nameObject



57
58
59
# File 'lib/osu_term.rb', line 57

def abbr_name
  names[season_number].fetch(:abbr)
end

#abbr_yearObject



61
62
63
# File 'lib/osu_term.rb', line 61

def abbr_year
  strm[1..2]
end

#abbreviatedObject



77
78
79
# File 'lib/osu_term.rb', line 77

def abbreviated
  @abbreviated ||= "#{abbr_name} '#{abbr_year} #{abbr_type}"
end

#descriptionObject



69
70
71
# File 'lib/osu_term.rb', line 69

def description
  @description ||= "#{name} #{year} #{type}"
end

#nameObject



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

def name
  names[season_number].fetch(:full)
end

#nextObject



111
112
113
# File 'lib/osu_term.rb', line 111

def next
  self.class.new(strm: next_strm)
end

#next_strmObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/osu_term.rb', line 87

def next_strm
  if transition_semester?
    '1124'
  else
    next_season_number = next_season.fetch(season_number)

    case season_number
    when 0, 2, 4
      century_and_decade = strm[0..2]

      "#{century_and_decade}#{next_season_number}"
    when 8
      century_indicator = strm[0].to_i
      next_year = decade + 1

      if next_year == 100
        [century_indicator + 1, 0, 0, next_season_number].join
      else
        century_indicator.to_s + next_year.to_s.rjust(2, '0') + next_season_number.to_s
      end
    end
  end
end

#to_sObject



73
74
75
# File 'lib/osu_term.rb', line 73

def to_s
  description
end

#yearObject



65
66
67
# File 'lib/osu_term.rb', line 65

def year
  @year ||= "#{century}#{abbr_year}"
end