Class: Kalenteri::Calendar
- Inherits:
-
Object
- Object
- Kalenteri::Calendar
- Defined in:
- lib/kalenteri/calendar.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#day_abbr ⇒ Object
Returns the value of attribute day_abbr.
-
#day_name ⇒ Object
Returns the value of attribute day_name.
-
#first_weekday ⇒ Object
readonly
Returns the value of attribute first_weekday.
-
#month_abbr ⇒ Object
Returns the value of attribute month_abbr.
-
#month_name ⇒ Object
Returns the value of attribute month_name.
Instance Method Summary collapse
-
#first_in_monthcal(first_day) ⇒ Object
First weekday (as Date object) of the first week of the month as specified by @firstweekday.
-
#initialize(day = Kalenteri::SUNDAY) ⇒ Calendar
constructor
A new instance of Calendar.
-
#iter_month_dates(year, month) ⇒ Object
An iterator for all days in the month specified as Date objects.
-
#last_in_monthcal(last_day) ⇒ Object
Last weekday (as Date object) of the last week of the month as specified by @firstweekday.
-
#month_calendar(year, month) ⇒ Object
Return a array of the weeks in the month of the year as full weeks.
- #split_to_weeks(dates, weeks = []) ⇒ Object
-
#weekdays ⇒ Object
Day numbers in one week.
- #year_rows ⇒ Object
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_abbr ⇒ Object
Returns the value of attribute day_abbr.
9 10 11 |
# File 'lib/kalenteri/calendar.rb', line 9 def day_abbr @day_abbr end |
#day_name ⇒ Object
Returns the value of attribute day_name.
9 10 11 |
# File 'lib/kalenteri/calendar.rb', line 9 def day_name @day_name end |
#first_weekday ⇒ Object (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_abbr ⇒ Object
Returns the value of attribute month_abbr.
9 10 11 |
# File 'lib/kalenteri/calendar.rb', line 9 def month_abbr @month_abbr end |
#month_name ⇒ Object
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 |
#weekdays ⇒ Object
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_rows ⇒ Object
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 |