Class: Quarter

Inherits:
Object
  • Object
show all
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.



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

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

Instance Attribute Details

#quarterObject

Returns the value of attribute quarter.



2
3
4
# File 'lib/quarter.rb', line 2

def quarter
  @quarter
end

#yearObject

Returns the value of attribute year.



2
3
4
# File 'lib/quarter.rb', line 2

def year
  @year
end

Instance Method Details

#datesObject



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

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

#first_dateObject



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

def first_date; first_month.first_day; end

#first_monthObject



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

def first_month; Month.new(@year,@quarter); end

#first_weekdayObject



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

def first_weekday; first_month.first_weekday; end

#last_dateObject



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

def last_date; last_month.last_day; end

#last_monthObject



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

def last_month; Month.new(@year,@quarter+2); end

#last_weekdayObject



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

def last_weekday; last_month.last_weekday; end

#monthsObject

def -(x)

if x.is_a?(Numeric)
  return self.class.new(@year - x)
elsif x.is_a?(Quarter) 
  return @year - x.year
else
  raise TypeError, 'expected numeric or year'
end

end

def +(x)

if x.is_a?(Numeric)
  return self.class.new(@year + x)
elsif x.is_a?(Quarter) 
  return @year + x.year
else
  raise TypeError, 'expected numeric or year'
end

end



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

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

#to_yearObject



9
10
11
# File 'lib/quarter.rb', line 9

def to_year
  Year.new(@year)
end

#weekdaysObject



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

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