Module: DateIterator

Defined in:
lib/date_iterator.rb,
lib/date_iterator/version.rb

Constant Summary collapse

VERSION =
'1.0.0'

Instance Method Summary collapse

Instance Method Details

#each_day_until(_until = Date.today, &block) ⇒ Object

yields inclusive date object of each day



6
7
8
9
10
# File 'lib/date_iterator.rb', line 6

def each_day_until(_until=Date.today, &block)
  return self.to_enum(:each_day_until, _until) unless block_given?
  self.step(_until.is_a?(Date) ? _until : Date.parse(_until.to_s), +1, &block)
  nil
end

#each_month_until(_until = Date.today, &block) ⇒ Object

yields inclusive date object of first day of month



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/date_iterator.rb', line 22

def each_month_until(_until=Date.today, &block)
  return self.to_enum(:each_month_until, _until) unless block_given?

  _date = Date.new(self.year,self.month,1)
  _until = _until.is_a?(Date) ? _until : Date.parse(_until.to_s)
  _until = Date.new(_until.year,_until.month,1)

  while _date <= _until
    yield _date
    _date = _date >> 1
    _date = Date.new(_date.year,_date.month,1)
  end
end

#each_week_until(_until = Date.today, use_sunday_as_beg_of_week = false, &block) ⇒ Object

yields inclusive date object of first day of week, starting with mondays



13
14
15
16
17
18
19
# File 'lib/date_iterator.rb', line 13

def each_week_until(_until=Date.today, use_sunday_as_beg_of_week = false, &block)
  return self.to_enum(:each_week_until, _until, use_sunday_as_beg_of_week) unless block_given?
  _days_to_monday = self.wday == 0 ? 6 : self.wday - 1
  _monday = self - _days_to_monday
  _monday.step(_until.is_a?(Date) ? _until : Date.parse(_until.to_s), +7, &block)
  nil
end

#each_year_until(_until = Date.today, &block) ⇒ Object

yields inclusive date object of first day of the year



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/date_iterator.rb', line 37

def each_year_until(_until=Date.today, &block)
  return self.to_enum(:each_year_until, _until) unless block_given?

  _date = Date.new(self.year,1,1)
  _until = _until.is_a?(Date) ? _until : Date.parse(_until.to_s)
  _until = Date.new(_until.year,1,1)

  while _date <= _until
    yield _date
    _date = _date >> 12
    _date = Date.new(_date.year,_date.month,1)
  end
end