Class: Quarter
Instance Attribute Summary collapse
-
#quarter ⇒ Object
Returns the value of attribute quarter.
-
#year ⇒ Object
Returns the value of attribute year.
Instance Method Summary collapse
- #+(x) ⇒ Object
- #-(x) ⇒ Object
- #<=>(other) ⇒ Object
- #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
- #next ⇒ Object
- #previous ⇒ Object
- #to_year ⇒ Object
- #weekdays ⇒ Object
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
#quarter ⇒ Object
Returns the value of attribute quarter.
4 5 6 |
# File 'lib/quarter.rb', line 4 def quarter @quarter end |
#year ⇒ Object
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 |
#dates ⇒ Object
55 |
# File 'lib/quarter.rb', line 55 def dates; (first_date..last_date).to_a; end |
#first_date ⇒ Object
57 |
# File 'lib/quarter.rb', line 57 def first_date; first_month.first_day; end |
#first_month ⇒ Object
52 |
# File 'lib/quarter.rb', line 52 def first_month; Month.new(@year,((@quarter - 1)*3)+1); end |
#first_weekday ⇒ Object
62 |
# File 'lib/quarter.rb', line 62 def first_weekday; first_month.first_weekday; end |
#last_date ⇒ Object
58 |
# File 'lib/quarter.rb', line 58 def last_date; last_month.last_day; end |
#last_month ⇒ Object
53 |
# File 'lib/quarter.rb', line 53 def last_month; Month.new(@year,((@quarter - 1)*3)+3); end |
#last_weekday ⇒ Object
63 |
# File 'lib/quarter.rb', line 63 def last_weekday; last_month.last_weekday; end |
#months ⇒ Object
50 |
# File 'lib/quarter.rb', line 50 def months; (first_month..last_month).to_a; end |
#next ⇒ Object
47 |
# File 'lib/quarter.rb', line 47 def next; self + 1; end |
#previous ⇒ Object
48 |
# File 'lib/quarter.rb', line 48 def previous; self - 1; end |
#weekdays ⇒ Object
60 |
# File 'lib/quarter.rb', line 60 def weekdays; (first_weekday..last_weekday).to_a; end |