norwegian_holidays 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -23,3 +23,13 @@ Christmas: December 24th (Norwegians only care about Christmas Eve, apparently).
23
23
 
24
24
  NorwegianHolidays.next_mothersday( Date.new(2011, 3, 14) )
25
25
  => February 12, 2012
26
+
27
+
28
+ ## Next (if coming up) or last holiday
29
+
30
+ There is a need to return _the next holiday_ if it is coming up within a certain timeframe, otherwise return _last year's holiday_.
31
+
32
+ The name for this has not been suitably determined as of yet.
33
+
34
+ fathersday_for_season(2.months)
35
+
@@ -33,19 +33,32 @@ module NorwegianHolidays
33
33
 
34
34
  def method_missing(method, *args, &block)
35
35
  if method.to_s =~ /^next_(.*)$/
36
+ next_holiday($1, args.first)
37
+ elsif method.to_s =~ /(.*)_for_season/
38
+ holiday_for_season($1, *args)
39
+ else
40
+ super
41
+ end
42
+ end
36
43
 
37
- given_date = args.first
38
- holiday = $1
44
+ private
39
45
 
40
- the_day = self.send(holiday.to_sym, given_date.year)
46
+ def next_holiday(holiday, given_date)
47
+ the_day = self.send(holiday.to_sym, given_date.year)
41
48
 
42
- if the_day < given_date
43
- the_day = self.send(holiday.to_sym, given_date.year + 1)
44
- end
49
+ if the_day < given_date
50
+ the_day = self.send(holiday.to_sym, given_date.year + 1)
51
+ end
52
+
53
+ the_day
54
+ end
45
55
 
46
- the_day
56
+ def holiday_for_season(holiday, date, timeframe)
57
+ upcoming = next_holiday(holiday, date)
58
+ if (date + timeframe) >= upcoming
59
+ upcoming
47
60
  else
48
- super
61
+ send(holiday, upcoming.year - 1)
49
62
  end
50
63
  end
51
64
 
@@ -1,3 +1,3 @@
1
1
  module NorwegianHolidays
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -16,134 +16,211 @@ describe NorwegianHolidays do
16
16
  end
17
17
  end
18
18
 
19
- describe "#christmas(year)" do
19
+ describe "Christmas" do
20
+ let(:christmas) { Date.new(2011, 12, 24) }
21
+
20
22
  it "is on Christmas Eve" do
21
- NorwegianHolidays.christmas(2011).should eq(Date.new(2011, 12, 24))
23
+ NorwegianHolidays.christmas(2011).should eq(christmas)
22
24
  end
23
- end
24
25
 
25
- describe "#valentines_day(year)" do
26
- it "is on Feb 14th" do
27
- NorwegianHolidays.valentines_day(2011).should eq(Date.new(2011, 2, 14))
28
- end
29
- end
26
+ describe "#next_christmas" do
30
27
 
31
- describe "#mothersday(year)" do
32
- it "is on the second Sunday of February" do
33
- NorwegianHolidays.mothersday(2010).should eq(Date.new(2010, 2, 14))
34
- end
35
- end
28
+ it "is on Dec 24th" do
29
+ NorwegianHolidays.next_christmas(christmas - 1.day).should eq(christmas)
30
+ end
36
31
 
37
- describe "#fathersday(year)" do
38
- it "is on the second Sunday of November" do
39
- NorwegianHolidays.fathersday(2010).should eq(Date.new(2010, 11, 14))
32
+ it "is this year if today is Christmas Eve" do
33
+ NorwegianHolidays.next_christmas(christmas).should eq(christmas)
34
+ end
35
+
36
+ it "is next year if it's already passed" do
37
+ NorwegianHolidays.next_christmas(christmas + 1.day).should eq(christmas + 1.year)
38
+ end
40
39
  end
41
- end
42
40
 
43
- describe "#next_christmas" do
44
- let(:christmas) { Date.new(2011, 12, 24) }
41
+ describe "#christmas_for_season(date, timespan)" do
42
+ let(:timeframe) { 2.months }
43
+ let(:cutoff) { christmas - 2.months }
45
44
 
