Class: Kalenteri::HTMLCalendar

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

Instance Attribute Summary collapse

Attributes inherited from Calendar

#day_abbr, #day_name, #first_weekday, #month_abbr, #month_name

Instance Method Summary collapse

Methods inherited from Calendar

#first_in_monthcal, #iter_month_dates, #last_in_monthcal, #month_calendar, #split_to_weeks, #weekdays, #year_rows

Constructor Details

#initialize(weekday = Kalenteri::SUNDAY) ⇒ HTMLCalendar

Returns a new instance of HTMLCalendar.



9
10
11
12
13
14
15
16
# File 'lib/kalenteri/html_calendar.rb', line 9

def initialize(weekday=Kalenteri::SUNDAY)
  super(weekday)
  @day_classes= %w[sun mon tue wed thu fri sat]
  if defined?(I18n)
    rails_i18n!
  end
  @today = Time.respond_to?(:zone) && Time.zone ? Time.zone.now.to_date : Date.today
end

Instance Attribute Details

#day_classesObject

Returns the value of attribute day_classes.



7
8
9
# File 'lib/kalenteri/html_calendar.rb', line 7

def day_classes
  @day_classes
end

#month_headerObject

Returns the value of attribute month_header.



7
8
9
# File 'lib/kalenteri/html_calendar.rb', line 7

def month_header
  @month_header
end

#year_headerObject

Returns the value of attribute year_header.



7
8
9
# File 'lib/kalenteri/html_calendar.rb', line 7

def year_header
  @year_header
end

Instance Method Details

#format_month(year = @today.year, month = @today.month, &block) ⇒ Object

Returns a month’s calendar as an HTML table.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/kalenteri/html_calendar.rb', line 65

def format_month(year=@today.year, month=@today.month, &block)
  @year = year
  @month = month
  if block.nil?
    block = lambda {|d| d.month == @month ? d.mday : ' '}
  end

  # How month name header should be presented. You can replace this with your own
  # for e.g. next and previous month links.
  @month_header ||= lambda { |year, month|
    %(<tr><th colspan="7" class="month">#{@month_name[month]} #{year}</th></tr>\n)
  }

  cal = %(<table border="0" cellpadding="0" cellspacing="0" class="month">\n)
  cal << @month_header[@year, @month]
  cal << format_week_header()
  self.month_calendar(@year, @month).each do |days|
    cal << format_week(days, block)
  end
  cal << "</table>"
  return cal
end

#format_week(the_week, block) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/kalenteri/html_calendar.rb', line 97

def format_week(the_week, block)
  wr = "<tr>\n"
  the_week.each do |day|
    klass = @day_classes[day.wday]
    klass += ' today' if day.to_s == @today.to_s
    klass += ' noday' if day.month != @month
    wr << %(<td class="#{klass}">#{block.call(day)}</td>\n)
  end
  wr << "</tr>\n"
  return wr
end

#format_week_headerObject



88
89
90
91
92
93
94
95
# File 'lib/kalenteri/html_calendar.rb', line 88

def format_week_header
  wh = "<tr>\n"
  self.weekdays() do |d|
    wh << %(<th scope="col" class="#{@day_classes[d]}">#{@day_abbr[d]}</th>\n)
  end
  wh << "</tr>\n"
  return wh
end

#format_year(year = @today.year, &block) ⇒ Object

Returns a calendar for the given year as nested HTML tables, three months per row.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kalenteri/html_calendar.rb', line 43

def format_year(year=@today.year, &block)
  @year_header ||= lambda { |year|
    %(<tr><th colspan="3" class="year">#{year}</th></tr>)
  }
  # use simple month name formatter
  @month_header ||= lambda { |year, month|
    %(<tr><th colspan="7" class="month">#{@month_name[month]}</th></tr>\n)
  }
  cal = %(<table border="0" cellpadding="0" cellspacing="0" class="year">\n)
  cal << @year_header[year]
  self.year_rows do |c1, c2, c3|
    cal << "<tr>"
    cal << %(<td class="month" valign="top">#{format_month(year, c1, &block)}</td>)
    cal << %(<td class="month" valign="top">#{format_month(year, c2, &block)}</td>)
    cal << %(<td class="month" valign="top">#{format_month(year, c3, &block)}</td>)
    cal << "</tr>"
  end
  cal << "</table>"
  return cal
end

#rails_i18n!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kalenteri/html_calendar.rb', line 18

def rails_i18n!
  @day_name = begin
                I18n.t("date.day_names", :raise => true).dup
              rescue I18n::MissingTranslationData
                Date::DAYNAMES.dup
              end
  @day_abbr = begin
                I18n.t("date.abbr_day_names", :raise => true).dup
              rescue I18n::MissingTranslationData
                Date::ABBR_DAYNAMES.dup
              end
  @month_name = begin
                  I18n.t("date.month_names", :raise => true).dup
                rescue I18n::MissingTranslationData
                  Date::MONTHNAMES.dup
                end
  @month_abbr = begin
                  I18n.t("date.abbr_month_names", :raise => true).dup
                rescue I18n::MissingTranslationData
                  Date::ABBR_MONTHNAMES.dup
                end
end