Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

year class added

  • Loading branch information...
commit 8bca093b3259c895caab60983553da9e35cefa7e 1 parent 10d72ae
@melborne authored
Showing with 68 additions and 0 deletions.
  1. +52 −0 lib/year.rb
  2. +16 −0 test/tc_year.rb
View
52 lib/year.rb
@@ -0,0 +1,52 @@
+#!/usr/bin/env ruby
+#-*-encoding: utf-8-*-
+require_relative 'month'
+require_relative 'gcalendar'
+
+module Caline
+ class Year
+ @@holidays = Hash.new{ |h, k| h[k] = {} }
+ @@month_width = 20
+ def initialize(year)
+ @year = year
+ @months = (1..12).map { |m| Caline::Month.new(@year, m, neighbor: nil) }
+ end
+
+ def holidays=(code)
+ @code = code
+ @@holidays[@year][@code] ||= Caline::GCalendar.new(@year).holidays(@code)
+ end
+
+ def holidays
+ @@holidays[@year]
+ end
+
+ alias formaty format
+ def format(style=:week, from=0, color=false)
+ out = []
+ @months.each_slice(3) do |gr|
+ left, center, right = gr.map do |mon|
+ mon.holidays=:ja_ja
+ mon.color_format(:week, 0)[1..-1]
+ end
+ left, center, right = align_size(left, center, right)
+ out << left.zip(center, right).map { |line| line.join(" ") }
+ end
+ year_label = "#{@year}".center(@@month_width*3+4).yellow
+ out.unshift year_label
+ end
+
+ private
+ def align_size(*args)
+ max = args.map(&:size).max
+ if args[0].size != max
+ args[0] << " " * @@month_width
+ end
+ if args[1].size != max
+ args[1] << " " * @@month_width
+ end
+ args
+ end
+
+ end
+end
View
16 test/tc_year.rb
@@ -0,0 +1,16 @@
+require "test/unit"
+
+require_relative "../lib/year"
+
+class TestYear < Test::Unit::TestCase
+ def setup
+ @y2010 = Caline::Year.new(2010)
+ end
+ def test_holidays
+ @y2010.holidays = :ja_ja
+ end
+
+ def test_format
+ puts @y2010.format
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.