cal 0.5.1 → 0.6.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/README.md +24 -2
- data/lib/cal.rb +7 -0
- data/lib/cal/day_name.rb +73 -0
- data/lib/cal/monthly_calendar.rb +1 -11
- data/lib/cal/version.rb +1 -1
- data/spec/cal/day_name_spec.rb +134 -0
- data/spec/cal/monthly_calendar_spec.rb +17 -9
- data/spec/cal_spec.rb +17 -0
- metadata +7 -2
data/README.md
CHANGED
@@ -24,6 +24,28 @@ Cal is a simple calendar structure. It **does not** render a calendar. It **does
|
|
24
24
|
|
25
25
|
See the Rails app [https://github.com/austinthecoder/cal-app](https://github.com/austinthecoder/cal-app) for an example.
|
26
26
|
|
27
|
+
### API
|
28
|
+
|
29
|
+
``` ruby
|
30
|
+
calendar = Cal::MonthlyCalendar.new 2012, 11, :start_week_on => :monday
|
31
|
+
|
32
|
+
calendar.month # a Cal::Month
|
33
|
+
|
34
|
+
calendar.first_day # a Cal::Day representing the first day on the calendar, might be in the previous month
|
35
|
+
|
36
|
+
calendar.last_day # a Cal::Day representing the last day on the calendar, might be in the next month
|
37
|
+
|
38
|
+
calendar.days # a Range of Cal::Day's from the first day to the last
|
39
|
+
|
40
|
+
calendar.weeks # an Array of Cal::Day Arrays
|
41
|
+
|
42
|
+
calendar.previous # the calendar for the previous month
|
43
|
+
|
44
|
+
calendar.next # the calendar for the next month
|
45
|
+
|
46
|
+
calendar.day_names # an Array of the day names, e.g. ['Sunday', 'Monday', ...], depends on the `:start_week_on` option
|
47
|
+
```
|
48
|
+
|
27
49
|
### Rails example
|
28
50
|
|
29
51
|
``` ruby
|
@@ -37,7 +59,7 @@ calendar = Cal::MonthlyCalendar.from_param params[:month], :start_week_on => :mo
|
|
37
59
|
%h3
|
38
60
|
= link_to 'Previous month', calendar_path(calendar.previous)
|
39
61
|
|
|
40
|
-
= "#{calendar.month} #{calendar.year}"
|
62
|
+
= "#{calendar.month.to_s "%B"} #{calendar.year}"
|
41
63
|
|
|
42
64
|
= link_to 'Next month', calendar_path(calendar.next)
|
43
65
|
|
@@ -60,4 +82,4 @@ calendar = Cal::MonthlyCalendar.from_param params[:month], :start_week_on => :mo
|
|
60
82
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
61
83
|
3. Commit your changes (`git commit -am 'Added some feature'`)
|
62
84
|
4. Push to the branch (`git push origin my-new-feature`)
|
63
|
-
5. Create new Pull Request
|
85
|
+
5. Create new Pull Request
|
data/lib/cal.rb
CHANGED
@@ -6,5 +6,12 @@ module Cal
|
|
6
6
|
autoload :MonthlyCalendar, 'cal/monthly_calendar'
|
7
7
|
autoload :Day, 'cal/day'
|
8
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
|
15
|
+
end
|
9
16
|
|
10
17
|
end
|
data/lib/cal/day_name.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
module Cal
|
2
|
+
class DayName
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def all(options = {})
|
6
|
+
if options[:start_on]
|
7
|
+
sunday_to_saturday.rotate(send(options[:start_on].to_s.downcase.to_sym).position - 1)
|
8
|
+
else
|
9
|
+
sunday_to_saturday
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def sunday
|
14
|
+
@sunday ||= new :sunday, 'Sunday', 1
|
15
|
+
end
|
16
|
+
|
17
|
+
def monday
|
18
|
+
@monday ||= new :monday, 'Monday', 2
|
19
|
+
end
|
20
|
+
|
21
|
+
def tuesday
|
22
|
+
@tuesday ||= new :tuesday, 'Tuesday', 3
|
23
|
+
end
|
24
|
+
|
25
|
+
def wednesday
|
26
|
+
@wednesday ||= new :wednesday, 'Wednesday', 4
|
27
|
+
end
|
28
|
+
|
29
|
+
def thursday
|
30
|
+
@thursday ||= new :thursday, 'Thursday', 5
|
31
|
+
end
|
32
|
+
|
33
|
+
def friday
|
34
|
+
@friday ||= new :friday, 'Friday', 6
|
35
|
+
end
|
36
|
+
|
37
|
+
def saturday
|
38
|
+
@saturday ||= new :saturday, 'Saturday', 7
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def sunday_to_saturday
|
44
|
+
@sunday_to_saturday ||= [sunday, monday, tuesday, wednesday, thursday, friday, saturday]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private_class_method :new
|
49
|
+
|
50
|
+
def initialize(symbol, string, position)
|
51
|
+
@to_sym = symbol
|
52
|
+
@to_s = string
|
53
|
+
@position = position
|
54
|
+
end
|
55
|
+
|
56
|
+
attr_reader :to_sym, :to_s, :position
|
57
|
+
|
58
|
+
def succ
|
59
|
+
self.class.all.detect do |day_name|
|
60
|
+
day_name.position == (position < 7 ? position + 1 : 1)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
alias_method :next, :succ
|
65
|
+
|
66
|
+
def previous
|
67
|
+
self.class.all.detect do |day_name|
|
68
|
+
day_name.position == (position > 1 ? position - 1 : 7)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
data/lib/cal/monthly_calendar.rb
CHANGED
@@ -6,16 +6,6 @@ module Cal
|
|
6
6
|
|
7
7
|
include Comparable
|
8
8
|
|
9
|
-
DAY_NAMES = {
|
10
|
-
:sunday => 0,
|
11
|
-
:monday => 1,
|
12
|
-
:tuesday => 2,
|
13
|
-
:wednesday => 3,
|
14
|
-
:thursday => 4,
|
15
|
-
:friday => 5,
|
16
|
-
:saturday => 6
|
17
|
-
}
|
18
|
-
|
19
9
|
class << self
|
20
10
|
def from_month(month, options = {})
|
21
11
|
month = month.to_month
|
@@ -79,7 +69,7 @@ module Cal
|
|
79
69
|
end
|
80
70
|
|
81
71
|
def day_names
|
82
|
-
|
72
|
+
@day_names ||= DayName.all :start_on => start_week_on
|
83
73
|
end
|
84
74
|
|
85
75
|
def to_param
|
data/lib/cal/version.rb
CHANGED
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Cal::DayName do
|
4
|
+
|
5
|
+
describe "class method" do
|
6
|
+
subject { described_class }
|
7
|
+
|
8
|
+
describe "all" do
|
9
|
+
it "returns all the days, starting with sunday" do
|
10
|
+
subject.all.should == [
|
11
|
+
subject.sunday,
|
12
|
+
subject.monday,
|
13
|
+
subject.tuesday,
|
14
|
+
subject.wednesday,
|
15
|
+
subject.thursday,
|
16
|
+
subject.friday,
|
17
|
+
subject.saturday
|
18
|
+
]
|
19
|
+
end
|
20
|
+
|
21
|
+
it "when given a day name symbol as the :start_on option, it starts the week on that day" do
|
22
|
+
subject.all(:start_on => :tuesday).should == [
|
23
|
+
subject.tuesday,
|
24
|
+
subject.wednesday,
|
25
|
+
subject.thursday,
|
26
|
+
subject.friday,
|
27
|
+
subject.saturday,
|
28
|
+
subject.sunday,
|
29
|
+
subject.monday
|
30
|
+
]
|
31
|
+
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
|
+
end
|
51
|
+
|
52
|
+
it "doesn't allow direct initialization" do
|
53
|
+
expect { subject.new }.to raise_error NoMethodError
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "instance method" do
|
58
|
+
[
|
59
|
+
[:sunday, 'Sunday', 1],
|
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 }
|
69
|
+
|
70
|
+
it "is the same instance as another #{day}" do
|
71
|
+
subject.equal?(described_class.send(day)).should be_true
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "to_sym" do
|
75
|
+
it "is #{day.inspect}" do
|
76
|
+
subject.to_sym.should == day
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "to_s" do
|
81
|
+
it "is #{string.inspect}" do
|
82
|
+
subject.to_s.should == string
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "position" do
|
87
|
+
it "is #{position.inspect}" do
|
88
|
+
subject.position.should == position
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "succ" do
|
95
|
+
[
|
96
|
+
[:sunday, :monday],
|
97
|
+
[:monday, :tuesday],
|
98
|
+
[:tuesday, :wednesday],
|
99
|
+
[:wednesday, :thursday],
|
100
|
+
[:thursday, :friday],
|
101
|
+
[:friday, :saturday],
|
102
|
+
[:saturday, :sunday]
|
103
|
+
].each do |day, next_day|
|
104
|
+
it "is #{next_day} when the say is #{day}" do
|
105
|
+
described_class.send(day).succ.should == described_class.send(next_day)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe "next" do
|
111
|
+
it "is an alias of #succ" do
|
112
|
+
day_name = described_class.friday
|
113
|
+
day_name.next.should == day_name.succ
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
describe "previous" do
|
118
|
+
[
|
119
|
+
[:sunday, :saturday],
|
120
|
+
[:monday, :sunday],
|
121
|
+
[:tuesday, :monday],
|
122
|
+
[:wednesday, :tuesday],
|
123
|
+
[:thursday, :wednesday],
|
124
|
+
[:friday, :thursday],
|
125
|
+
[:saturday, :friday]
|
126
|
+
].each do |day, previous_day|
|
127
|
+
it "is #{previous_day} when the say is #{day}" do
|
128
|
+
described_class.send(day).previous.should == described_class.send(previous_day)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
@@ -172,20 +172,28 @@ describe Cal::MonthlyCalendar do
|
|
172
172
|
|
173
173
|
describe "day_names" do
|
174
174
|
it "defaults to sunday through saturday" do
|
175
|
-
subject.day_names.should ==
|
175
|
+
subject.day_names.should == [
|
176
|
+
Cal::DayName.sunday,
|
177
|
+
Cal::DayName.monday,
|
178
|
+
Cal::DayName.tuesday,
|
179
|
+
Cal::DayName.wednesday,
|
180
|
+
Cal::DayName.thursday,
|
181
|
+
Cal::DayName.friday,
|
182
|
+
Cal::DayName.saturday
|
183
|
+
]
|
176
184
|
end
|
177
185
|
|
178
186
|
[
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
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]
|
185
193
|
].each do |day_names|
|
186
194
|
it "is #{day_names[0]} through #{day_names[6]} when the weekday is set to start on #{day_names[0]}" do
|
187
|
-
@options[:start_week_on] = day_names[0]
|
188
|
-
subject.day_names.should == day_names
|
195
|
+
@options[:start_week_on] = day_names[0]
|
196
|
+
subject.day_names.should == day_names.map { |dn| Cal::DayName.send dn }
|
189
197
|
end
|
190
198
|
end
|
191
199
|
end
|
data/spec/cal_spec.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Cal do
|
4
|
+
subject { described_class }
|
5
|
+
|
6
|
+
describe "new_monthly_calendar" do
|
7
|
+
it "creates a new monthly calendar with the given args" do
|
8
|
+
@args = [1, 2, 3]
|
9
|
+
monthly_calendar = Object.new
|
10
|
+
Cal::MonthlyCalendar.stub :new do |*args|
|
11
|
+
monthly_calendar if args == @args
|
12
|
+
end
|
13
|
+
subject.new_monthly_calendar(*@args).should == monthly_calendar
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -59,12 +59,15 @@ files:
|
|
59
59
|
- cal.gemspec
|
60
60
|
- lib/cal.rb
|
61
61
|
- lib/cal/day.rb
|
62
|
+
- lib/cal/day_name.rb
|
62
63
|
- lib/cal/month.rb
|
63
64
|
- lib/cal/monthly_calendar.rb
|
64
65
|
- lib/cal/version.rb
|
66
|
+
- spec/cal/day_name_spec.rb
|
65
67
|
- spec/cal/day_spec.rb
|
66
68
|
- spec/cal/month_spec.rb
|
67
69
|
- spec/cal/monthly_calendar_spec.rb
|
70
|
+
- spec/cal_spec.rb
|
68
71
|
- spec/spec_helper.rb
|
69
72
|
homepage: https://github.com/austinthecoder/cal
|
70
73
|
licenses: []
|
@@ -91,7 +94,9 @@ signing_key:
|
|
91
94
|
specification_version: 3
|
92
95
|
summary: A low level calendar engine.
|
93
96
|
test_files:
|
97
|
+
- spec/cal/day_name_spec.rb
|
94
98
|
- spec/cal/day_spec.rb
|
95
99
|
- spec/cal/month_spec.rb
|
96
100
|
- spec/cal/monthly_calendar_spec.rb
|
101
|
+
- spec/cal_spec.rb
|
97
102
|
- spec/spec_helper.rb
|