46
- it "is on Dec 24th" do
47
- NorwegianHolidays.next_christmas(christmas - 1.day).should eq(christmas)
48
- end
45
+ it "is this year if it's coming up within the timeframe" do
46
+ NorwegianHolidays.christmas_for_season(cutoff + 1.day, timeframe).should eq(christmas)
47
+ end
49
48
 
50
- it "is this year if today is Christmas Eve" do
51
- NorwegianHolidays.next_christmas(christmas).should eq(christmas)
52
- end
49
+ it "is this year if it's coming up in exactly the timeframe" do
50
+ NorwegianHolidays.christmas_for_season(cutoff, timeframe).should eq(christmas)
51
+ end
53
52
 
54
- it "is next year if it's already passed" do
55
- NorwegianHolidays.next_christmas(christmas + 1.day).should eq(christmas + 1.year)
53
+ it "is last year if it's too long til the next one" do
54
+ NorwegianHolidays.christmas_for_season(cutoff - 1.day, timeframe).should eq(christmas - 1.year)
55
+ end
56
56
  end
57
57
  end
58
58
 
59
- describe "#next_valentines_day" do
59
+ describe "Valentines Day" do
60
60
  let(:valentines_day) { Date.new(2011, 2, 14) }
61
61
 
62
62
  it "is on Feb 14th" do
63
- NorwegianHolidays.next_valentines_day(valentines_day - 1.day).should eq(valentines_day)
63
+ NorwegianHolidays.valentines_day(2011).should eq(valentines_day)
64
64
  end
65
65
 
66
- it "is this year if today is valentines day" do
67
- NorwegianHolidays.next_valentines_day(valentines_day).should eq(valentines_day)
66
+ describe "#next_valentines_day" do
67
+
68
+ it "is on Feb 14th" do
69
+ NorwegianHolidays.next_valentines_day(valentines_day - 1.day).should eq(valentines_day)
70
+ end
71
+
72
+ it "is this year if today is valentines day" do
73
+ NorwegianHolidays.next_valentines_day(valentines_day).should eq(valentines_day)
74
+ end
75
+
76
+ it "is next year if it's already passed" do
77
+ NorwegianHolidays.next_valentines_day(valentines_day + 1.day).should eq(valentines_day + 1.year)
78
+ end
68
79
  end
69
80
 
70
- it "is next year if it's already passed" do
71
- NorwegianHolidays.next_valentines_day(valentines_day + 1.day).should eq(valentines_day + 1.year)
81
+ describe "#valentines_day_for_season(date, timespan)" do
82
+ let(:timeframe) { 2.months }
83
+ let(:cutoff) { valentines_day - 2.months }
84
+
85
+ it "is this year if it's coming up within the timeframe" do
86
+ NorwegianHolidays.valentines_day_for_season(cutoff + 1.day, timeframe).should eq(valentines_day)
87
+ end
88
+
89
+ it "is this year if it's coming up in exactly the timeframe" do
90
+ NorwegianHolidays.valentines_day_for_season(cutoff, timeframe).should eq(valentines_day)
91
+ end
92
+
93
+ it "is last year if it's too long til the next one" do
94
+ NorwegianHolidays.valentines_day_for_season(cutoff - 1.day, timeframe).should eq(valentines_day - 1.year)
95
+ end
72
96
  end
97
+
73
98
  end
74
99
 
75
- describe "#next_mothersday" do
100
+ describe "Mothersday" do
76
101
  let(:mothersday_2010) { Date.new(2010, 2, 14) }
77
102
  let(:mothersday_2011) { Date.new(2011, 2, 13) }
78
103
  let(:mothersday_2012) { Date.new(2012, 2, 12) }
79
104
 
