class Calrom::DateRange

Public Instance Methods

each_month() { |self| ... } click to toggle source
# File lib/calrom/date_range.rb, line 3
def each_month
  return to_enum(:each_month) unless block_given?

  if first.year == last.year && first.month == last.month
    # a single month or it's part
    yield self
    return
  end

  (Month.new(first.year, first.month) .. Month.new(last.year, last.month))
    .each_with_index do |m,i|
    if i == 0 && first.day > 1
      # first month, incomplete
      end_of_month = first.next_month - first.day + 1
      yield self.class.new(first, m.last)
    elsif m.first.year == last.year && m.first.month == last.month && last != m.last
      # last month, incomplete
      yield self.class.new(m.first, last)
    else
      yield m
    end
  end
end