cal 0.0.1 → 0.1.0
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/.travis.yml +7 -0
- data/README.md +33 -1
- data/Rakefile +4 -0
- data/lib/cal.rb +1 -1
- data/lib/cal/day.rb +10 -2
- data/lib/cal/ender.rb +30 -13
- data/lib/cal/month.rb +5 -3
- data/lib/cal/version.rb +1 -1
- data/spec/cal/day_spec.rb +32 -4
- data/spec/cal/ender_spec.rb +69 -23
- data/spec/cal/month_spec.rb +23 -11
- metadata +3 -2
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
A low level calendar engine.
|
4
4
|
|
5
|
+
[](http://travis-ci.org/austinthecoder/poser)
|
6
|
+
|
5
7
|
## Installation
|
6
8
|
|
7
9
|
Add this line to your application's Gemfile:
|
@@ -18,7 +20,37 @@ Or install it yourself as:
|
|
18
20
|
|
19
21
|
## Usage
|
20
22
|
|
21
|
-
|
23
|
+
### Rails example
|
24
|
+
|
25
|
+
``` ruby
|
26
|
+
# in the controller
|
27
|
+
- calendar = Cal::Ender.new params[:date]
|
28
|
+
|
29
|
+
# in the view
|
30
|
+
%h3
|
31
|
+
= link_to 'Previous month', url_for(:date => calendar.previous.date)
|
32
|
+
|
|
33
|
+
= calendar.month
|
34
|
+
|
|
35
|
+
= link_to 'Next month', url_for(:date => calendar.next.date)
|
36
|
+
|
37
|
+
%table
|
38
|
+
%thead
|
39
|
+
%tr
|
40
|
+
%th Sunday
|
41
|
+
%th Monday
|
42
|
+
%th Tuesday
|
43
|
+
%th Wednesday
|
44
|
+
%th Thursday
|
45
|
+
%th Friday
|
46
|
+
%th Saturday
|
47
|
+
%tbody
|
48
|
+
- calendar.weeks.each do |week|
|
49
|
+
%tr
|
50
|
+
- week.days.each do |day|
|
51
|
+
%td{:class => ('today' if day.today?)}
|
52
|
+
= day.number
|
53
|
+
```
|
22
54
|
|
23
55
|
## Contributing
|
24
56
|
|
data/Rakefile
CHANGED
data/lib/cal.rb
CHANGED
data/lib/cal/day.rb
CHANGED
@@ -1,16 +1,24 @@
|
|
1
|
+
require 'active_support/core_ext/module/delegation'
|
2
|
+
|
1
3
|
module Cal
|
2
4
|
class Day
|
3
5
|
|
4
|
-
def initialize(
|
5
|
-
@calendar = calendar
|
6
|
+
def initialize(date, calendar)
|
6
7
|
@date = date
|
8
|
+
@calendar = calendar
|
7
9
|
end
|
8
10
|
|
9
11
|
attr_reader :calendar, :date
|
10
12
|
|
13
|
+
delegate :today?, :to => :date
|
14
|
+
|
11
15
|
def ==(other)
|
12
16
|
other.is_a?(Day) && other.calendar == calendar && other.date == date
|
13
17
|
end
|
14
18
|
|
19
|
+
def number
|
20
|
+
date.day
|
21
|
+
end
|
22
|
+
|
15
23
|
end
|
16
24
|
end
|
data/lib/cal/ender.rb
CHANGED
@@ -1,44 +1,61 @@
|
|
1
1
|
require 'active_support/time'
|
2
2
|
require 'active_support/core_ext/array/grouping'
|
3
|
+
require 'active_support/core_ext/string/conversions'
|
3
4
|
|
4
5
|
module Cal
|
5
6
|
class Ender
|
6
7
|
|
7
|
-
def initialize(
|
8
|
-
|
8
|
+
def initialize(dateable, options = {})
|
9
|
+
options = {:format => :monthly}.merge options
|
10
|
+
|
11
|
+
if options[:format] != :monthly
|
12
|
+
raise ArgumentError, "only supported format currently is :monthly"
|
13
|
+
end
|
14
|
+
|
15
|
+
@date = dateable.to_date
|
16
|
+
@format = options[:format]
|
9
17
|
end
|
10
18
|
|
11
|
-
attr_reader :date
|
19
|
+
attr_reader :format, :date
|
12
20
|
|
13
21
|
def ==(other)
|
14
22
|
other.is_a?(Ender) && other.date == date
|
15
23
|
end
|
16
24
|
|
17
25
|
def month
|
18
|
-
@
|
26
|
+
@month ||= Month.new self
|
27
|
+
end
|
28
|
+
|
29
|
+
def days
|
30
|
+
@days ||= dates.map { |date| Day.new self, date }
|
19
31
|
end
|
20
32
|
|
21
33
|
def weeks
|
22
34
|
@weeks ||= days.in_groups_of 7
|
23
35
|
end
|
24
36
|
|
25
|
-
def
|
26
|
-
|
37
|
+
def previous
|
38
|
+
self.class.new date.prev_month
|
39
|
+
end
|
40
|
+
|
41
|
+
def next
|
42
|
+
self.class.new date.next_month
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
# TODO: simplify/improve this
|
48
|
+
def dates
|
49
|
+
[].tap do |dates|
|
27
50
|
day = date.beginning_of_month.beginning_of_week :sunday
|
28
51
|
last_day = date.end_of_month.end_of_week :sunday
|
29
52
|
|
30
|
-
days = []
|
31
53
|
while day <= last_day
|
32
|
-
|
54
|
+
dates << day
|
33
55
|
day = day.tomorrow
|
34
56
|
end
|
35
|
-
days
|
36
57
|
end
|
37
58
|
end
|
38
59
|
|
39
|
-
def week_headings
|
40
|
-
@week_headings ||= %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday]
|
41
|
-
end
|
42
|
-
|
43
60
|
end
|
44
61
|
end
|
data/lib/cal/month.rb
CHANGED
@@ -7,11 +7,13 @@ module Cal
|
|
7
7
|
|
8
8
|
attr_reader :calendar
|
9
9
|
|
10
|
-
def
|
11
|
-
|
10
|
+
def ==(other)
|
11
|
+
other.is_a?(Month) && other.calendar == calendar
|
12
12
|
end
|
13
13
|
|
14
|
-
|
14
|
+
def to_s
|
15
|
+
calendar.date.strftime "%B"
|
16
|
+
end
|
15
17
|
|
16
18
|
end
|
17
19
|
end
|
data/lib/cal/version.rb
CHANGED
data/spec/cal/day_spec.rb
CHANGED
@@ -1,16 +1,44 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Cal::Day do
|
4
|
-
subject { described_class.new @
|
4
|
+
subject { described_class.new @date, @calendar }
|
5
5
|
|
6
6
|
before do
|
7
|
-
@
|
8
|
-
@
|
7
|
+
@date = Date.new 2012, 1, 15
|
8
|
+
@calendar = OpenStruct.new :current_day => Date.new(2012, 1, 5)
|
9
9
|
end
|
10
10
|
|
11
11
|
describe "==" do
|
12
12
|
it "is true with another day with the same calendar and date" do
|
13
|
-
subject.should == described_class.new(@
|
13
|
+
subject.should == described_class.new(@date, @calendar)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "is false with another day with a different calendar" do
|
17
|
+
subject.should_not == described_class.new(@date, Object.new)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "is false with another day with a different date" do
|
21
|
+
subject.should_not == described_class.new(Date.new(2012, 1, 14), @calendar)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "is false with a non Cal::Day" do
|
25
|
+
subject.should_not == Object.new
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "today?" do
|
30
|
+
it "is true when the date is today" do
|
31
|
+
@date = Date.current
|
32
|
+
subject.should be_today
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "number" do
|
37
|
+
[3, 15].each do |n|
|
38
|
+
it "is the day of the month" do
|
39
|
+
@date = Date.new 2012, 1, n
|
40
|
+
subject.number.should == n
|
41
|
+
end
|
14
42
|
end
|
15
43
|
end
|
16
44
|
|
data/spec/cal/ender_spec.rb
CHANGED
@@ -1,36 +1,88 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Cal::Ender do
|
4
|
-
subject { described_class.new @date }
|
4
|
+
subject { described_class.new @date, @options }
|
5
5
|
|
6
6
|
before do
|
7
|
-
@date = Date.
|
7
|
+
@date = Date.new 2012, 2, 1
|
8
|
+
@options = {}
|
8
9
|
end
|
9
10
|
|
10
|
-
describe "
|
11
|
-
it "
|
12
|
-
|
11
|
+
describe "initialize" do
|
12
|
+
it "raises argument error if given format isn't :monthly" do
|
13
|
+
@options[:format] = :weekly
|
14
|
+
expect { subject }.to raise_error(ArgumentError)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "raises an error if the first arg can't be converted to a date" do
|
18
|
+
['Octember', Object.new, '2012'].each do |obj|
|
19
|
+
expect { described_class.new obj }.to raise_error
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "doesn't raise an error if the first arg can be converted to a date" do
|
24
|
+
[Date.current, Date.new(2012, 1, 15), '2012-02-24'].each do |obj|
|
25
|
+
expect { described_class.new obj }.to_not raise_error
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "format" do
|
31
|
+
it "defaults to monthly" do
|
32
|
+
subject.format.should == :monthly
|
33
|
+
end
|
34
|
+
|
35
|
+
it "otherwise is the given format" do
|
36
|
+
@options[:format] = :monthly
|
37
|
+
subject.format.should == :monthly
|
13
38
|
end
|
14
39
|
end
|
15
40
|
|
16
41
|
describe "date" do
|
17
|
-
it "is the date
|
42
|
+
it "is the given date" do
|
18
43
|
subject.date.should == @date
|
19
44
|
end
|
20
45
|
end
|
21
46
|
|
22
|
-
describe "
|
23
|
-
it "is
|
24
|
-
|
47
|
+
describe "==" do
|
48
|
+
it "is true with another calendar with the same format and date" do
|
49
|
+
@date = Date.new 2012, 2, 1
|
50
|
+
@options[:format] = :monthly
|
51
|
+
subject.should == described_class.new(@date, @options)
|
25
52
|
end
|
26
53
|
|
27
|
-
it "
|
28
|
-
|
54
|
+
it "is false with another calendar with a different date" do
|
55
|
+
@date = Date.new 2012, 2, 1
|
56
|
+
@options[:format] = :monthly
|
57
|
+
subject.should_not == described_class.new(Date.new(2012, 2, 2), @options)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "is false with a non-Ender object" do
|
61
|
+
subject.should_not == Object.new
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "month" do
|
66
|
+
it { subject.month.should == Cal::Month.new(subject) }
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "days" do
|
70
|
+
it "is an array of all the viewable days" do
|
71
|
+
@date = Date.new 2012, 2, 23
|
72
|
+
|
73
|
+
prev_month_days = %w[01-29 01-30 01-31]
|
74
|
+
days = %w[02-01 02-02 02-03 02-04 02-05 02-06 02-07 02-08 02-09 02-10 02-11 02-12 02-13 02-14 02-15 02-16 02-17 02-18 02-19 02-20 02-21 02-22 02-23 02-24 02-25 02-26 02-27 02-28 02-29]
|
75
|
+
next_month_days = %w[03-01 03-02 03-03]
|
76
|
+
|
77
|
+
subject.days.should == (prev_month_days + days + next_month_days).map do |s|
|
78
|
+
Cal::Day.new subject, Date.parse("2012-#{s}")
|
79
|
+
end
|
29
80
|
end
|
30
81
|
end
|
31
82
|
|
32
83
|
describe "weeks" do
|
33
84
|
it "is the days in groups of 7" do
|
85
|
+
@date = Date.parse "2012-02-23"
|
34
86
|
subject.weeks.should == [
|
35
87
|
%w[01-29 01-30 01-31 02-01 02-02 02-03 02-04].map { |s| Cal::Day.new subject, Date.parse("2012-#{s}") },
|
36
88
|
%w[02-05 02-06 02-07 02-08 02-09 02-10 02-11].map { |s| Cal::Day.new subject, Date.parse("2012-#{s}") },
|
@@ -41,21 +93,15 @@ describe Cal::Ender do
|
|
41
93
|
end
|
42
94
|
end
|
43
95
|
|
44
|
-
describe "
|
45
|
-
it "
|
46
|
-
|
47
|
-
feb = %w[02-01 02-02 02-03 02-04 02-05 02-06 02-07 02-08 02-09 02-10 02-11 02-12 02-13 02-14 02-15 02-16 02-17 02-18 02-19 02-20 02-21 02-22 02-23 02-24 02-25 02-26 02-27 02-28 02-29]
|
48
|
-
mar = %w[03-01 03-02 03-03]
|
49
|
-
|
50
|
-
subject.days.should == (jan + feb + mar).map do |s|
|
51
|
-
Cal::Day.new subject, Date.parse("2012-#{s}")
|
52
|
-
end
|
96
|
+
describe "previous" do
|
97
|
+
it "returns the calendar for the previous month" do
|
98
|
+
subject.previous.should == described_class.new(@date.prev_month)
|
53
99
|
end
|
54
100
|
end
|
55
101
|
|
56
|
-
describe "
|
57
|
-
it "returns the
|
58
|
-
subject.
|
102
|
+
describe "next" do
|
103
|
+
it "returns the calendar for the next month" do
|
104
|
+
subject.next.should == described_class.new(@date.next_month)
|
59
105
|
end
|
60
106
|
end
|
61
107
|
|
data/spec/cal/month_spec.rb
CHANGED
@@ -2,22 +2,34 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Cal::Month do
|
4
4
|
|
5
|
-
subject {
|
5
|
+
subject { described_class.new @calendar }
|
6
6
|
|
7
7
|
before do
|
8
|
-
@calendar = OpenStruct.new
|
8
|
+
@calendar = OpenStruct.new :date => Date.new(2012, 3, 13)
|
9
9
|
end
|
10
10
|
|
11
|
-
describe "
|
12
|
-
it "is
|
13
|
-
|
14
|
-
[Date.parse('2012-01-21'), 'January'],
|
15
|
-
[Date.parse('2012-02-21'), 'February']
|
16
|
-
].each do |date, name|
|
17
|
-
@calendar.date = date
|
18
|
-
subject.name.should == name
|
19
|
-
end
|
11
|
+
describe "==" do
|
12
|
+
it "is true with a Cal::Month with the same calendar" do
|
13
|
+
subject.should == described_class.new(@calendar)
|
20
14
|
end
|
15
|
+
|
16
|
+
it "is false with a Cal::Month with a different calendar" do
|
17
|
+
subject.should_not == described_class.new(Object.new)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "is false with a non-Cal::Month" do
|
21
|
+
subject.should_not == Object.new
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "calendar" do
|
26
|
+
it "is the given calendar" do
|
27
|
+
subject.calendar.should == @calendar
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "to_s" do
|
32
|
+
it { subject.to_s.should == 'March' }
|
21
33
|
end
|
22
34
|
|
23
35
|
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.0
|
4
|
+
version: 0.1.0
|
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-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -51,6 +51,7 @@ extensions: []
|
|
51
51
|
extra_rdoc_files: []
|
52
52
|
files:
|
53
53
|
- .gitignore
|
54
|
+
- .travis.yml
|
54
55
|
- Gemfile
|
55
56
|
- LICENSE
|
56
57
|
- README.md
|