80
- context "(sanity check) correct in" do
81
- specify "2010" do
82
- NorwegianHolidays.next_mothersday( Date.new(2010, 1, 1) ).should eq(mothersday_2010)
105
+ it "is on the second Sunday of February" do
106
+ NorwegianHolidays.mothersday(2010).should eq(mothersday_2010)
107
+ end
108
+
109
+ describe "#next_mothersday" do
110
+
111
+ context "(sanity check) correct in" do
112
+ specify "2010" do
113
+ NorwegianHolidays.next_mothersday( Date.new(2010, 1, 1) ).should eq(mothersday_2010)
114
+ end
115
+
116
+ specify "2011" do
117
+ NorwegianHolidays.next_mothersday( Date.new(2011, 1, 1) ).should eq(mothersday_2011)
118
+ end
119
+
120
+ specify "2012" do
121
+ NorwegianHolidays.next_mothersday( Date.new(2012, 1, 1) ).should eq(mothersday_2012)
122
+ end
83
123
  end
84
124
 
85
- specify "2011" do
86
- NorwegianHolidays.next_mothersday( Date.new(2011, 1, 1) ).should eq(mothersday_2011)
125
+ context "on mothersday" do
126
+ it "is this year" do
127
+ NorwegianHolidays.next_mothersday(mothersday_2010).should eq(mothersday_2010)
128
+ end
87
129
  end
88
130
 
89
- specify "2012" do
90
- NorwegianHolidays.next_mothersday( Date.new(2012, 1, 1) ).should eq(mothersday_2012)
131
+ context "before mothersday" do
132
+ it "is this year" do
133
+ NorwegianHolidays.next_mothersday(mothersday_2010 - 1.day).should eq(mothersday_2010)
134
+ end
91
135
  end
92
- end
93
136
 
94
- context "on mothersday" do
95
- it "is this year" do
96
- NorwegianHolidays.next_mothersday(mothersday_2010).should eq(mothersday_2010)
137
+ context "after mothersday" do
138
+ it "is next year" do
139
+ NorwegianHolidays.next_mothersday(mothersday_2010 + 1.day).should eq(mothersday_2011)
140
+ end
97
141
  end
98
142
  end
99
143
 
100
- context "before mothersday" do
101
- it "is this year" do
102
- NorwegianHolidays.next_mothersday(mothersday_2010 - 1.day).should eq(mothersday_2010)
144
+ describe "#mothersday_for_season(date, timespan)" do
145
+ let(:mothersday) { mothersday_2011 }
146
+ let(:last_mothersday) { mothersday_2010 }
147
+
148
+ let(:cutoff) { mothersday - 2.months }
149
+ let(:timeframe) { 2.months }
150
+
151
+ it "is this year if it's coming up within the timeframe" do
152
+ NorwegianHolidays.mothersday_for_season(cutoff + 1.day, timeframe).should eq(mothersday)
153
+ end
154
+
155
+ it "is this year if it's coming up in exactly the timeframe" do
156
+ NorwegianHolidays.mothersday_for_season(cutoff, timeframe).should eq(mothersday)
103
157
  end
104
- end
105
158
 
106
- context "after mothersday" do
107
- it "is next year" do
108
- NorwegianHolidays.next_mothersday(mothersday_2010 + 1.day).should eq(mothersday_2011)
159
+ it "is last year if it's too long til the next one" do
160
+ NorwegianHolidays.mothersday_for_season(cutoff - 1.day, timeframe).should eq(last_mothersday)
109
161
  end
110
162
  end
111
163
  end
112
164
 
113
- describe "#next_fathersday" do
165
+ describe "Fathersday" do
114
166
  let(:fathersday_2010) { Date.new(2010, 11, 14) }
115
167
  let(:fathersday_2011) { Date.new(2011, 11, 13) }
116
168
  let(:fathersday_2012) { Date.new(2012, 11, 11) }
117
169
 
