class Monthra::MonthRange

Attributes

months[RW]

Public Class Methods

months_after_start(start_month, size) click to toggle source

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.

@param [Month/Date/Time] start_month The beginning of the range. Needs to include a year and month

method to match the interface.

@param [Integer] size The Number of months in the range

# 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
new(start_month, end_month) click to toggle source

@param [Month/Date/Time] start_month The beginning of the range. Needs to include a year and

month method to match the interface.

@param [Month/Date/Time] end_month The end of the range. Needs to include a year and month

method to match the interface.
# 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

Public Instance Methods

each() { |m| ... } click to toggle source

Iterates over the months to meet the conditions of Enumerable

# File lib/monthra/month_range.rb, line 39
def each
  months.each do |m|
    yield(m)
  end
end
last() click to toggle source

@return [Month] The last month in the range

# File lib/monthra/month_range.rb, line 51
def last
  months.last
end
size() click to toggle source

@return [Integer] Number of months in the range

# File lib/monthra/month_range.rb, line 46
def size
  months.size
end

Private Instance Methods

setup_months(start_month, end_month) click to toggle source

Creates the internal representation of the month range

@param [Month] start_month @param [Month] end_month

# File lib/monthra/month_range.rb, line 61
def setup_months(start_month, end_month)
  @months = []
  while start_month <= end_month
    @months << start_month
    start_month += 1
  end
end