Class: SemanticCalendar::Monthly::TableCalendarBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/builders/monthly/table_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, date, options = {}, &proc) ⇒ TableCalendarBuilder

Returns a new instance of TableCalendarBuilder.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/builders/monthly/table_builder.rb', line 7

def initialize(template, date, options = {}, &proc)
  @t, @date = template, date

  @year = date.year
  @month = date.month
  @today = (Time.respond_to?(:zone) && !(zone = Time.zone).nil? ? zone.now.to_date : Date.today)
  @month_days = []

  @first = Date.civil(@year, @month, 1)
  @last  = Date.civil(@year, @month, -1)

  @first_weekday = first_day_of_week(options[:first_day_of_week] || 0)
  @last_weekday = last_day_of_week(options[:first_day_of_week] || 0)

  @day_names = Date::DAYNAMES.dup
  @first_weekday.times { @day_names.push(@day_names.shift) }

  render(options.delete(:html), &proc)
end

Instance Attribute Details

#tObject

Returns the value of attribute t.



5
6
7
# File 'lib/builders/monthly/table_builder.rb', line 5

def t
  @t
end

Instance Method Details

#collect_days(&proc) ⇒ Object



73
74
75
76
77
# File 'lib/builders/monthly/table_builder.rb', line 73

def collect_days(&proc)
  collect_previous_month_days(&proc)
  collect_month_days(&proc)
  collect_next_month_days(&proc)
end

#collect_month_days(&proc) ⇒ Object



79
80
81
82
83
84
# File 'lib/builders/monthly/table_builder.rb', line 79

def collect_month_days(&proc)
  @first.upto(@last) do |d|
    value, options = t.capture(d, &proc) if block_given?
    @month_days << day(d, value, options)
  end
end

#collect_next_month_days(&proc) ⇒ Object



93
94
95
96
97
98
# File 'lib/builders/monthly/table_builder.rb', line 93

def collect_next_month_days(&proc)
  (@last + 1).upto(beginning_of_week(@last + 7, @first_weekday) - 1)  do |d|
    value, options = t.capture(d, &proc) if block_given?
    @month_days << day(d, value, options, 'next-month')
  end unless @last.wday == @last_weekday
end

#collect_previous_month_days(&proc) ⇒ Object



86
87
88
89
90
91
# File 'lib/builders/monthly/table_builder.rb', line 86

def collect_previous_month_days(&proc)
  beginning_of_week(@first, @first_weekday).upto(@first - 1) do |d|
    value, options = t.capture(d, &proc) if block_given?
    @month_days << day(d, value, options, 'previous-month')
  end unless @first.wday == @first_weekday
end

#day(d, value, options, extra_class = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/builders/monthly/table_builder.rb', line 100

def day(d, value, options, extra_class = nil)
  options ||= {}
  options[:class] ||= ''

  value = t.(:span, d.mday, {:class => 'month-day'}) + value || ''

  options[:class] = t.add_class(options[:class], weekend?(d) ? 'weekend' : 'weekday')
  options[:class] = t.add_class(options[:class], 'today') if (d == @today)
  options[:class] = t.add_class(options[:class], extra_class) unless extra_class.blank?

  t.(:td, value, options, false)
end

#days(options = {}, &proc) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/builders/monthly/table_builder.rb', line 55

def days(options = {}, &proc)
  options[:html] ||= {}

  collect_days(&proc)

  buffer = ''
  month_days = ''
  @month_days.each_with_index do |d, index|
    month_days << d
    if (index+1) % 7 == 0
      buffer << t.(:tr, month_days, {}, false)
      month_days = ''
    end
  end

  t.(:tbody, buffer, options.delete(:html), false)
end

#head(options = {}, &proc) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/builders/monthly/table_builder.rb', line 32

def head(options = {}, &proc)
  options[:abbrev] ||= (0..2)

  options[:html] ||= {}

  buffer = ''
  if block_given?
    value = t.capture(self, &proc).gsub('{month}', Date::MONTHNAMES[@month])
    buffer = t.(:tr, t.(:th, value, {:colspan => 7}))
  end

  day_titles = @day_names.map do |d|
    d[options[:abbrev]] == d ? d[options[:abbrev]] : t.(:abbr, d[options[:abbrev]], :title => d)
  end

  head = [
    buffer,
    t.(:tr, day_titles.map { |d| t.(:th, d) }.join(''), {}, false)
  ].join('')

  t.(:thead, head, options.delete(:html), false)
end

#render(options, &proc) ⇒ Object



27
28
29
30
# File 'lib/builders/monthly/table_builder.rb', line 27

def render(options, &proc)
  buffer = t.capture(self, &proc)
  t.concat(t.(:table, buffer, options))
end