118
- context "(sanity check) correct in" do
119
- specify "2010" do
120
- NorwegianHolidays.next_fathersday( Date.new(2010, 1, 1) ).should eq(fathersday_2010)
170
+ it "is on the second Sunday of November" do
171
+ NorwegianHolidays.fathersday(2011).should eq(fathersday_2011)
172
+ end
173
+
174
+ describe "#next_fathersday" do
175
+ context "(sanity check) correct in" do
176
+ specify "2010" do
177
+ NorwegianHolidays.next_fathersday( Date.new(2010, 1, 1) ).should eq(fathersday_2010)
178
+ end
179
+
180
+ specify "2011" do
181
+ NorwegianHolidays.next_fathersday( Date.new(2011, 1, 1) ).should eq(fathersday_2011)
182
+ end
183
+
184
+ specify "2012" do
185
+ NorwegianHolidays.next_fathersday( Date.new(2012, 1, 1) ).should eq(fathersday_2012)
186
+ end
121
187
  end
122
188
 
123
- specify "2011" do
124
- NorwegianHolidays.next_fathersday( Date.new(2011, 1, 1) ).should eq(fathersday_2011)
189
+ context "on fathersday" do
190
+ it "is this year" do
191
+ NorwegianHolidays.next_fathersday(fathersday_2010).should eq(fathersday_2010)
192
+ end
125
193
  end
126
194
 
127
- specify "2012" do
128
- NorwegianHolidays.next_fathersday( Date.new(2012, 1, 1) ).should eq(fathersday_2012)
195
+ context "before fathersday" do
196
+ it "is this year" do
197
+ NorwegianHolidays.next_fathersday(fathersday_2010 - 1.day).should eq(fathersday_2010)
198
+ end
129
199
  end
130
- end
131
200
 
132
- context "on fathersday" do
133
- it "is this year" do
134
- NorwegianHolidays.next_fathersday(fathersday_2010).should eq(fathersday_2010)
201
+ context "after fathersday" do
202
+ it "is next year" do
203
+ NorwegianHolidays.next_fathersday(fathersday_2010 + 1.day).should eq(fathersday_2011)
204
+ end
135
205
  end
136
206
  end
137
207
 
138
- context "before fathersday" do
139
- it "is this year" do
140
- NorwegianHolidays.next_fathersday(fathersday_2010 - 1.day).should eq(fathersday_2010)
208
+ describe "#fathersday_for_season(date, timespan)" do
209
+ let(:fathersday) { fathersday_2011 }
210
+ let(:last_fathersday) { fathersday_2010 }
211
+ let(:timeframe) { 2.months }
212
+ let(:cutoff) { fathersday - 2.months }
213
+
214
+ it "is this year if it's coming up within the timeframe" do
215
+ NorwegianHolidays.fathersday_for_season(cutoff + 1.day, timeframe).should eq(fathersday)
216
+ end
217
+
218
+ it "is this year if it's coming up in exactly the timeframe" do
219
+ NorwegianHolidays.fathersday_for_season(cutoff, timeframe).should eq(fathersday)
141
220
  end
142
- end
143
221
 
144
- context "after fathersday" do
145
- it "is next year" do
146
- NorwegianHolidays.next_fathersday(fathersday_2010 + 1.day).should eq(fathersday_2011)
222
+ it "is last year if it's too long til the next one" do
223
+ NorwegianHolidays.fathersday_for_season(cutoff - 1.day, timeframe).should eq(last_fathersday)
147
224
  end
148
225
  end
149
226
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: norwegian_holidays
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
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: 2012-01-04 00:00:00.000000000Z
12
+ date: 2012-01-19 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70138570566060 !ruby/object:Gem::Requirement
16
+ requirement: &70170598094940 !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: *70138570566060
24
+ version_requirements: *70170598094940
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: active_support
27
- requirement: &70138570565640 !ruby/object:Gem::Requirement
27
+ requirement: &70170598094500 !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: *70138570565640
35
+ version_requirements: *70170598094500
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: i18n
38
- requirement: &70138570565220 !ruby/object:Gem::Requirement
38
+ requirement: &70170598094080 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70138570565220
46
+ version_requirements: *70170598094080
47
47
  description: ! 'A Norwegian holiday calendar. Answers the age-old question: "When
48
48
  is mothersday this year?"'
49
49
  email: