Class: Quarter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year, quarter) ⇒ Quarter

Returns a new instance of Quarter.



6
7
8
9
# File 'lib/quarter.rb', line 6

def initialize(year, quarter)
  @year = year.to_i
  @quarter = quarter.to_i
end

Instance Attribute Details

#quarterObject

Returns the value of attribute quarter.



4
5
6
# File 'lib/quarter.rb', line 4

def quarter
  @quarter
end

#yearObject

Returns the value of attribute year.



4
5
6
# File 'lib/quarter.rb', line 4

def year
  @year
end

Instance Method Details

#+(x) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/quarter.rb', line 33

def +(x)
  if x.is_a?(Numeric)
    y = year + (x / 4)
    q = quarter + (x % 4)
    if q > 4
      q -= 4
      y += 1
    end
    return Quarter.new(y, q)      
  else
    raise TypeError, 'expected numeric'
  end
end

#-(x) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/quarter.rb', line 19

def -(x)
  if x.is_a?(Numeric)
    y = year - (x / 4)
    q = quarter - (x % 4)
    if q < 1
      y -= 1
      q += 4
    end 
    return Quarter.new(y, q)
  else
    raise TypeError, 'expected numeric'
  end
end

#<=>(other) ⇒ Object



15
16
17
# File 'lib/quarter.rb', line 15

def <=>(other)
  self.first_date <=> other.first_date
end

#datesObject



55
# File 'lib/quarter.rb', line 55

def dates; (first_date..last_date).to_a; end

#first_dateObject



57
# File 'lib/quarter.rb', line 57

def first_date; first_month.first_day; end

#first_monthObject



52
# File 'lib/quarter.rb', line 52

def first_month; Month.new(@year,((@quarter - 1)*3)+1); end

#first_weekdayObject



62
# File 'lib/quarter.rb', line 62

def first_weekday; first_month.first_weekday; end

#last_dateObject



58
# File 'lib/quarter.rb', line 58

def last_date; last_month.last_day; end

#last_monthObject



53
# File 'lib/quarter.rb', line 53

def last_month; Month.new(@year,((@quarter - 1)*3)+3); end

#last_weekdayObject



63
# File 'lib/quarter.rb', line 63

def last_weekday; last_month.last_weekday; end

#monthsObject



50
# File 'lib/quarter.rb', line 50

def months; (first_month..last_month).to_a; end

#nextObject



47
# File 'lib/quarter.rb', line 47

def next; self + 1; end

#previousObject



48
# File 'lib/quarter.rb', line 48

def previous; self - 1; end

#to_yearObject



11
12
13
# File 'lib/quarter.rb', line 11

def to_year
  Year.new(@year)
end

#weekdaysObject



60
# File 'lib/quarter.rb', line 60

def weekdays; (first_weekday..last_weekday).to_a; end