calorie 0.0.2 → 0.0.3
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 +3 -3
- data/calorie.gemspec +1 -1
- data/lib/calorie/calendar.rb +4 -56
- data/lib/calorie/day_of_the_week.rb +1 -7
- data/lib/calorie/version.rb +1 -1
- data/lib/calorie/weeks_in_month.rb +53 -1
- data/lib/calorie.rb +16 -0
- data/spec/calendar_spec.rb +0 -44
- data/spec/weeks_in_month_spec.rb +49 -2
- metadata +9 -9
data/README.md
CHANGED
@@ -23,13 +23,13 @@ The data can be anything; only your code will be interacting with it.
|
|
23
23
|
# ... etc
|
24
24
|
}
|
25
25
|
|
26
|
-
Obviously, you don't need to pass in any data -- if it's easy enough to just loop through and check what day it is and do whatever you need to do in your
|
26
|
+
Obviously, you don't need to pass in any data -- if it's easy enough to just loop through and check what day it is and do whatever you need to do in your template, that's up to you.
|
27
27
|
|
28
|
-
Create a
|
28
|
+
Create a decorator for a month by sending in `year`, `month`, and the data:
|
29
29
|
|
30
30
|
cal = Calorie.new(2010, 6, data)
|
31
31
|
|
32
|
-
In your
|
32
|
+
In your template, you can style it however you wish.
|
33
33
|
|
34
34
|
# previous month, e.g. December 2009
|
35
35
|
cal.previous
|
data/calorie.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.email = ["katrina.owen@gmail.com"]
|
10
10
|
s.homepage = ""
|
11
11
|
s.summary = %q{A calendar is a calendar is a calendar.}
|
12
|
-
s.description = %q{A simple, ruby calendar
|
12
|
+
s.description = %q{A simple, ruby calendar decorator.}
|
13
13
|
|
14
14
|
s.rubyforge_project = "calorie"
|
15
15
|
|
data/lib/calorie/calendar.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
module Calorie
|
2
2
|
|
3
3
|
class Calendar
|
4
|
-
include WeeksInMonth
|
5
4
|
|
6
5
|
attr_reader :days, :weeks, :year, :month, :data
|
7
6
|
def initialize(year, month, data = {})
|
@@ -9,7 +8,7 @@ module Calorie
|
|
9
8
|
@year = year
|
10
9
|
@month = month
|
11
10
|
initialize_days
|
12
|
-
|
11
|
+
@weeks = WeeksInMonth.new(days).weeks
|
13
12
|
end
|
14
13
|
|
15
14
|
def first_day
|
@@ -22,18 +21,6 @@ module Calorie
|
|
22
21
|
@last_day
|
23
22
|
end
|
24
23
|
|
25
|
-
def days_in_month
|
26
|
-
days.size
|
27
|
-
end
|
28
|
-
|
29
|
-
def first_day_falls_on
|
30
|
-
if Calorie.configuration.week_starts_on?(:monday)
|
31
|
-
(first_day.wday - 1) % 7
|
32
|
-
else
|
33
|
-
first_day.wday
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
24
|
def initialize_days
|
38
25
|
@days = []
|
39
26
|
(first_day.mday..last_day.mday).map do |i|
|
@@ -42,39 +29,6 @@ module Calorie
|
|
42
29
|
end
|
43
30
|
end
|
44
31
|
|
45
|
-
def initialize_weeks
|
46
|
-
@weeks = []
|
47
|
-
padded_days = days_for_slicing
|
48
|
-
number_of_weeks.times do
|
49
|
-
@weeks << Calorie::Week.new(padded_days.slice!(0..6))
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def days_for_slicing
|
54
|
-
slices = days.clone
|
55
|
-
|
56
|
-
(1..blank_days_at_start).each do |i|
|
57
|
-
slices.unshift(Calorie::NullDay.new(first_day - i))
|
58
|
-
end
|
59
|
-
(1..blank_days_at_end).each do |i|
|
60
|
-
slices.push(Calorie::NullDay.new(last_day + i))
|
61
|
-
end
|
62
|
-
|
63
|
-
slices
|
64
|
-
end
|
65
|
-
|
66
|
-
def blank_days_at_start
|
67
|
-
first_day_falls_on
|
68
|
-
end
|
69
|
-
|
70
|
-
def blank_days_at_end
|
71
|
-
7 - ((blank_days_at_start + days_in_month) % 7)
|
72
|
-
end
|
73
|
-
|
74
|
-
def days_in_month
|
75
|
-
last_day.mday
|
76
|
-
end
|
77
|
-
|
78
32
|
def each_day(&block)
|
79
33
|
days.each {|day| block.call(day) }
|
80
34
|
end
|
@@ -92,21 +46,15 @@ module Calorie
|
|
92
46
|
end
|
93
47
|
|
94
48
|
def previous
|
95
|
-
|
96
|
-
the_month = I18n.translate('calorie.months')[date.month-1]
|
97
|
-
"#{the_month} #{date.year}"
|
49
|
+
Calorie.label_for(first_day.prev_month)
|
98
50
|
end
|
99
51
|
|
100
52
|
def current
|
101
|
-
|
102
|
-
the_month = I18n.translate('calorie.months')[date.month-1]
|
103
|
-
"#{the_month} #{date.year}"
|
53
|
+
Calorie.label_for(first_day)
|
104
54
|
end
|
105
55
|
|
106
56
|
def next
|
107
|
-
|
108
|
-
the_month = I18n.translate('calorie.months')[date.month-1]
|
109
|
-
"#{the_month} #{date.year}"
|
57
|
+
Calorie.label_for(first_day.next_month)
|
110
58
|
end
|
111
59
|
end
|
112
60
|
end
|
@@ -1,17 +1,11 @@
|
|
1
1
|
module Calorie
|
2
2
|
class DayOfTheWeek
|
3
|
-
class << self
|
4
|
-
def day_names
|
5
|
-
I18n.translate('calorie.days_of_the_week')
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
3
|
def initialize(day_of_the_week)
|
10
4
|
@day_of_the_week = day_of_the_week
|
11
5
|
end
|
12
6
|
|
13
7
|
def label
|
14
|
-
|
8
|
+
@label ||= Calorie.day_names[@day_of_the_week]
|
15
9
|
end
|
16
10
|
|
17
11
|
def weekend?
|
data/lib/calorie/version.rb
CHANGED
@@ -1,9 +1,61 @@
|
|
1
1
|
module Calorie
|
2
|
-
|
2
|
+
class WeeksInMonth
|
3
|
+
def initialize(days = [])
|
4
|
+
@source_days = days
|
5
|
+
end
|
6
|
+
|
7
|
+
def weeks
|
8
|
+
result = []
|
9
|
+
padded_days = days_for_slicing
|
10
|
+
number_of_weeks.times do
|
11
|
+
result << Calorie::Week.new(padded_days.slice!(0..6))
|
12
|
+
end
|
13
|
+
result
|
14
|
+
end
|
15
|
+
|
16
|
+
def days_for_slicing
|
17
|
+
slices = @source_days.clone
|
18
|
+
|
19
|
+
(1..blank_days_at_start).each do |i|
|
20
|
+
slices.unshift(Calorie::NullDay.new(first_day - i))
|
21
|
+
end
|
22
|
+
(1..blank_days_at_end).each do |i|
|
23
|
+
slices.push(Calorie::NullDay.new(last_day + i))
|
24
|
+
end
|
25
|
+
|
26
|
+
slices
|
27
|
+
end
|
28
|
+
|
29
|
+
def blank_days_at_start
|
30
|
+
first_day_falls_on
|
31
|
+
end
|
32
|
+
|
33
|
+
def blank_days_at_end
|
34
|
+
7 - ((blank_days_at_start + days_in_month) % 7)
|
35
|
+
end
|
3
36
|
|
4
37
|
def number_of_weeks
|
5
38
|
((days_in_month + first_day_falls_on) / 7.0).ceil
|
6
39
|
end
|
7
40
|
|
41
|
+
def days_in_month
|
42
|
+
@source_days.length
|
43
|
+
end
|
44
|
+
|
45
|
+
def first_day
|
46
|
+
@source_days.first.date
|
47
|
+
end
|
48
|
+
|
49
|
+
def last_day
|
50
|
+
@source_days.last.date
|
51
|
+
end
|
52
|
+
|
53
|
+
def first_day_falls_on
|
54
|
+
if Calorie.configuration.week_starts_on?(:monday)
|
55
|
+
(first_day.wday - 1) % 7
|
56
|
+
else
|
57
|
+
first_day.wday
|
58
|
+
end
|
59
|
+
end
|
8
60
|
end
|
9
61
|
end
|
data/lib/calorie.rb
CHANGED
@@ -44,6 +44,22 @@ module Calorie
|
|
44
44
|
def new(year, month, data = {})
|
45
45
|
Calendar.new(year, month, data)
|
46
46
|
end
|
47
|
+
|
48
|
+
def day_names
|
49
|
+
@day_names ||= I18n.translate('calorie.days_of_the_week')
|
50
|
+
end
|
51
|
+
|
52
|
+
def month_names
|
53
|
+
@month_names ||= I18n.translate('calorie.months')
|
54
|
+
end
|
55
|
+
|
56
|
+
def month_name(i)
|
57
|
+
month_names[i-1]
|
58
|
+
end
|
59
|
+
|
60
|
+
def label_for(date)
|
61
|
+
"#{Calorie.month_name(date.month)} #{date.year}"
|
62
|
+
end
|
47
63
|
end
|
48
64
|
|
49
65
|
end
|
data/spec/calendar_spec.rb
CHANGED
@@ -61,50 +61,6 @@ describe Calorie::Calendar do
|
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
|
-
context "when week starts on sunday" do
|
65
|
-
before(:each) { Calorie.config = nil }
|
66
|
-
|
67
|
-
describe "first day falls on" do
|
68
|
-
{
|
69
|
-
2003 => 2,
|
70
|
-
2004 => 4,
|
71
|
-
2005 => 5,
|
72
|
-
2006 => 6,
|
73
|
-
2007 => 0,
|
74
|
-
2008 => 2
|
75
|
-
}.each do |year, offset|
|
76
|
-
it "#{offset} in #{year}" do
|
77
|
-
cal = Calorie::Calendar.new(year, 4, {})
|
78
|
-
cal.first_day_falls_on.should eq(offset)
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
context "when week starts on monday" do
|
85
|
-
before :each do
|
86
|
-
Calorie.configuration do |config|
|
87
|
-
config.week_starts_on :monday
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
describe "first day falls on" do
|
92
|
-
{
|
93
|
-
2003 => 3,
|
94
|
-
2004 => 5,
|
95
|
-
2005 => 6,
|
96
|
-
2006 => 0,
|
97
|
-
2007 => 1,
|
98
|
-
2008 => 3
|
99
|
-
}.each do |year, offset|
|
100
|
-
it "#{offset} in #{year}" do
|
101
|
-
cal = Calorie::Calendar.new(year, 5, {})
|
102
|
-
cal.first_day_falls_on.should eq(offset)
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
64
|
describe "translation" do
|
109
65
|
around :each do |example|
|
110
66
|
I18n.with_locale(:xx) do
|
data/spec/weeks_in_month_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require 'calorie
|
1
|
+
require 'calorie'
|
2
2
|
|
3
3
|
describe Calorie::WeeksInMonth do
|
4
|
-
let(:cal) {
|
4
|
+
let(:cal) { Calorie::WeeksInMonth.new }
|
5
5
|
|
6
6
|
context "with a 28 day month" do
|
7
7
|
before :each do
|
@@ -92,4 +92,51 @@ describe Calorie::WeeksInMonth do
|
|
92
92
|
cal.number_of_weeks.should eq(6)
|
93
93
|
end
|
94
94
|
end
|
95
|
+
|
96
|
+
context "when week starts on sunday" do
|
97
|
+
before(:each) { Calorie.config = nil }
|
98
|
+
|
99
|
+
describe "first day falls on" do
|
100
|
+
{
|
101
|
+
2003 => 2,
|
102
|
+
2004 => 4,
|
103
|
+
2005 => 5,
|
104
|
+
2006 => 6,
|
105
|
+
2007 => 0,
|
106
|
+
2008 => 2
|
107
|
+
}.each do |year, offset|
|
108
|
+
it "#{offset} in April #{year}" do
|
109
|
+
apr1 = Calorie::Day.new(Date.new(year, 4, 1))
|
110
|
+
cal = Calorie::WeeksInMonth.new [apr1]
|
111
|
+
cal.first_day_falls_on.should eq(offset)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
context "when week starts on monday" do
|
118
|
+
before :each do
|
119
|
+
Calorie.configuration do |config|
|
120
|
+
config.week_starts_on :monday
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe "first day falls on" do
|
125
|
+
{
|
126
|
+
2003 => 3,
|
127
|
+
2004 => 5,
|
128
|
+
2005 => 6,
|
129
|
+
2006 => 0,
|
130
|
+
2007 => 1,
|
131
|
+
2008 => 3
|
132
|
+
}.each do |year, offset|
|
133
|
+
it "#{offset} in May #{year}" do
|
134
|
+
may1 = Calorie::Day.new(Date.new(year, 5, 1))
|
135
|
+
cal = Calorie::WeeksInMonth.new [may1]
|
136
|
+
cal.first_day_falls_on.should eq(offset)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
95
142
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: calorie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-12-
|
12
|
+
date: 2011-12-21 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &2160865760 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2160865760
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: timecop
|
27
|
-
requirement: &
|
27
|
+
requirement: &2160865340 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2160865340
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: i18n
|
38
|
-
requirement: &
|
38
|
+
requirement: &2160864920 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,8 +43,8 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
47
|
-
description: A simple, ruby calendar
|
46
|
+
version_requirements: *2160864920
|
47
|
+
description: A simple, ruby calendar decorator.
|
48
48
|
email:
|
49
49
|
- katrina.owen@gmail.com
|
50
50
|
executables: []
|