Class: SemanticCalendar::Monthly::TableCalendarBuilder
- Inherits:
-
Object
- Object
- SemanticCalendar::Monthly::TableCalendarBuilder
- Defined in:
- lib/builders/monthly/table_builder.rb
Instance Attribute Summary collapse
-
#t ⇒ Object
Returns the value of attribute t.
Instance Method Summary collapse
- #collect_days(&proc) ⇒ Object
- #collect_month_days(&proc) ⇒ Object
- #collect_next_month_days(&proc) ⇒ Object
- #collect_previous_month_days(&proc) ⇒ Object
- #day(d, value, options, extra_class = nil) ⇒ Object
- #days(options = {}, &proc) ⇒ Object
- #head(options = {}, &proc) ⇒ Object
-
#initialize(template, date, options = {}, &proc) ⇒ TableCalendarBuilder
constructor
A new instance of TableCalendarBuilder.
- #render(options, &proc) ⇒ Object
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, = {}, &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([:first_day_of_week] || 0) @last_weekday = last_day_of_week([:first_day_of_week] || 0) @day_names = Date::DAYNAMES.dup @first_weekday.times { @day_names.push(@day_names.shift) } render(.delete(:html), &proc) end |
Instance Attribute Details
#t ⇒ Object
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, = t.capture(d, &proc) if block_given? @month_days << day(d, value, ) 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, = t.capture(d, &proc) if block_given? @month_days << day(d, value, , '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, = t.capture(d, &proc) if block_given? @month_days << day(d, value, , '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, , extra_class = nil) ||= {} [:class] ||= '' value = t.content_tag(:span, d.mday, {:class => 'month-day'}) + value || '' [:class] = t.add_class([:class], weekend?(d) ? 'weekend' : 'weekday') [:class] = t.add_class([:class], 'today') if (d == @today) [:class] = t.add_class([:class], extra_class) unless extra_class.blank? t.content_tag(:td, value, , 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( = {}, &proc) [: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.content_tag(:tr, month_days, {}, false) month_days = '' end end t.content_tag(:tbody, buffer, .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( = {}, &proc) [:abbrev] ||= (0..2) [:html] ||= {} buffer = '' if block_given? value = t.capture(self, &proc).gsub('{month}', Date::MONTHNAMES[@month]) buffer = t.content_tag(:tr, t.content_tag(:th, value, {:colspan => 7})) end day_titles = @day_names.map do |d| d[[:abbrev]] == d ? d[[:abbrev]] : t.content_tag(:abbr, d[[:abbrev]], :title => d) end head = [ buffer, t.content_tag(:tr, day_titles.map { |d| t.content_tag(:th, d) }.join(''), {}, false) ].join('') t.content_tag(:thead, head, .delete(:html), false) end |
#render(options, &proc) ⇒ Object
27 28 29 30 |
# File 'lib/builders/monthly/table_builder.rb', line 27 def render(, &proc) buffer = t.capture(self, &proc) t.concat(t.content_tag(:table, buffer, )) end |