Class: Monthra::MonthRange

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/monthra/month_range.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_month, end_month) ⇒ MonthRange

Returns a new instance of MonthRange.

Parameters:

  • start_month (Month/Date/Time)

    The beginning of the range. Needs to include a year and month method to match the interface.

  • end_month (Month/Date/Time)

    The end of the range. Needs to include a year and month method to match the interface.


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

#monthsObject

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.

Parameters:

  • start_month (Month/Date/Time)

    The beginning of the range. Needs to include a year and month method to match the interface.

  • size (Integer)

    The Number of months in the range


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

#eachObject

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

#lastMonth

Returns The last month in the range.

Returns:

  • (Month)

    The last month in the range


51
52
53
# File 'lib/monthra/month_range.rb', line 51

def last
  months.last
end

#sizeInteger

Returns Number of months in the range.

Returns:

  • (Integer)

    Number of months in the range


46
47
48
# File 'lib/monthra/month_range.rb', line 46

def size
  months.size
end