Skip to content

Commit

Permalink
refactor format method
Browse files Browse the repository at this point in the history
  • Loading branch information
melborne committed Dec 20, 2010
1 parent 9359013 commit 94fdd2b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
2 changes: 2 additions & 0 deletions lib/calour/column.rb
@@ -1,3 +1,5 @@
# -*- encoding:utf-8 -*-

module Calour::ColumnForm
def three_columns_formatter(months, from, color)
out, year_label = [], nil
Expand Down
60 changes: 28 additions & 32 deletions lib/calour/month.rb
Expand Up @@ -29,15 +29,15 @@ def dates_by_block(from=0)

alias formaty format
def format(style=:block, from=0, color=false, footer=false)
formatter = color ? color_proc : mono_proc
formatter = color ? method(:color_formatter) : method(:mono_formatter)
body =
case style
when :block
header(from, color, :block) +
dates_by_block(from).map { |aweek| formatter[aweek] }
dates_by_block(from).map { |aweek| formatter.call aweek }
when :line
header(from, color, :line) +
Array( formatter[dates] )
Array( formatter.call dates )
when :block3
months = [self-1, self, self+1]
three_columns_formatter(months, from, color)
Expand Down Expand Up @@ -91,35 +91,31 @@ def last_date(year, month, day=[28, 29, 30, 31])
Date.new(year, month, day.pop) rescue retry
end

def mono_proc
->w{
w.map do |d|
str = "%2d"
str = case d
when neighbor? then str.replace " "
else str
end
formaty str, d.day
end.join(" ")
}
end

def color_proc
->w{
w.map do |d|
str = "%2d"
str = case d
when neighbor?
@colors[:neighbor] ? str.color(@colors[:neighbor]) : str.replace(" ")
when holiday? then str.color(@colors[:holiday])
when sunday? then str.color(@colors[:sunday])
when saturday? then str.color(@colors[:saturday])
else str
end
str = str.color(@colors[:today]) if today?[d]
formaty str, d.day
end.join(" ")
}
def mono_formatter(dates)
dates.map do |d|
str = "%2d"
str = case d
when neighbor? then str.replace " "
else str
end
formaty str, d.day
end.join(" ")
end

def color_formatter(dates)
dates.map do |d|
str = "%2d"
str = case d
when neighbor?
@colors[:neighbor] ? str.color(@colors[:neighbor]) : str.replace(" ")
when holiday? then str.color(@colors[:holiday])
when sunday? then str.color(@colors[:sunday])
when saturday? then str.color(@colors[:saturday])
else str
end
str = str.color(@colors[:today]) if today?[d]
formaty str, d.day
end.join(" ")
end

def sunday?
Expand Down

0 comments on commit 94fdd2b

Please sign in to comment.