cal 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/cal.gemspec +1 -1
- data/lib/cal.rb +4 -14
- data/lib/cal/day.rb +1 -2
- data/lib/cal/day_name.rb +1 -3
- data/lib/cal/month.rb +1 -3
- data/lib/cal/monthly_calendar.rb +10 -23
- data/lib/cal/version.rb +2 -4
- data/spec/cal/day_name_spec.rb +94 -103
- data/spec/cal/day_spec.rb +16 -19
- data/spec/cal/month_spec.rb +31 -32
- data/spec/cal/monthly_calendar_spec.rb +114 -166
- data/spec/cal_spec.rb +5 -10
- data/spec/spec_helper.rb +0 -1
- metadata +11 -17
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 448f52e733440c6f89a21075c476e96848eb7dfb
|
4
|
+
data.tar.gz: 3e02e40c42d4f3444c7792f5d0476b5bdf9fecd2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4ed601f9a089ae1809ea588166b71fee311a8bb27e63c2845420cc2beb1fafdf3798111ab72051feaf5d03372532c7e117014f71580a094c72f7b499a418f30b
|
7
|
+
data.tar.gz: f6b66ca1dec81bc922dcac0b6527b0b48bba101e3c6a245873c1e8d1836a14024613fb6b90328673444af3eaf9ff7d41dbd7e050411fa4740745292930f5ee1a
|
data/.gitignore
CHANGED
data/cal.gemspec
CHANGED
data/lib/cal.rb
CHANGED
@@ -1,17 +1,7 @@
|
|
1
|
-
require
|
2
|
-
require 'active_support/time'
|
1
|
+
require 'cal/monthly_calendar'
|
3
2
|
|
4
3
|
module Cal
|
5
|
-
|
6
|
-
|
7
|
-
autoload :Day, 'cal/day'
|
8
|
-
autoload :Month, 'cal/month'
|
9
|
-
autoload :DayName, 'cal/day_name'
|
10
|
-
|
11
|
-
class << self
|
12
|
-
def new_monthly_calendar(*args)
|
13
|
-
MonthlyCalendar.new *args
|
14
|
-
end
|
4
|
+
def self.new_monthly_calendar(*args)
|
5
|
+
MonthlyCalendar.new *args
|
15
6
|
end
|
16
|
-
|
17
|
-
end
|
7
|
+
end
|
data/lib/cal/day.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'active_support/core_ext/module/delegation'
|
2
|
+
require 'active_support/time'
|
2
3
|
|
3
4
|
module Cal
|
4
5
|
class Day
|
5
|
-
|
6
6
|
include Comparable
|
7
7
|
|
8
8
|
def initialize(date, calendar)
|
@@ -29,6 +29,5 @@ module Cal
|
|
29
29
|
def number
|
30
30
|
date.day
|
31
31
|
end
|
32
|
-
|
33
32
|
end
|
34
33
|
end
|
data/lib/cal/day_name.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
module Cal
|
2
2
|
class DayName
|
3
|
-
|
4
3
|
class << self
|
5
4
|
def all(options = {})
|
6
5
|
if options[:start_on]
|
@@ -38,7 +37,7 @@ module Cal
|
|
38
37
|
@saturday ||= new :saturday, 'Saturday', 7
|
39
38
|
end
|
40
39
|
|
41
|
-
|
40
|
+
private
|
42
41
|
|
43
42
|
def sunday_to_saturday
|
44
43
|
@sunday_to_saturday ||= [sunday, monday, tuesday, wednesday, thursday, friday, saturday]
|
@@ -68,6 +67,5 @@ module Cal
|
|
68
67
|
day_name.position == (position > 1 ? position - 1 : 7)
|
69
68
|
end
|
70
69
|
end
|
71
|
-
|
72
70
|
end
|
73
71
|
end
|
data/lib/cal/month.rb
CHANGED
data/lib/cal/monthly_calendar.rb
CHANGED
@@ -1,28 +1,14 @@
|
|
1
1
|
require 'active_support/core_ext/array/grouping'
|
2
2
|
require 'active_support/core_ext/string/conversions'
|
3
|
+
require 'active_support/time'
|
4
|
+
require 'cal/day'
|
5
|
+
require 'cal/day_name'
|
6
|
+
require 'cal/month'
|
3
7
|
|
4
8
|
module Cal
|
5
9
|
class MonthlyCalendar
|
6
|
-
|
7
10
|
include Comparable
|
8
11
|
|
9
|
-
class << self
|
10
|
-
def from_month(month, options = {})
|
11
|
-
month = month.to_month
|
12
|
-
new month.year, month.number, options
|
13
|
-
end
|
14
|
-
|
15
|
-
def from_param(param, options = {})
|
16
|
-
year, month_number = if param.present?
|
17
|
-
param.split '-'
|
18
|
-
else
|
19
|
-
now = Date.current
|
20
|
-
[now.year, now.month]
|
21
|
-
end
|
22
|
-
new year, month_number, options
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
12
|
def initialize(year, month_number, options = {})
|
27
13
|
@start_week_on = options[:start_week_on] || :sunday
|
28
14
|
@month = Month.new year, month_number
|
@@ -33,7 +19,7 @@ module Cal
|
|
33
19
|
delegate :year, :to => :month
|
34
20
|
|
35
21
|
def <=>(other)
|
36
|
-
if other.is_a?(
|
22
|
+
if other.is_a?(self.class) && start_week_on == other.send(:start_week_on)
|
37
23
|
month <=> other.month
|
38
24
|
end
|
39
25
|
end
|
@@ -61,11 +47,13 @@ module Cal
|
|
61
47
|
end
|
62
48
|
|
63
49
|
def previous
|
64
|
-
|
50
|
+
previous_month = month.previous
|
51
|
+
self.class.new previous_month.year, previous_month.number, :start_week_on => start_week_on
|
65
52
|
end
|
66
53
|
|
67
54
|
def next
|
68
|
-
|
55
|
+
next_month = month.succ
|
56
|
+
self.class.new next_month.year, next_month.number, :start_week_on => start_week_on
|
69
57
|
end
|
70
58
|
|
71
59
|
def day_names
|
@@ -76,9 +64,8 @@ module Cal
|
|
76
64
|
"#{year}-#{month.to_i}"
|
77
65
|
end
|
78
66
|
|
79
|
-
|
67
|
+
private
|
80
68
|
|
81
69
|
attr_reader :start_week_on
|
82
|
-
|
83
70
|
end
|
84
71
|
end
|
data/lib/cal/version.rb
CHANGED
data/spec/cal/day_name_spec.rb
CHANGED
@@ -1,134 +1,125 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'cal/day_name'
|
2
3
|
|
3
4
|
describe Cal::DayName do
|
5
|
+
describe ".all" do
|
6
|
+
it "returns all the days, starting with sunday" do
|
7
|
+
Cal::DayName.all.should == [
|
8
|
+
Cal::DayName.sunday,
|
9
|
+
Cal::DayName.monday,
|
10
|
+
Cal::DayName.tuesday,
|
11
|
+
Cal::DayName.wednesday,
|
12
|
+
Cal::DayName.thursday,
|
13
|
+
Cal::DayName.friday,
|
14
|
+
Cal::DayName.saturday
|
15
|
+
]
|
16
|
+
end
|
4
17
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
subject.friday,
|
17
|
-
subject.saturday
|
18
|
-
]
|
19
|
-
end
|
18
|
+
it "when given a day name symbol as the :start_on option, it starts the week on that day" do
|
19
|
+
Cal::DayName.all(:start_on => :tuesday).should == [
|
20
|
+
Cal::DayName.tuesday,
|
21
|
+
Cal::DayName.wednesday,
|
22
|
+
Cal::DayName.thursday,
|
23
|
+
Cal::DayName.friday,
|
24
|
+
Cal::DayName.saturday,
|
25
|
+
Cal::DayName.sunday,
|
26
|
+
Cal::DayName.monday
|
27
|
+
]
|
28
|
+
end
|
20
29
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
+
it "accepts the :start_on option in various formats" do
|
31
|
+
[:tuesday, 'Tuesday', 'tuesday'].each do |start_on|
|
32
|
+
Cal::DayName.all(:start_on => start_on).should == [
|
33
|
+
Cal::DayName.tuesday,
|
34
|
+
Cal::DayName.wednesday,
|
35
|
+
Cal::DayName.thursday,
|
36
|
+
Cal::DayName.friday,
|
37
|
+
Cal::DayName.saturday,
|
38
|
+
Cal::DayName.sunday,
|
39
|
+
Cal::DayName.monday
|
30
40
|
]
|
31
41
|
end
|
32
|
-
|
33
|
-
it "accepts the :start_on option in various formats" do
|
34
|
-
[:tuesday, 'Tuesday', 'tuesday'].each do |start_on|
|
35
|
-
subject.all(:start_on => start_on).should == [
|
36
|
-
subject.tuesday,
|
37
|
-
subject.wednesday,
|
38
|
-
subject.thursday,
|
39
|
-
subject.friday,
|
40
|
-
subject.saturday,
|
41
|
-
subject.sunday,
|
42
|
-
subject.monday
|
43
|
-
]
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
it "without the :start_on option, it returns the same object" do
|
48
|
-
subject.all.equal?(subject.all).should be_true
|
49
|
-
end
|
50
42
|
end
|
51
43
|
|
52
|
-
it "
|
53
|
-
|
44
|
+
it "without the :start_on option, it returns the same object" do
|
45
|
+
Cal::DayName.all.equal?(Cal::DayName.all).should be_true
|
54
46
|
end
|
55
47
|
end
|
56
48
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
[:monday, 'Monday', 2],
|
61
|
-
[:tuesday, 'Tuesday', 3],
|
62
|
-
[:wednesday, 'Wednesday', 4],
|
63
|
-
[:thursday, 'Thursday', 5],
|
64
|
-
[:friday, 'Friday', 6],
|
65
|
-
[:saturday, 'Saturday', 7]
|
66
|
-
].each do |day, string, position|
|
67
|
-
context "when the day is #{day}" do
|
68
|
-
subject { described_class.send day }
|
49
|
+
it "doesn't allow direct initialization" do
|
50
|
+
expect { Cal::DayName.new }.to raise_error NoMethodError
|
51
|
+
end
|
69
52
|
|
70
|
-
|
71
|
-
|
72
|
-
|
53
|
+
[
|
54
|
+
[:sunday, 'Sunday', 1],
|
55
|
+
[:monday, 'Monday', 2],
|
56
|
+
[:tuesday, 'Tuesday', 3],
|
57
|
+
[:wednesday, 'Wednesday', 4],
|
58
|
+
[:thursday, 'Thursday', 5],
|
59
|
+
[:friday, 'Friday', 6],
|
60
|
+
[:saturday, 'Saturday', 7]
|
61
|
+
].each do |day, string, position|
|
62
|
+
context "when the day is #{day}" do
|
63
|
+
it "is the same instance as another #{day}" do
|
64
|
+
Cal::DayName.send(day).equal?(Cal::DayName.send(day)).should be_true
|
65
|
+
end
|
73
66
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
end
|
67
|
+
describe "to_sym" do
|
68
|
+
it "is #{day.inspect}" do
|
69
|
+
Cal::DayName.send(day).to_sym.should == day
|
78
70
|
end
|
71
|
+
end
|
79
72
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
end
|
73
|
+
describe "to_s" do
|
74
|
+
it "is #{string.inspect}" do
|
75
|
+
Cal::DayName.send(day).to_s.should == string
|
84
76
|
end
|
77
|
+
end
|
85
78
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
end
|
79
|
+
describe "position" do
|
80
|
+
it "is #{position.inspect}" do
|
81
|
+
Cal::DayName.send(day).position.should == position
|
90
82
|
end
|
91
83
|
end
|
92
84
|
end
|
85
|
+
end
|
93
86
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
end
|
87
|
+
describe "#succ" do
|
88
|
+
[
|
89
|
+
[:sunday, :monday],
|
90
|
+
[:monday, :tuesday],
|
91
|
+
[:tuesday, :wednesday],
|
92
|
+
[:wednesday, :thursday],
|
93
|
+
[:thursday, :friday],
|
94
|
+
[:friday, :saturday],
|
95
|
+
[:saturday, :sunday]
|
96
|
+
].each do |day, next_day|
|
97
|
+
it "is #{next_day} when the say is #{day}" do
|
98
|
+
Cal::DayName.send(day).succ.should == Cal::DayName.send(next_day)
|
107
99
|
end
|
108
100
|
end
|
101
|
+
end
|
109
102
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
end
|
103
|
+
describe "#next" do
|
104
|
+
it "is an alias of #succ" do
|
105
|
+
day_name = Cal::DayName.friday
|
106
|
+
day_name.next.should == day_name.succ
|
115
107
|
end
|
108
|
+
end
|
116
109
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
end
|
110
|
+
describe "#previous" do
|
111
|
+
[
|
112
|
+
[:sunday, :saturday],
|
113
|
+
[:monday, :sunday],
|
114
|
+
[:tuesday, :monday],
|
115
|
+
[:wednesday, :tuesday],
|
116
|
+
[:thursday, :wednesday],
|
117
|
+
[:friday, :thursday],
|
118
|
+
[:saturday, :friday]
|
119
|
+
].each do |day, previous_day|
|
120
|
+
it "is #{previous_day} when the say is #{day}" do
|
121
|
+
Cal::DayName.send(day).previous.should == Cal::DayName.send(previous_day)
|
130
122
|
end
|
131
123
|
end
|
132
124
|
end
|
133
|
-
|
134
125
|
end
|
data/spec/cal/day_spec.rb
CHANGED
@@ -1,63 +1,60 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'cal/day'
|
2
3
|
|
3
4
|
describe Cal::Day do
|
4
|
-
|
5
|
-
subject { described_class.new @date, @calendar }
|
6
|
-
|
7
5
|
before do
|
8
6
|
@date = Date.new 2012, 1, 15
|
9
7
|
@calendar = OpenStruct.new :current_day => Date.new(2012, 1, 5)
|
10
8
|
end
|
11
9
|
|
12
|
-
it { should be_a(Comparable) }
|
10
|
+
it { Cal::Day.new(@date, @calendar).should be_a(Comparable) }
|
13
11
|
|
14
|
-
describe "
|
12
|
+
describe "#==" do
|
15
13
|
it "is true with another day with the same calendar and date" do
|
16
|
-
|
14
|
+
Cal::Day.new(@date, @calendar).should == Cal::Day.new(@date, @calendar)
|
17
15
|
end
|
18
16
|
|
19
17
|
it "is false with another day with a different calendar" do
|
20
|
-
|
18
|
+
Cal::Day.new(@date, @calendar).should_not == Cal::Day.new(@date, Object.new)
|
21
19
|
end
|
22
20
|
|
23
21
|
it "is false with another day with a different date" do
|
24
|
-
|
22
|
+
Cal::Day.new(@date, @calendar).should_not == Cal::Day.new(Date.new(2012, 1, 14), @calendar)
|
25
23
|
end
|
26
24
|
|
27
25
|
it "is false with a non Cal::Day" do
|
28
|
-
|
26
|
+
Cal::Day.new(@date, @calendar).should_not == Object.new
|
29
27
|
end
|
30
28
|
end
|
31
29
|
|
32
|
-
describe "today?" do
|
30
|
+
describe "#today?" do
|
33
31
|
it "is true when the date is today" do
|
34
32
|
@date = Date.current
|
35
|
-
|
33
|
+
Cal::Day.new(@date, @calendar).should be_today
|
36
34
|
end
|
37
35
|
end
|
38
36
|
|
39
|
-
describe "number" do
|
37
|
+
describe "#number" do
|
40
38
|
[3, 15].each do |n|
|
41
39
|
it "is the day of the month" do
|
42
40
|
@date = Date.new 2012, 1, n
|
43
|
-
|
41
|
+
Cal::Day.new(@date, @calendar).number.should == n
|
44
42
|
end
|
45
43
|
end
|
46
44
|
end
|
47
45
|
|
48
|
-
describe "
|
46
|
+
describe "#<=>" do
|
49
47
|
it "is the result of comparing the dates" do
|
50
48
|
other_date = Date.new 2012, 2, 15
|
51
49
|
result = Object.new
|
52
|
-
|
53
|
-
(
|
50
|
+
Cal::Day.new(@date, @calendar).date.stub(:<=>) { |obj| result if obj == other_date }
|
51
|
+
(Cal::Day.new(@date, @calendar) <=> Cal::Day.new(other_date, Object.new)).should == result
|
54
52
|
end
|
55
53
|
end
|
56
54
|
|
57
|
-
describe "succ" do
|
55
|
+
describe "#succ" do
|
58
56
|
it "is the next day" do
|
59
|
-
|
57
|
+
Cal::Day.new(@date, @calendar).succ.should == Cal::Day.new(Date.new(2012, 1, 16), @calendar)
|
60
58
|
end
|
61
59
|
end
|
62
|
-
|
63
60
|
end
|
data/spec/cal/month_spec.rb
CHANGED
@@ -1,28 +1,26 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'cal/month'
|
2
3
|
|
3
4
|
describe Cal::Month do
|
4
|
-
|
5
|
-
subject { described_class.new @year, @month_number }
|
6
|
-
|
7
5
|
before do
|
8
6
|
@year = 2012
|
9
7
|
@month_number = 3
|
10
8
|
end
|
11
9
|
|
12
|
-
describe "
|
10
|
+
describe ".new" do
|
13
11
|
it "coerces the year and month number to integers" do
|
14
12
|
@year = "2012"
|
15
13
|
@month_number = "04"
|
16
|
-
|
17
|
-
|
14
|
+
Cal::Month.new(@year, @month_number).year.should == 2012
|
15
|
+
Cal::Month.new(@year, @month_number).number.should == 4
|
18
16
|
end
|
19
17
|
end
|
20
18
|
|
21
|
-
it { should be_a(Comparable) }
|
19
|
+
it { Cal::Month.new(@year, @month_number).should be_a(Comparable) }
|
22
20
|
|
23
|
-
describe "
|
21
|
+
describe "#<=>" do
|
24
22
|
it "is nil with a non month" do
|
25
|
-
(
|
23
|
+
(Cal::Month.new(@year, @month_number) <=> Object.new).should be_nil
|
26
24
|
end
|
27
25
|
|
28
26
|
context "given another month" do
|
@@ -30,93 +28,94 @@ describe Cal::Month do
|
|
30
28
|
|
31
29
|
context "when the other's year is the same" do
|
32
30
|
it "is 0 when the other's month is the same" do
|
33
|
-
(
|
31
|
+
(Cal::Month.new(@year, @month_number) <=> @other).should == 0
|
34
32
|
end
|
35
33
|
|
36
34
|
it "is -1 when the other's month is higher" do
|
37
35
|
@month_number = 2
|
38
|
-
(
|
36
|
+
(Cal::Month.new(@year, @month_number) <=> @other).should == -1
|
39
37
|
end
|
40
38
|
|
41
39
|
it "is 1 when the other's month is lower" do
|
42
40
|
@month_number = 4
|
43
|
-
(
|
41
|
+
(Cal::Month.new(@year, @month_number) <=> @other).should == 1
|
44
42
|
end
|
45
43
|
end
|
46
44
|
|
47
45
|
it "is -1 when the other's year is higher" do
|
48
46
|
@year = 2011
|
49
|
-
(
|
47
|
+
(Cal::Month.new(@year, @month_number) <=> @other).should == -1
|
50
48
|
end
|
51
49
|
|
52
50
|
it "is 1 when the other's year is lower" do
|
53
51
|
@year = 2013
|
54
|
-
(
|
52
|
+
(Cal::Month.new(@year, @month_number) <=> @other).should == 1
|
55
53
|
end
|
56
54
|
end
|
57
55
|
end
|
58
56
|
|
59
|
-
describe "
|
57
|
+
describe "#==" do
|
60
58
|
it "is false with a non month" do
|
61
|
-
(
|
59
|
+
(Cal::Month.new(@year, @month_number) == Object.new).should be_false
|
62
60
|
end
|
63
61
|
|
64
62
|
context "given another month" do
|
65
63
|
before { @month = described_class.new @year, @month_number }
|
66
64
|
|
67
65
|
it "is true if the comparison is zero" do
|
68
|
-
|
69
|
-
(
|
66
|
+
month = Cal::Month.new(@year, @month_number)
|
67
|
+
month.stub(:<=>) { 0 }
|
68
|
+
(month == @month).should be_true
|
70
69
|
end
|
71
70
|
|
72
71
|
it "is false if the comparison is not zero" do
|
73
|
-
|
74
|
-
(
|
72
|
+
month = Cal::Month.new(@year, @month_number)
|
73
|
+
month.stub(:<=>) { -1 }
|
74
|
+
(month == @month).should be_false
|
75
75
|
end
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
79
|
-
it {
|
79
|
+
it { Cal::Month.new(@year, @month_number).number.should == @month_number }
|
80
80
|
|
81
|
-
describe "to_s" do
|
81
|
+
describe "#to_s" do
|
82
82
|
it "is the result of formatting a date object" do
|
83
83
|
date = Object.new
|
84
84
|
format = Object.new
|
85
85
|
result = Object.new
|
86
86
|
Date.stub(:new) { |*args| date if args == [@year, @month_number] }
|
87
87
|
date.stub(:strftime) { |f| result if f == format }
|
88
|
-
|
88
|
+
Cal::Month.new(@year, @month_number).to_s(format).should == result
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
92
|
-
describe "year" do
|
92
|
+
describe "#year" do
|
93
93
|
it "is the given year" do
|
94
|
-
|
94
|
+
Cal::Month.new(@year, @month_number).year.should == 2012
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
98
|
-
describe "succ" do
|
98
|
+
describe "#succ" do
|
99
99
|
[[2012, 12, 2013, 1], [2012, 1, 2012, 2]].each do |year, month_number, new_year, new_month_number|
|
100
100
|
it "is the next month" do
|
101
101
|
@year = year
|
102
102
|
@month_number = month_number
|
103
|
-
|
103
|
+
Cal::Month.new(@year, @month_number).succ.should == described_class.new(new_year, new_month_number)
|
104
104
|
end
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
108
|
-
describe "previous" do
|
108
|
+
describe "#previous" do
|
109
109
|
[[2012, 12, 2012, 11], [2012, 1, 2011, 12]].each do |year, month_number, new_year, new_month_number|
|
110
110
|
it "is the previous month" do
|
111
111
|
@year = year
|
112
112
|
@month_number = month_number
|
113
|
-
|
113
|
+
Cal::Month.new(@year, @month_number).previous.should == described_class.new(new_year, new_month_number)
|
114
114
|
end
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
118
|
-
describe "to_month" do
|
119
|
-
it {
|
118
|
+
describe "#to_month" do
|
119
|
+
it { Cal::Month.new(@year, @month_number).to_month.should == Cal::Month.new(@year, @month_number) }
|
120
120
|
end
|
121
|
-
|
122
121
|
end
|
@@ -1,209 +1,157 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'cal/monthly_calendar'
|
2
3
|
|
3
4
|
describe Cal::MonthlyCalendar do
|
5
|
+
it { Cal::MonthlyCalendar.new(2012, 2).should be_a(Comparable) }
|
4
6
|
|
5
|
-
describe "
|
6
|
-
|
7
|
-
|
8
|
-
describe "from_month" do
|
9
|
-
it "returns a calendar for the month's year and month number" do
|
10
|
-
subject.from_month(Cal::Month.new(2012, 11), :start_week_on => :tuesday).should == subject.new(2012, 11, :start_week_on => :tuesday)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "raises an error without something that can be converted to a month" do
|
14
|
-
[nil, Object.new].each do |object|
|
15
|
-
expect { subject.from_month(object) }.to raise_error(NoMethodError)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
describe "from_param" do
|
21
|
-
it "is the calendar for the given param" do
|
22
|
-
calendar = subject.new 2012, 5, :start_week_on => :tuesday
|
23
|
-
subject.from_param(calendar.to_param, :start_week_on => :tuesday).should == calendar
|
24
|
-
end
|
25
|
-
|
26
|
-
context "with a blank param" do
|
27
|
-
it "returns a calendar for the current year and month" do
|
28
|
-
[' ', nil].each do |param|
|
29
|
-
date = Date.current
|
30
|
-
subject.from_param(param).should == subject.new(date.year, date.month)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
7
|
+
describe ".new" do
|
8
|
+
it "coerces the year and month number to integers" do
|
9
|
+
Cal::MonthlyCalendar.new('2012', '04').should == Cal::MonthlyCalendar.new(2012, 4)
|
34
10
|
end
|
35
11
|
end
|
36
12
|
|
37
|
-
describe "
|
38
|
-
|
39
|
-
|
40
|
-
before do
|
41
|
-
@year = 2012
|
42
|
-
@month = 2
|
43
|
-
@options = {}
|
13
|
+
describe "#<=>" do
|
14
|
+
it "is nil with a non monthly calendar" do
|
15
|
+
(Cal::MonthlyCalendar.new(2012, 2) <=> Object.new).should be_nil
|
44
16
|
end
|
45
17
|
|
46
|
-
|
47
|
-
|
48
|
-
describe "initialize" do
|
49
|
-
it "coerces the year and month number to integers" do
|
50
|
-
@year = "2012"
|
51
|
-
@month = "04"
|
52
|
-
subject.should == described_class.new(2012, 4)
|
53
|
-
end
|
54
|
-
end
|
18
|
+
context "given another monthly calendar" do
|
19
|
+
before { @other = Cal::MonthlyCalendar.new 2012, 2 }
|
55
20
|
|
56
|
-
|
57
|
-
|
58
|
-
(subject <=> Object.new).should be_nil
|
21
|
+
it "is nil when the other's start day is different" do
|
22
|
+
(Cal::MonthlyCalendar.new(2012, 2, :start_week_on => :monday) <=> @other).should be_nil
|
59
23
|
end
|
60
24
|
|
61
|
-
it "is
|
62
|
-
|
25
|
+
it "is the result of comparing the months when the other's start day is the same" do
|
26
|
+
result = Object.new
|
27
|
+
calendar = Cal::MonthlyCalendar.new(2012, 2)
|
28
|
+
calendar.month.stub(:<=>).with(@other.month) { result }
|
29
|
+
(calendar <=> @other).should == result
|
63
30
|
end
|
31
|
+
end
|
32
|
+
end
|
64
33
|
|
65
|
-
|
66
|
-
|
34
|
+
describe "#month" do
|
35
|
+
it { Cal::MonthlyCalendar.new(2012, 2).month.should == Cal::Month.new(2012, 2) }
|
36
|
+
end
|
67
37
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
end
|
38
|
+
describe "#year" do
|
39
|
+
it { Cal::MonthlyCalendar.new(2012, 2).year.should == 2012 }
|
40
|
+
end
|
72
41
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
42
|
+
describe "#first_day" do
|
43
|
+
[
|
44
|
+
[:monday, [1, 30]],
|
45
|
+
[:tuesday, [1, 31]],
|
46
|
+
[:wednesday, [2, 1]],
|
47
|
+
[:thursday, [1, 26]],
|
48
|
+
[:friday, [1, 27]],
|
49
|
+
[:saturday, [1, 28]]
|
50
|
+
].each do |weekday, month_day|
|
51
|
+
context "when the weekday is set to start on #{weekday}" do
|
52
|
+
it "is the first viewable day on the calendar" do
|
53
|
+
calendar = Cal::MonthlyCalendar.new(2012, 2, :start_week_on => weekday)
|
54
|
+
calendar.first_day.should == Cal::Day.new(Date.new(2012, *month_day), calendar)
|
77
55
|
end
|
78
56
|
end
|
79
57
|
end
|
80
58
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
describe "year" do
|
86
|
-
it { subject.year.should == @year }
|
59
|
+
it "is the first viewable day on the calendar, using sunday as the default start day" do
|
60
|
+
calendar = Cal::MonthlyCalendar.new(2012, 2)
|
61
|
+
calendar.first_day.should == Cal::Day.new(Date.new(2012, 1, 29), calendar)
|
87
62
|
end
|
63
|
+
end
|
88
64
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
[
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
it "is the first viewable day on the calendar" do
|
103
|
-
subject.first_day.should == Cal::Day.new(Date.new(2012, *month_day), subject)
|
104
|
-
end
|
65
|
+
describe "#last_day" do
|
66
|
+
[
|
67
|
+
[:monday, [3, 4]],
|
68
|
+
[:tuesday, [3, 5]],
|
69
|
+
[:wednesday, [3, 6]],
|
70
|
+
[:thursday, [2, 29]],
|
71
|
+
[:friday, [3, 1]],
|
72
|
+
[:saturday, [3, 2]]
|
73
|
+
].each do |weekday, month_day|
|
74
|
+
context "when the weekday is set to start on #{weekday}" do
|
75
|
+
it "is the last viewable day on the calendar" do
|
76
|
+
calendar = Cal::MonthlyCalendar.new(2012, 2, :start_week_on => weekday)
|
77
|
+
calendar.last_day.should == Cal::Day.new(Date.new(2012, *month_day), calendar)
|
105
78
|
end
|
106
79
|
end
|
107
|
-
|
108
|
-
it "is the first viewable day on the calendar, using sunday as the default start day" do
|
109
|
-
subject.first_day.should == Cal::Day.new(Date.new(2012, 1, 29), subject)
|
110
|
-
end
|
111
80
|
end
|
112
81
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
[
|
117
|
-
[:monday, [3, 4]],
|
118
|
-
[:tuesday, [3, 5]],
|
119
|
-
[:wednesday, [3, 6]],
|
120
|
-
[:thursday, [2, 29]],
|
121
|
-
[:friday, [3, 1]],
|
122
|
-
[:saturday, [3, 2]]
|
123
|
-
].each do |weekday, month_day|
|
124
|
-
context "when the weekday is set to start on #{weekday}" do
|
125
|
-
before { @options[:start_week_on] = weekday }
|
126
|
-
it "is the last viewable day on the calendar" do
|
127
|
-
subject.last_day.should == Cal::Day.new(Date.new(2012, *month_day), subject)
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
it "is the last viewable day on the calendar, using sunday as the default start day" do
|
133
|
-
subject.last_day.should == Cal::Day.new(Date.new(2012, 3, 3), subject)
|
134
|
-
end
|
82
|
+
it "is the last viewable day on the calendar, using sunday as the default start day" do
|
83
|
+
calendar = Cal::MonthlyCalendar.new(2012, 2)
|
84
|
+
calendar.last_day.should == Cal::Day.new(Date.new(2012, 3, 3), calendar)
|
135
85
|
end
|
86
|
+
end
|
136
87
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
end
|
88
|
+
describe "#days" do
|
89
|
+
it "is a range of the first day to the last day" do
|
90
|
+
calendar = Cal::MonthlyCalendar.new(2012, 2)
|
91
|
+
first_day = Cal::Day.new Date.new(2012, 1, 29), calendar
|
92
|
+
last_day = Cal::Day.new Date.new(2012, 3, 3), calendar
|
93
|
+
calendar.days.should == (first_day..last_day)
|
144
94
|
end
|
95
|
+
end
|
145
96
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
end
|
97
|
+
describe "#weeks" do
|
98
|
+
it "an array of the days in groups of 7" do
|
99
|
+
calendar = Cal::MonthlyCalendar.new(2012, 2)
|
100
|
+
calendar.weeks.should == [
|
101
|
+
%w[01-29 01-30 01-31 02-01 02-02 02-03 02-04].map { |s| Cal::Day.new Date.parse("2012-#{s}"), calendar },
|
102
|
+
%w[02-05 02-06 02-07 02-08 02-09 02-10 02-11].map { |s| Cal::Day.new Date.parse("2012-#{s}"), calendar },
|
103
|
+
%w[02-12 02-13 02-14 02-15 02-16 02-17 02-18].map { |s| Cal::Day.new Date.parse("2012-#{s}"), calendar },
|
104
|
+
%w[02-19 02-20 02-21 02-22 02-23 02-24 02-25].map { |s| Cal::Day.new Date.parse("2012-#{s}"), calendar },
|
105
|
+
%w[02-26 02-27 02-28 02-29 03-01 03-02 03-03].map { |s| Cal::Day.new Date.parse("2012-#{s}"), calendar }
|
106
|
+
]
|
157
107
|
end
|
108
|
+
end
|
158
109
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
end
|
110
|
+
describe "#previous" do
|
111
|
+
it "returns the calendar for the previous month" do
|
112
|
+
calendar = Cal::MonthlyCalendar.new(2012, 12, :start_week_on => :wednesday)
|
113
|
+
calendar.previous.should == Cal::MonthlyCalendar.new(2012, 11, :start_week_on => :wednesday)
|
164
114
|
end
|
115
|
+
end
|
165
116
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
end
|
117
|
+
describe "#next" do
|
118
|
+
it "returns the calendar for the next month" do
|
119
|
+
calendar = Cal::MonthlyCalendar.new(2012, 12, :start_week_on => :wednesday)
|
120
|
+
calendar.next.should == Cal::MonthlyCalendar.new(2013, 1, :start_week_on => :wednesday)
|
171
121
|
end
|
122
|
+
end
|
172
123
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
end
|
185
|
-
|
186
|
-
[
|
187
|
-
[:monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday],
|
188
|
-
[:tuesday, :wednesday, :thursday, :friday, :saturday, :sunday, :monday],
|
189
|
-
[:wednesday, :thursday, :friday, :saturday, :sunday, :monday, :tuesday],
|
190
|
-
[:thursday, :friday, :saturday, :sunday, :monday, :tuesday, :wednesday],
|
191
|
-
[:friday, :saturday, :sunday, :monday, :tuesday, :wednesday, :thursday],
|
192
|
-
[:saturday, :sunday, :monday, :tuesday, :wednesday, :thursday, :friday]
|
193
|
-
].each do |day_names|
|
194
|
-
it "is #{day_names[0]} through #{day_names[6]} when the weekday is set to start on #{day_names[0]}" do
|
195
|
-
@options[:start_week_on] = day_names[0]
|
196
|
-
subject.day_names.should == day_names.map { |dn| Cal::DayName.send dn }
|
197
|
-
end
|
198
|
-
end
|
124
|
+
describe "#day_names" do
|
125
|
+
it "defaults to sunday through saturday" do
|
126
|
+
Cal::MonthlyCalendar.new(2012, 12).day_names.should == [
|
127
|
+
Cal::DayName.sunday,
|
128
|
+
Cal::DayName.monday,
|
129
|
+
Cal::DayName.tuesday,
|
130
|
+
Cal::DayName.wednesday,
|
131
|
+
Cal::DayName.thursday,
|
132
|
+
Cal::DayName.friday,
|
133
|
+
Cal::DayName.saturday,
|
134
|
+
]
|
199
135
|
end
|
200
136
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
137
|
+
[
|
138
|
+
%i[monday tuesday wednesday thursday friday saturday sunday],
|
139
|
+
%i[tuesday wednesday thursday friday saturday sunday monday],
|
140
|
+
%i[wednesday thursday friday saturday sunday monday tuesday],
|
141
|
+
%i[thursday friday saturday sunday monday tuesday wednesday],
|
142
|
+
%i[friday saturday sunday monday tuesday wednesday thursday],
|
143
|
+
%i[saturday sunday monday tuesday wednesday thursday friday],
|
144
|
+
].each do |day_names|
|
145
|
+
it "is #{day_names[0]} through #{day_names[6]} when the weekday is set to start on #{day_names[0]}" do
|
146
|
+
calendar = Cal::MonthlyCalendar.new(2012, 12, :start_week_on => day_names[0])
|
147
|
+
calendar.day_names.should == day_names.map { |dn| Cal::DayName.send dn }
|
205
148
|
end
|
206
149
|
end
|
207
150
|
end
|
208
151
|
|
152
|
+
describe "#to_param" do
|
153
|
+
it "is the year and month string" do
|
154
|
+
Cal::MonthlyCalendar.new(2012, 12).to_param.should == "2012-12"
|
155
|
+
end
|
156
|
+
end
|
209
157
|
end
|
data/spec/cal_spec.rb
CHANGED
@@ -1,17 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'cal'
|
2
3
|
|
3
4
|
describe Cal do
|
4
|
-
|
5
|
-
|
6
|
-
describe "new_monthly_calendar" do
|
7
|
-
it "creates a new monthly calendar with the given args" do
|
8
|
-
@args = [1, 2, 3]
|
5
|
+
describe ".new_monthly_calendar" do
|
6
|
+
it "is delegated to MonthlyCalendar.new" do
|
9
7
|
monthly_calendar = Object.new
|
10
|
-
Cal::MonthlyCalendar.stub
|
11
|
-
|
12
|
-
end
|
13
|
-
subject.new_monthly_calendar(*@args).should == monthly_calendar
|
8
|
+
Cal::MonthlyCalendar.stub(:new).with(1, 2, 3) { monthly_calendar }
|
9
|
+
Cal.new_monthly_calendar(1, 2, 3).should == monthly_calendar
|
14
10
|
end
|
15
11
|
end
|
16
|
-
|
17
12
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,48 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
5
|
-
prerelease:
|
4
|
+
version: 0.6.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Austin Schneider
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2013-05-24 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: activesupport
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '2'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '2'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rspec
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ~>
|
36
32
|
- !ruby/object:Gem::Version
|
37
|
-
version: 2.
|
33
|
+
version: 2.11.0
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ~>
|
44
39
|
- !ruby/object:Gem::Version
|
45
|
-
version: 2.
|
40
|
+
version: 2.11.0
|
46
41
|
description: A low level calendar engine.
|
47
42
|
email:
|
48
43
|
- austinthecoder@gmail.com
|
@@ -71,27 +66,26 @@ files:
|
|
71
66
|
- spec/spec_helper.rb
|
72
67
|
homepage: https://github.com/austinthecoder/cal
|
73
68
|
licenses: []
|
69
|
+
metadata: {}
|
74
70
|
post_install_message:
|
75
71
|
rdoc_options: []
|
76
72
|
require_paths:
|
77
73
|
- lib
|
78
74
|
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
-
none: false
|
80
75
|
requirements:
|
81
|
-
- -
|
76
|
+
- - '>='
|
82
77
|
- !ruby/object:Gem::Version
|
83
78
|
version: '0'
|
84
79
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
-
none: false
|
86
80
|
requirements:
|
87
|
-
- -
|
81
|
+
- - '>='
|
88
82
|
- !ruby/object:Gem::Version
|
89
83
|
version: '0'
|
90
84
|
requirements: []
|
91
85
|
rubyforge_project:
|
92
|
-
rubygems_version:
|
86
|
+
rubygems_version: 2.0.3
|
93
87
|
signing_key:
|
94
|
-
specification_version:
|
88
|
+
specification_version: 4
|
95
89
|
summary: A low level calendar engine.
|
96
90
|
test_files:
|
97
91
|
- spec/cal/day_name_spec.rb
|