Class: Kalenteri::Calendar

Inherits:
Object
  • Object
show all
Defined in:
lib/kalenteri/calendar.rb

Direct Known Subclasses

HTMLCalendar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(day = Kalenteri::SUNDAY) ⇒ Calendar

Returns a new instance of Calendar.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kalenteri/calendar.rb', line 11

def initialize(day=Kalenteri::SUNDAY)
  # @firstweekday is an integer specifying the first day of the week.
  # Sunday is day-of-week 0; Saturday is day-of-week 6.
  if (0..6).include?(day)
    @first_weekday = day
  else
    raise ArgumentError, "Day must be an integer between 0 and 6."
  end
  @day_name   = Date::DAYNAMES.dup
  @day_abbr   = Date::ABBR_DAYNAMES.dup
  @month_name = Date::MONTHNAMES.dup
  @month_abbr = Date::ABBR_MONTHNAMES.dup
end

Instance Attribute Details

#day_abbrObject

Returns the value of attribute day_abbr.



9
10
11
# File 'lib/kalenteri/calendar.rb', line 9

def day_abbr
  @day_abbr
end

#day_nameObject

Returns the value of attribute day_name.



9
10
11
# File 'lib/kalenteri/calendar.rb', line 9

def day_name
  @day_name
end

#first_weekdayObject (readonly)

Returns the value of attribute first_weekday.



8
9
10
# File 'lib/kalenteri/calendar.rb', line 8

def first_weekday
  @first_weekday
end

#month_abbrObject

Returns the value of attribute month_abbr.



9
10
11
# File 'lib/kalenteri/calendar.rb', line 9

def month_abbr
  @month_abbr
end

#month_nameObject

Returns the value of attribute month_name.



9
10
11
# File 'lib/kalenteri/calendar.rb', line 9

def month_name
  @month_name
end

Instance Method Details

#first_in_monthcal(first_day) ⇒ Object

First weekday (as Date object) of the first week of the month as specified by @firstweekday



46
47
48
49
50
51
# File 'lib/kalenteri/calendar.rb', line 46

def first_in_monthcal(first_day)
  while first_day.wday != @first_weekday
    first_day -= 1
  end
  return first_day
end

#iter_month_dates(year, month) ⇒ Object

An iterator for all days in the month specified as Date objects. First and last days yielded are possibly in previous or next months, so that full weeks are generated.



36
37
38
39
40
41
42
# File 'lib/kalenteri/calendar.rb', line 36

def iter_month_dates(year, month)
  firstd = Date.new(year, month, 1)
  lastd = Date.new(year, month, -1)
  first_in_monthcal(firstd).upto(last_in_monthcal(lastd)) do |d|
    yield(d)
  end
end

#last_in_monthcal(last_day) ⇒ Object

Last weekday (as Date object) of the last week of the month as specified by @firstweekday.



55
56
57
58
59
60
61
# File 'lib/kalenteri/calendar.rb', line 55

def last_in_monthcal(last_day)
  last_weekday = weekdays().last
  while last_day.wday != last_weekday
    last_day += 1
  end
  return last_day
end

#month_calendar(year, month) ⇒ Object

Return a array of the weeks in the month of the year as full weeks. Weeks are arrays of seven Date objects.



65
66
67
68
69
70
71
# File 'lib/kalenteri/calendar.rb', line 65

def month_calendar(year, month)
  dates = []
  iter_month_dates(year, month) do |day|
    dates << day
  end
  return split_to_weeks(dates)
end

#split_to_weeks(dates, weeks = []) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/kalenteri/calendar.rb', line 73

def split_to_weeks(dates, weeks=[])
  if dates.size % 7 != 0 # DEBUG
    raise 'Calendar month is not divisible to weeks!'
  end
  return weeks if dates.empty?
  weeks << dates[0...7]
  return split_to_weeks(dates[7..-1], weeks)
end

#weekdaysObject

Day numbers in one week. The first value will be the @firstweekday.



27
28
29
30
31
# File 'lib/kalenteri/calendar.rb', line 27

def weekdays
  [0,1,2,3,4,5,6,0,1,2,3,4,5][@first_weekday, 7].each { |x|
    yield(x) if block_given?
  }
end

#year_rowsObject



82
83
84
85
86
# File 'lib/kalenteri/calendar.rb', line 82

def year_rows
  [1,4,7,10].each do |first|
    yield(first, first+1, first+2)
  end
end