Module: BroadcastCalendar

Extended by:
BroadcastCalendar
Included in:
BroadcastCalendar
Defined in:
lib/broadcast_calendar.rb,
lib/broadcast_calendar/version.rb

Constant Summary collapse

VERSION =
"1.1.1"

Instance Method Summary collapse

Instance Method Details

#dates_for(month, year) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/broadcast_calendar.rb', line 6

def dates_for(month,year)
  # find the monday of the first week of the month
  beginning = Date.civil(year,month,1)

  if (wday = beginning.wday) == 0
    beginning -= 6
  else
    beginning -= wday - 1
  end
  
  ending = Date.civil(year,month,-1)
  ending -= ending.wday # broadcast calendar always ends on Sunday

  beginning..ending
end

#month_and_year_for(date) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/broadcast_calendar.rb', line 26

def month_and_year_for(date)
  range = dates_for(date.month,date.year)

  month = date.month
  year = date.year

  unless range.cover?(date)
    month += (date <=> range.begin)
    if month < 1
      month = 12
      year -= 1
    elsif month > 12
      month = 1
      year += 1
    end
  end

  [month,year]
end

#weeks_for(month, year) ⇒ Object



22
23
24
# File 'lib/broadcast_calendar.rb', line 22

def weeks_for(month,year)
  dates_for(month,year).each_slice(7).collect { |s| s.first..s.last }
end