cal 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cal/monthly_calendar.rb +5 -3
- data/lib/cal/version.rb +1 -1
- data/spec/cal/monthly_calendar_spec.rb +44 -7
- metadata +1 -1
data/lib/cal/monthly_calendar.rb
CHANGED
@@ -4,8 +4,10 @@ require 'active_support/core_ext/string/conversions'
|
|
4
4
|
module Cal
|
5
5
|
class MonthlyCalendar
|
6
6
|
|
7
|
-
def initialize(dateable)
|
7
|
+
def initialize(dateable, options = {})
|
8
|
+
options = options.reverse_merge :start_week_on => :sunday
|
8
9
|
@date = dateable.to_date
|
10
|
+
@options = options
|
9
11
|
end
|
10
12
|
|
11
13
|
attr_reader :date
|
@@ -21,11 +23,11 @@ module Cal
|
|
21
23
|
end
|
22
24
|
|
23
25
|
def first_day
|
24
|
-
@first_day ||= Day.new date.beginning_of_month.beginning_of_week(:
|
26
|
+
@first_day ||= Day.new date.beginning_of_month.beginning_of_week(@options[:start_week_on]), self
|
25
27
|
end
|
26
28
|
|
27
29
|
def last_day
|
28
|
-
@last_day ||= Day.new date.end_of_month.end_of_week(:
|
30
|
+
@last_day ||= Day.new date.end_of_month.end_of_week(@options[:start_week_on]), self
|
29
31
|
end
|
30
32
|
|
31
33
|
def days
|
data/lib/cal/version.rb
CHANGED
@@ -2,9 +2,12 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Cal::MonthlyCalendar do
|
4
4
|
|
5
|
-
subject { described_class.new @date }
|
5
|
+
subject { described_class.new @date, @options }
|
6
6
|
|
7
|
-
before
|
7
|
+
before do
|
8
|
+
@date = Date.new 2012, 2
|
9
|
+
@options = {}
|
10
|
+
end
|
8
11
|
|
9
12
|
describe "initialize" do
|
10
13
|
it "raises an error if arg can't be converted to a date" do
|
@@ -52,21 +55,55 @@ describe Cal::MonthlyCalendar do
|
|
52
55
|
end
|
53
56
|
|
54
57
|
describe "first_day" do
|
55
|
-
|
56
|
-
|
58
|
+
before { @date = Date.new 2012, 2, 23 }
|
59
|
+
|
60
|
+
[
|
61
|
+
[:monday, [1, 30]],
|
62
|
+
[:tuesday, [1, 31]],
|
63
|
+
[:wednesday, [2, 1]],
|
64
|
+
[:thursday, [1, 26]],
|
65
|
+
[:friday, [1, 27]],
|
66
|
+
[:saturday, [1, 28]]
|
67
|
+
].each do |weekday, month_day|
|
68
|
+
context "when the weekday is set to start on #{weekday}" do
|
69
|
+
before { @options[:start_week_on] = weekday }
|
70
|
+
it "is the first viewable day on the calendar" do
|
71
|
+
subject.first_day.should == Cal::Day.new(Date.new(2012, *month_day), subject)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
it "is the first viewable day on the calendar, using sunday as the default start day" do
|
57
77
|
subject.first_day.should == Cal::Day.new(Date.new(2012, 1, 29), subject)
|
58
78
|
end
|
59
79
|
end
|
60
80
|
|
61
81
|
describe "last_day" do
|
62
|
-
|
63
|
-
|
82
|
+
before { @date = Date.new 2012, 2, 23 }
|
83
|
+
|
84
|
+
[
|
85
|
+
[:monday, [3, 4]],
|
86
|
+
[:tuesday, [3, 5]],
|
87
|
+
[:wednesday, [3, 6]],
|
88
|
+
[:thursday, [2, 29]],
|
89
|
+
[:friday, [3, 1]],
|
90
|
+
[:saturday, [3, 2]]
|
91
|
+
].each do |weekday, month_day|
|
92
|
+
context "when the weekday is set to start on #{weekday}" do
|
93
|
+
before { @options[:start_week_on] = weekday }
|
94
|
+
it "is the last viewable day on the calendar" do
|
95
|
+
subject.last_day.should == Cal::Day.new(Date.new(2012, *month_day), subject)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
it "is the last viewable day on the calendar, using sunday as the default start day" do
|
64
101
|
subject.last_day.should == Cal::Day.new(Date.new(2012, 3, 3), subject)
|
65
102
|
end
|
66
103
|
end
|
67
104
|
|
68
105
|
describe "days" do
|
69
|
-
it "is a range of
|
106
|
+
it "is a range of the first day to the last day" do
|
70
107
|
@date = Date.new 2012, 2, 23
|
71
108
|
first_day = Cal::Day.new Date.new(2012, 1, 29), subject
|
72
109
|
last_day = Cal::Day.new Date.new(2012, 3, 3), subject
|