Class: Quarter
- Inherits:
-
Object
- Object
- Quarter
- Defined in:
- lib/quarter.rb
Instance Attribute Summary collapse
-
#quarter ⇒ Object
Returns the value of attribute quarter.
-
#year ⇒ Object
Returns the value of attribute year.
Instance Method Summary collapse
- #dates ⇒ Object
- #first_date ⇒ Object
- #first_month ⇒ Object
- #first_weekday ⇒ Object
-
#initialize(year, quarter) ⇒ Quarter
constructor
A new instance of Quarter.
- #last_date ⇒ Object
- #last_month ⇒ Object
- #last_weekday ⇒ Object
-
#months ⇒ Object
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.
- #to_year ⇒ Object
- #weekdays ⇒ Object
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
#quarter ⇒ Object
Returns the value of attribute quarter.
2 3 4 |
# File 'lib/quarter.rb', line 2 def quarter @quarter end |
#year ⇒ Object
Returns the value of attribute year.
2 3 4 |
# File 'lib/quarter.rb', line 2 def year @year end |
Instance Method Details
#dates ⇒ Object
38 |
# File 'lib/quarter.rb', line 38 def dates; (first_date..last_date).to_a; end |
#first_date ⇒ Object
40 |
# File 'lib/quarter.rb', line 40 def first_date; first_month.first_day; end |
#first_month ⇒ Object
35 |
# File 'lib/quarter.rb', line 35 def first_month; Month.new(@year,@quarter); end |
#first_weekday ⇒ Object
45 |
# File 'lib/quarter.rb', line 45 def first_weekday; first_month.first_weekday; end |
#last_date ⇒ Object
41 |
# File 'lib/quarter.rb', line 41 def last_date; last_month.last_day; end |
#last_month ⇒ Object
36 |
# File 'lib/quarter.rb', line 36 def last_month; Month.new(@year,@quarter+2); end |
#last_weekday ⇒ Object
46 |
# File 'lib/quarter.rb', line 46 def last_weekday; last_month.last_weekday; end |
#months ⇒ Object
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_year ⇒ Object
9 10 11 |
# File 'lib/quarter.rb', line 9 def to_year Year.new(@year) end |
#weekdays ⇒ Object
43 |
# File 'lib/quarter.rb', line 43 def weekdays; (first_weekday..last_weekday).to_a; end |