Class: OpenStax::Salesforce::Remote::TermYear

Inherits:
Object
  • Object
show all
Defined in:
lib/openstax/salesforce/remote/term_year.rb

Defined Under Namespace

Classes: ParseError

Constant Summary collapse

TERMS =

TermYear strings in Salesforce look like:

2015 - 16 Fall
2015 - 16 Spring

One day in the not-distant future we will probably be adding a Summer term

[:fall, :spring]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_year:, term:) ⇒ TermYear

Returns a new instance of TermYear.



12
13
14
15
16
17
# File 'lib/openstax/salesforce/remote/term_year.rb', line 12

def initialize(start_year:, term:)
  raise "Invalid term #{term}" if !TERMS.include?(term)
  @start_year = start_year
  @end_year = [:fall, :spring].include?(term) ? start_year + 1 : start_year
  @term = term
end

Instance Attribute Details

#end_yearObject (readonly)

Returns the value of attribute end_year.



10
11
12
# File 'lib/openstax/salesforce/remote/term_year.rb', line 10

def end_year
  @end_year
end

#start_yearObject (readonly)

Returns the value of attribute start_year.



10
11
12
# File 'lib/openstax/salesforce/remote/term_year.rb', line 10

def start_year
  @start_year
end

#termObject (readonly)

Returns the value of attribute term.



10
11
12
# File 'lib/openstax/salesforce/remote/term_year.rb', line 10

def term
  @term
end

Class Method Details

.from_string(string) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/openstax/salesforce/remote/term_year.rb', line 32

def self.from_string(string)
  return if string.blank?

  string.match(/20(\d\d) - (\d\d) (\w+)/).tap do |match|
    raise(ParseError, "Cannot parse '#{string}' as a TermYear") if match.nil?
  end

  term = $3.downcase.to_sym
  start_year = "20#{$1}".to_i
  raise "Non-sequential years in TermYear: '#{string}'" if $2.to_i != $1.to_i + 1

  new(start_year: start_year, term: term)
end

.guess_from_created_at(created_at) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/openstax/salesforce/remote/term_year.rb', line 19

def self.guess_from_created_at(created_at)
  spring_to_fall_cutoff = Time.zone.local(created_at.year, 4, 15, 00, 00)
  fall_to_spring_cutoff = Time.zone.local(created_at.year, 11, 15, 00, 00)

  if created_at < spring_to_fall_cutoff
    new(start_year: created_at.year - 1, term: :spring)
  elsif created_at > fall_to_spring_cutoff
    new(start_year: created_at.year, term: :spring)
  else
    new(start_year: created_at.year, term: :fall)
  end
end

Instance Method Details

#==(other) ⇒ Object



64
65
66
# File 'lib/openstax/salesforce/remote/term_year.rb', line 64

def ==(other)
  to_s == other.to_s
end

#dupObject



72
73
74
# File 'lib/openstax/salesforce/remote/term_year.rb', line 72

def dup
  self.class.new(start_year: start_year, term: term)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/openstax/salesforce/remote/term_year.rb', line 68

def eql?(other)
  self == other
end

#fall?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/openstax/salesforce/remote/term_year.rb', line 56

def fall?
  :fall == @term
end

#nextObject



46
47
48
49
50
# File 'lib/openstax/salesforce/remote/term_year.rb', line 46

def next
  fall? ?
    self.class.new(start_year: start_year, term: :spring) :
    self.class.new(start_year: start_year + 1, term: :fall)
end

#spring?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/openstax/salesforce/remote/term_year.rb', line 60

def spring?
  !fall?
end

#to_sObject



52
53
54
# File 'lib/openstax/salesforce/remote/term_year.rb', line 52

def to_s
  "#{start_year} - #{end_year.to_s[2..3]} #{fall? ? 'Fall' : 'Spring'}"
end