Permalink
Browse files

provide holidays footer for year calendar

  • Loading branch information...
1 parent 9af008a commit 8fe2ce9fe14ecddadfe2c84a9f3604abb4f0ef5a @melborne committed Dec 17, 2010
Showing with 38 additions and 9 deletions.
  1. +4 −0 lib/month.rb
  2. +34 −9 lib/year.rb
View
@@ -61,6 +61,10 @@ def holidays=(code)
@@holidays[@year][@code] ||= GCalendar.new(@year).holidays(@code)
end
+ def holidays
+ @@holidays
+ end
+
def +(month)
mon = @first.next_month(month)
m = Month.new(mon.year, mon.mon, @colors)
View
@@ -4,30 +4,55 @@
module Caline
class Year
include ColumnForm
+ @@holidays = Hash.new{ |h, k| h[k] = {} }
def initialize(year, opts={})
@year = year
- @holidays = opts.delete(:holidays)
+ @code = opts.delete(:holidays)
@months = (1..12).map { |m| Caline::Month.new(@year, m, opts) }
+ if @code
+ @months.each { |m| m.holidays = @code }
+ @@holidays[@year] = @months.first.holidays[@year]
+ end
+ end
+
+ def holidays=(code)
+ @months.first.holidays = @code = code
+ @@holidays[@year] = @months.first.holidays[@year]
end
alias formaty format
- def format(style=:block, from=0, color=false, ho_name=true)
- out, year_label = [], nil
+ def format(style=:block, from=0, color=false, footer=false)
+ body =
case style
when :block
- three_columns_formatter[@months, from, color, ho_name]
+ body = three_columns_formatter[@months, from, color]
when :line
- @months.each_with_index do |mon, i|
- mon.colors = {neighbor: :black}
- year_label, *body = mon.format(:line, from, color, ho_name)
+ out, year_label = [], nil
+ @months.each do |mon|
+ year_label, *body = mon.format(:line, from, color, false)
out << body
end
out.unshift year_label
end
+ footer ? body + holiday_names(@months) : body
end
- def color_format(style=:block, from=0, ho_name=true)
- format(style, from, true, ho_name)
+ def color_format(style=:block, from=0, footer=false)
+ format(style, from, true, footer)
+ end
+
+ private
+ def holiday_names(months)
+ holidays = @@holidays[@year][@code]
+ if holidays
+ out = []
+ holidays.sort_by { |d, _| d }
+ .map { |date, name| date.strftime('%_m/%_d').yellow + ": " + name.green }
+ .each_slice(3) { |gr| out << gr.join }
+ out
+ else
+ []
+ end
end
end
end

0 comments on commit 8fe2ce9

Please sign in to comment.