Permalink
Browse files

holidays method retrieve yearly holiday data from google api at once

today color option accept multiple attr
  • Loading branch information...
1 parent 817e948 commit 52d773e6fbb6f828c2d277401898401d58111df1 @melborne committed Dec 10, 2010
Showing with 17 additions and 14 deletions.
  1. +17 −14 lib/month.rb
View
@@ -8,16 +8,16 @@ module Caline
String.send(:include, Term::ANSIColor)
class Month
+ @@holidays = {}
attr_reader :year, :month, :last, :colors
def initialize(year, month, colors={})
@year = year || Date.today.year
@month = month || Date.today.mon
@first = Date.new(@year, @month, 1)
@last = last_date(@year, @month)
- @colors = { title: [:green,:yellow], today: :green,
+ @colors = { title: [:green,:yellow], today: [:green, :underline],
saturday: :cyan, sunday: :magenta, holiday: :red }
@colors.update(colors)
- @holidays = nil
end
def dates
@@ -50,7 +50,7 @@ def colors=(colors)
end
def holidays(code=:ja_ja)
- @holidays = GCalendar.new(@year, @month).holidays(code)
+ @@holidays[@year] ||= GCalendar.new(@year).holidays(code)
end
private
@@ -66,15 +66,13 @@ def color_proc
->w{
w.map do |d|
f = "%2d"
- f =
- case d
- when holiday? then f.send(@colors[:holiday])
- when sunday? then f.send(@colors[:sunday])
- when saturday? then f.send(@colors[:saturday])
- else f
- end
- f = f.send(@colors[:today]) if today?[d]
-
+ f = case d
+ when holiday? then f.send(@colors[:holiday])
+ when sunday? then f.send(@colors[:sunday])
+ when saturday? then f.send(@colors[:saturday])
+ else f
+ end
+ f = today_format(f) if today?[d]
formaty f, d.day
end.join(" ")
}
@@ -93,8 +91,13 @@ def today?
end
def holiday?
- return nil unless @holidays
- ->d{ @holidays.has_key?(d) }
+ return nil unless @@holidays[@year]
+ ->d{ @@holidays[@year].has_key?(d) }
+ end
+
+ def today_format(f)
+ c = Array(@colors[:today])
+ c.inject(f) { |mem, c| mem = mem.send(c) }
end
end
end

0 comments on commit 52d773e

Please sign in to comment.