kalenteri 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +61 -0
- data/Rakefile +56 -0
- data/VERSION +1 -0
- data/kalenteri.gemspec +57 -0
- data/lib/kalenteri.rb +19 -0
- data/lib/kalenteri/calendar.rb +89 -0
- data/lib/kalenteri/html_calendar.rb +111 -0
- data/test/calendar_test.rb +92 -0
- data/test/html_calendar_test.rb +62 -0
- data/test/test_helper.rb +11 -0
- metadata +79 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Antti Koskinen
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
= kalenteri
|
2
|
+
|
3
|
+
== Description
|
4
|
+
|
5
|
+
A gem for building calendars. Designed to be framework-agnostic.
|
6
|
+
|
7
|
+
At the moment this library is mostly for html generation. However, it should
|
8
|
+
be easy to extend the Calendar class for other purposes.
|
9
|
+
|
10
|
+
|
11
|
+
== Install
|
12
|
+
|
13
|
+
Add Github as gem source (unless you already have it)
|
14
|
+
$ gem sources -a http://gems.github.com
|
15
|
+
|
16
|
+
$ sudo gem install ajk-kalenteri
|
17
|
+
|
18
|
+
|
19
|
+
== Synopsis
|
20
|
+
|
21
|
+
require 'kalenteri'
|
22
|
+
|
23
|
+
# creates a new calendar object with monday as first weekday.
|
24
|
+
cal = Kalenteri::HTMLCalendar(Kalenteri::MONDAY)
|
25
|
+
|
26
|
+
# A HTML table for single month...
|
27
|
+
basic_month = cal.format_month(2009, 10)
|
28
|
+
# ...and for the whole year.
|
29
|
+
basic_year = cal.format_year(2009)
|
30
|
+
|
31
|
+
# You can use a block with the format method.
|
32
|
+
# A Date object will be passed for each day.
|
33
|
+
# The block should return a string which will be
|
34
|
+
# used inside the table cells.
|
35
|
+
detailed_month = cal.format_month(2009, 10) do |day|
|
36
|
+
my_day_details(day)
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
== Support
|
41
|
+
|
42
|
+
Bugs? Problems? Questions?
|
43
|
+
|
44
|
+
4ntti.koskinen@gmail.com
|
45
|
+
|
46
|
+
|
47
|
+
== Note on Patches/Pull Requests
|
48
|
+
|
49
|
+
* Fork the project.
|
50
|
+
* Make your feature addition or bug fix.
|
51
|
+
* Add tests for it. This is important so I don't break it in a
|
52
|
+
future version unintentionally.
|
53
|
+
* Commit, do not mess with rakefile, version, or history.
|
54
|
+
(if you want to have your own version, that is fine but
|
55
|
+
bump version in a commit by itself I can ignore when I pull)
|
56
|
+
* Send me a pull request. Bonus points for topic branches.
|
57
|
+
|
58
|
+
|
59
|
+
== Copyright
|
60
|
+
|
61
|
+
Copyright (c) 2009 Antti Koskinen. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "kalenteri"
|
8
|
+
gem.summary = %Q{General purpose library for building calendars}
|
9
|
+
#gem.description = %Q{}
|
10
|
+
gem.email = "4ntti.koskinen@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/ajk/kalenteri"
|
12
|
+
gem.authors = ["Antti Koskinen"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/*_test.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/*_test.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
task :test => :check_dependencies
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
if File.exist?('VERSION')
|
47
|
+
version = File.read('VERSION')
|
48
|
+
else
|
49
|
+
version = ""
|
50
|
+
end
|
51
|
+
|
52
|
+
rdoc.rdoc_dir = 'rdoc'
|
53
|
+
rdoc.title = "kalenteri #{version}"
|
54
|
+
rdoc.rdoc_files.include('README*')
|
55
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
56
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
data/kalenteri.gemspec
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{kalenteri}
|
8
|
+
s.version = "0.1.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Antti Koskinen"]
|
12
|
+
s.date = %q{2009-10-04}
|
13
|
+
s.email = %q{4ntti.koskinen@gmail.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"LICENSE",
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".document",
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"kalenteri.gemspec",
|
26
|
+
"lib/kalenteri.rb",
|
27
|
+
"lib/kalenteri/calendar.rb",
|
28
|
+
"lib/kalenteri/html_calendar.rb",
|
29
|
+
"test/calendar_test.rb",
|
30
|
+
"test/html_calendar_test.rb",
|
31
|
+
"test/test_helper.rb"
|
32
|
+
]
|
33
|
+
s.has_rdoc = true
|
34
|
+
s.homepage = %q{http://github.com/ajk/kalenteri}
|
35
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.3.1}
|
38
|
+
s.summary = %q{General purpose library for building calendars}
|
39
|
+
s.test_files = [
|
40
|
+
"test/test_helper.rb",
|
41
|
+
"test/calendar_test.rb",
|
42
|
+
"test/html_calendar_test.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 2
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
53
|
+
end
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
56
|
+
end
|
57
|
+
end
|
data/lib/kalenteri.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'kalenteri/calendar'
|
4
|
+
require 'kalenteri/html_calendar'
|
5
|
+
|
6
|
+
module Kalenteri
|
7
|
+
|
8
|
+
VERSION = "0.1.1"
|
9
|
+
|
10
|
+
SUNDAY = 0
|
11
|
+
MONDAY = 1
|
12
|
+
TUESDAY = 2
|
13
|
+
WEDNESDAY = 3
|
14
|
+
THURSDAY = 4
|
15
|
+
FRIDAY = 5
|
16
|
+
SATURDAY = 6
|
17
|
+
|
18
|
+
end
|
19
|
+
|
@@ -0,0 +1,89 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
module Kalenteri
|
6
|
+
class Calendar
|
7
|
+
|
8
|
+
attr_reader :first_weekday
|
9
|
+
attr_accessor :day_name, :day_abbr, :month_name, :month_abbr
|
10
|
+
|
11
|
+
def initialize(day=Kalenteri::SUNDAY)
|
12
|
+
# @firstweekday is an integer specifying the first day of the week.
|
13
|
+
# Sunday is day-of-week 0; Saturday is day-of-week 6.
|
14
|
+
if (0..6).include?(day)
|
15
|
+
@first_weekday = day
|
16
|
+
else
|
17
|
+
raise ArgumentError, "Day must be an integer between 0 and 6."
|
18
|
+
end
|
19
|
+
@day_name = Date::DAYNAMES.dup
|
20
|
+
@day_abbr = Date::ABBR_DAYNAMES.dup
|
21
|
+
@month_name = Date::MONTHNAMES.dup
|
22
|
+
@month_abbr = Date::ABBR_MONTHNAMES.dup
|
23
|
+
end
|
24
|
+
|
25
|
+
# Day numbers in one week.
|
26
|
+
# The first value will be the @firstweekday.
|
27
|
+
def weekdays
|
28
|
+
[0,1,2,3,4,5,6,0,1,2,3,4,5][@first_weekday, 7].each { |x|
|
29
|
+
yield(x) if block_given?
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
# An iterator for all days in the month specified as Date objects.
|
34
|
+
# First and last days yielded are possibly in previous or next months,
|
35
|
+
# so that full weeks are generated.
|
36
|
+
def iter_month_dates(year, month)
|
37
|
+
firstd = Date.new(year, month, 1)
|
38
|
+
lastd = Date.new(year, month, -1)
|
39
|
+
first_in_monthcal(firstd).upto(last_in_monthcal(lastd)) do |d|
|
40
|
+
yield(d)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# First weekday (as Date object) of the first week of the month
|
45
|
+
# as specified by @firstweekday
|
46
|
+
def first_in_monthcal(first_day)
|
47
|
+
while first_day.wday != @first_weekday
|
48
|
+
first_day -= 1
|
49
|
+
end
|
50
|
+
return first_day
|
51
|
+
end
|
52
|
+
|
53
|
+
# Last weekday (as Date object) of the last week of the month
|
54
|
+
# as specified by @firstweekday.
|
55
|
+
def last_in_monthcal(last_day)
|
56
|
+
last_weekday = weekdays().last
|
57
|
+
while last_day.wday != last_weekday
|
58
|
+
last_day += 1
|
59
|
+
end
|
60
|
+
return last_day
|
61
|
+
end
|
62
|
+
|
63
|
+
# Return a array of the weeks in the month of the year as full weeks.
|
64
|
+
# Weeks are arrays of seven Date objects.
|
65
|
+
def month_calendar(year, month)
|
66
|
+
dates = []
|
67
|
+
iter_month_dates(year, month) do |day|
|
68
|
+
dates << day
|
69
|
+
end
|
70
|
+
return split_to_weeks(dates)
|
71
|
+
end
|
72
|
+
|
73
|
+
def split_to_weeks(dates, weeks=[])
|
74
|
+
if dates.size % 7 != 0 # DEBUG
|
75
|
+
raise 'Calendar month is not divisible to weeks!'
|
76
|
+
end
|
77
|
+
return weeks if dates.empty?
|
78
|
+
weeks << dates[0...7]
|
79
|
+
return split_to_weeks(dates[7..-1], weeks)
|
80
|
+
end
|
81
|
+
|
82
|
+
def year_rows
|
83
|
+
[1,4,7,10].each do |first|
|
84
|
+
yield(first, first+1, first+2)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
@@ -0,0 +1,111 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
# Creates caledars as HTML tables.
|
4
|
+
module Kalenteri
|
5
|
+
class HTMLCalendar < Calendar
|
6
|
+
|
7
|
+
attr_accessor :day_classes, :month_header, :year_header
|
8
|
+
|
9
|
+
def initialize(weekday=Kalenteri::SUNDAY)
|
10
|
+
super(weekday)
|
11
|
+
@day_classes= %w[sun mon tue wed thu fri sat]
|
12
|
+
if defined?(I18n)
|
13
|
+
rails_i18n!
|
14
|
+
end
|
15
|
+
@today = Time.respond_to?(:zone) && Time.zone ? Time.zone.now.to_date : Date.today
|
16
|
+
end
|
17
|
+
|
18
|
+
def rails_i18n!
|
19
|
+
@day_name = begin
|
20
|
+
I18n.t("date.day_names", :raise => true).dup
|
21
|
+
rescue I18n::MissingTranslationData
|
22
|
+
Date::DAYNAMES.dup
|
23
|
+
end
|
24
|
+
@day_abbr = begin
|
25
|
+
I18n.t("date.abbr_day_names", :raise => true).dup
|
26
|
+
rescue I18n::MissingTranslationData
|
27
|
+
Date::ABBR_DAYNAMES.dup
|
28
|
+
end
|
29
|
+
@month_name = begin
|
30
|
+
I18n.t("date.month_names", :raise => true).dup
|
31
|
+
rescue I18n::MissingTranslationData
|
32
|
+
Date::MONTHNAMES.dup
|
33
|
+
end
|
34
|
+
@month_abbr = begin
|
35
|
+
I18n.t("date.abbr_month_names", :raise => true).dup
|
36
|
+
rescue I18n::MissingTranslationData
|
37
|
+
Date::ABBR_MONTHNAMES.dup
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Returns a calendar for the given year as nested HTML tables,
|
42
|
+
# three months per row.
|
43
|
+
def format_year(year=@today.year, &block)
|
44
|
+
@year_header ||= lambda { |year|
|
45
|
+
%(<tr><th colspan="3" class="year">#{year}</th></tr>)
|
46
|
+
}
|
47
|
+
# use simple month name formatter
|
48
|
+
@month_header ||= lambda { |year, month|
|
49
|
+
%(<tr><th colspan="7" class="month">#{@month_name[month]}</th></tr>\n)
|
50
|
+
}
|
51
|
+
cal = %(<table border="0" cellpadding="0" cellspacing="0" class="year">\n)
|
52
|
+
cal << @year_header[year]
|
53
|
+
self.year_rows do |c1, c2, c3|
|
54
|
+
cal << "<tr>"
|
55
|
+
cal << %(<td class="month" valign="top">#{format_month(year, c1, &block)}</td>)
|
56
|
+
cal << %(<td class="month" valign="top">#{format_month(year, c2, &block)}</td>)
|
57
|
+
cal << %(<td class="month" valign="top">#{format_month(year, c3, &block)}</td>)
|
58
|
+
cal << "</tr>"
|
59
|
+
end
|
60
|
+
cal << "</table>"
|
61
|
+
return cal
|
62
|
+
end
|
63
|
+
|
64
|
+
# Returns a month's calendar as an HTML table.
|
65
|
+
def format_month(year=@today.year, month=@today.month, &block)
|
66
|
+
@year = year
|
67
|
+
@month = month
|
68
|
+
if block.nil?
|
69
|
+
block = lambda {|d| d.month == @month ? d.mday : ' '}
|
70
|
+
end
|
71
|
+
|
72
|
+
# How month name header should be presented. You can replace this with your own
|
73
|
+
# for e.g. next and previous month links.
|
74
|
+
@month_header ||= lambda { |year, month|
|
75
|
+
%(<tr><th colspan="7" class="month">#{@month_name[month]} #{year}</th></tr>\n)
|
76
|
+
}
|
77
|
+
|
78
|
+
cal = %(<table border="0" cellpadding="0" cellspacing="0" class="month">\n)
|
79
|
+
cal << @month_header[@year, @month]
|
80
|
+
cal << format_week_header()
|
81
|
+
self.month_calendar(@year, @month).each do |days|
|
82
|
+
cal << format_week(days, block)
|
83
|
+
end
|
84
|
+
cal << "</table>"
|
85
|
+
return cal
|
86
|
+
end
|
87
|
+
|
88
|
+
def format_week_header
|
89
|
+
wh = "<tr>\n"
|
90
|
+
self.weekdays() do |d|
|
91
|
+
wh << %(<th scope="col" class="#{@day_classes[d]}">#{@day_abbr[d]}</th>\n)
|
92
|
+
end
|
93
|
+
wh << "</tr>\n"
|
94
|
+
return wh
|
95
|
+
end
|
96
|
+
|
97
|
+
def format_week(the_week, block)
|
98
|
+
wr = "<tr>\n"
|
99
|
+
the_week.each do |day|
|
100
|
+
klass = @day_classes[day.wday]
|
101
|
+
klass += ' today' if day.to_s == @today.to_s
|
102
|
+
klass += ' noday' if day.month != @month
|
103
|
+
wr << %(<td class="#{klass}">#{block.call(day)}</td>\n)
|
104
|
+
end
|
105
|
+
wr << "</tr>\n"
|
106
|
+
return wr
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class CalendarTest < Test::Unit::TestCase
|
6
|
+
include Kalenteri
|
7
|
+
|
8
|
+
context "Weekday list" do
|
9
|
+
should "have length of seven" do
|
10
|
+
(SUNDAY..SATURDAY).each do |day|
|
11
|
+
cal = Calendar.new(day)
|
12
|
+
assert_equal 7, cal.weekdays.size
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
should "have proper elements" do
|
17
|
+
cal = Calendar.new(SUNDAY)
|
18
|
+
assert_equal SUNDAY, cal.weekdays.first
|
19
|
+
assert_equal SATURDAY, cal.weekdays.last
|
20
|
+
|
21
|
+
cal = Calendar.new(MONDAY)
|
22
|
+
assert_equal MONDAY, cal.weekdays.first
|
23
|
+
assert_equal SUNDAY, cal.weekdays.last
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "In September 2009" do
|
28
|
+
|
29
|
+
context "first day of calendar page" do
|
30
|
+
setup do
|
31
|
+
@day = Date.parse("2009-09-1")
|
32
|
+
@cal = Calendar.new()
|
33
|
+
@first = @cal.first_in_monthcal(@day)
|
34
|
+
end
|
35
|
+
|
36
|
+
should "not be first day of month" do
|
37
|
+
assert_equal TUESDAY, @day.wday
|
38
|
+
assert_not_equal @day, @first
|
39
|
+
end
|
40
|
+
|
41
|
+
should "be in previous month" do
|
42
|
+
assert_equal @first.mon, @day.mon - 1
|
43
|
+
end
|
44
|
+
|
45
|
+
should "be sunday" do
|
46
|
+
assert_equal SUNDAY, @first.wday
|
47
|
+
assert_equal @cal.first_weekday, @first.wday
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "last day of calendar page" do
|
52
|
+
setup do
|
53
|
+
@day = Date.parse("2009-09-30")
|
54
|
+
@cal = Calendar.new(SUNDAY)
|
55
|
+
@last = @cal.last_in_monthcal(@day)
|
56
|
+
end
|
57
|
+
|
58
|
+
should "not be last day of month" do
|
59
|
+
assert_equal WEDNESDAY, @day.wday
|
60
|
+
assert_not_equal @day, @last
|
61
|
+
end
|
62
|
+
|
63
|
+
should "be in next month" do
|
64
|
+
assert_equal @last.mon, @day.mon + 1
|
65
|
+
end
|
66
|
+
|
67
|
+
should "be saturday" do
|
68
|
+
assert_equal SATURDAY, @last.wday
|
69
|
+
assert_equal @cal.weekdays.last, @last.wday
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context "month array" do
|
74
|
+
setup do
|
75
|
+
cal = Calendar.new(SUNDAY)
|
76
|
+
@month = cal.month_calendar(2009, 9)
|
77
|
+
end
|
78
|
+
|
79
|
+
should "have five weeks" do
|
80
|
+
assert_equal 5, @month.size
|
81
|
+
end
|
82
|
+
|
83
|
+
should "have seven days in each week" do
|
84
|
+
@month.each do |week|
|
85
|
+
assert_equal 7, week.size
|
86
|
+
week.each {|day| assert_kind_of(Date, day) }
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
require 'nokogiri'
|
5
|
+
require 'open-uri'
|
6
|
+
|
7
|
+
class HTMLCalendarTest < Test::Unit::TestCase
|
8
|
+
include Kalenteri
|
9
|
+
|
10
|
+
context "Html output for August 2009" do
|
11
|
+
setup do
|
12
|
+
@cal = HTMLCalendar.new(Kalenteri::SUNDAY)
|
13
|
+
@cal.month_header = lambda {|year, month|
|
14
|
+
%(<tr><th>prev</th><th colspan="5" class="month">My #{@cal.month_name[month]} #{year}</th><th>next</th></tr>\n)
|
15
|
+
}
|
16
|
+
table = @cal.format_month(2009, 8) do |day|
|
17
|
+
if day.month != 8
|
18
|
+
"not August"
|
19
|
+
else
|
20
|
+
day.mday
|
21
|
+
end
|
22
|
+
end
|
23
|
+
@xhtml = <<-EOF
|
24
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
25
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
26
|
+
<html xmlns="http://www.w3.org/1999/xhtml">
|
27
|
+
<head><title></title></head>
|
28
|
+
<body>#{table}</body>
|
29
|
+
</html>
|
30
|
+
EOF
|
31
|
+
@doc = Nokogiri::HTML(@xhtml)
|
32
|
+
end
|
33
|
+
|
34
|
+
should "be a table" do
|
35
|
+
assert_equal "html > body > table", @doc.css('body').children.first.css_path
|
36
|
+
end
|
37
|
+
|
38
|
+
should "be valid xhtml document" do
|
39
|
+
xsd = Nokogiri::XML::Schema(open('http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd'))
|
40
|
+
assert_equal [], xsd.validate(Nokogiri::XML(@xhtml)) # This gives a list of errors unless valid
|
41
|
+
end
|
42
|
+
|
43
|
+
should "use overridden month header" do
|
44
|
+
header = @doc.css('th.month').first
|
45
|
+
assert_equal 5, header[:colspan].to_i
|
46
|
+
assert_equal "My August 2009", header.inner_html
|
47
|
+
end
|
48
|
+
|
49
|
+
should "have a css class for each day of week" do
|
50
|
+
classes = @doc.css('td').remove_class('noday').map {|td| td[:class]}.uniq
|
51
|
+
assert_equal @cal.day_classes, classes
|
52
|
+
assert_equal @cal.day_classes, %w[sun mon tue wed thu fri sat]
|
53
|
+
end
|
54
|
+
|
55
|
+
should "mark out-of-month days with 'noday'-class" do
|
56
|
+
oom = @doc.css('td.noday')
|
57
|
+
assert_equal 11, oom.size
|
58
|
+
assert oom.all? {|td| td.inner_html == "not August" }
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kalenteri
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Antti Koskinen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-04 00:00:00 +03:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: thoughtbot-shoulda
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: 4ntti.koskinen@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- kalenteri.gemspec
|
42
|
+
- lib/kalenteri.rb
|
43
|
+
- lib/kalenteri/calendar.rb
|
44
|
+
- lib/kalenteri/html_calendar.rb
|
45
|
+
- test/calendar_test.rb
|
46
|
+
- test/html_calendar_test.rb
|
47
|
+
- test/test_helper.rb
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://github.com/ajk/kalenteri
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options:
|
54
|
+
- --charset=UTF-8
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.3.5
|
73
|
+
signing_key:
|
74
|
+
specification_version: 2
|
75
|
+
summary: General purpose library for building calendars
|
76
|
+
test_files:
|
77
|
+
- test/test_helper.rb
|
78
|
+
- test/calendar_test.rb
|
79
|
+
- test/html_calendar_test.rb
|