Class: Monthra::MonthRange
- Inherits:
-
Object
- Object
- Monthra::MonthRange
- Includes:
- Enumerable
- Defined in:
- lib/monthra/month_range.rb
Instance Attribute Summary collapse
-
#months ⇒ Object
Returns the value of attribute months.
Class Method Summary collapse
-
.months_after_start(start_month, size) ⇒ Object
Note that this method will return a range with <size> number of months.
Instance Method Summary collapse
-
#each ⇒ Object
Iterates over the months to meet the conditions of Enumerable.
-
#initialize(start_month, end_month) ⇒ MonthRange
constructor
A new instance of MonthRange.
-
#last ⇒ Month
The last month in the range.
-
#size ⇒ Integer
Number of months in the range.
Constructor Details
#initialize(start_month, end_month) ⇒ MonthRange
Returns a new instance of MonthRange.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/monthra/month_range.rb', line 27 def initialize(start_month, end_month) start_month = Month.at(start_month) end_month = Month.at(end_month) if end_month < start_month raise ArgumentError, "Start month must occur before the end month" end setup_months(start_month, end_month) end |
Instance Attribute Details
#months ⇒ Object
Returns the value of attribute months.
4 5 6 |
# File 'lib/monthra/month_range.rb', line 4 def months @months end |
Class Method Details
.months_after_start(start_month, size) ⇒ Object
Note that this method will return a range with <size> number of months. It will not return a range from <start month> to <start month + size> It will actually return a range from <start month> to <start month + size - 1> because
that will return a range with <size> elements.
14 15 16 17 18 19 20 21 |
# File 'lib/monthra/month_range.rb', line 14 def self.months_after_start(start_month, size) start_month = Month.at(start_month) # the end date is inclusive so it needs to end one month before <size> to get the correct # number of months end_month = start_month + (size - 1) self.new(start_month, end_month) end |
Instance Method Details
#each ⇒ Object
Iterates over the months to meet the conditions of Enumerable
39 40 41 42 43 |
# File 'lib/monthra/month_range.rb', line 39 def each months.each do |m| yield(m) end end |
#last ⇒ Month
Returns The last month in the range.
51 52 53 |
# File 'lib/monthra/month_range.rb', line 51 def last months.last end |
#size ⇒ Integer
Returns Number of months in the range.
46 47 48 |
# File 'lib/monthra/month_range.rb', line 46 def size months.size end |