holidays_rules 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1f8c525ac6cd60ee1eb5e0dc914fb2c81266975d
4
+ data.tar.gz: 5529a8420ad022205138a204fc5a48900f1645fc
5
+ SHA512:
6
+ metadata.gz: 3887988f463c1371d44b2ff457128172ea6f504450983e154d71f9d90dcd31a73d5bbc658d94b44042e8c63fff2557144b8a24ad7d490ce747c2655a8ed2aaba
7
+ data.tar.gz: 7861fedba6b483b06d4aa713f50dd4ddf89be8bc03bcb9d788988424c426cf4282c026ce065b2db8d1b322832682052abc9949943d57c353b291cab480b1ace5
@@ -0,0 +1,49 @@
1
+ module Feriados
2
+ module Argentina
3
+ include Feriados::Rules
4
+
5
+ def self.rules
6
+ [
7
+ DayOfMonth.new(1, 1),
8
+ DayOfMonth.new(24, 3),
9
+ DayOfMonth.new(2, 4),
10
+ DayOfMonth.new(1, 5),
11
+ DayOfMonth.new(25, 5),
12
+ DayOfMonth.new(20, 6),
13
+ DayOfMonth.new(9, 7),
14
+ DayOfMonth.new(8, 12),
15
+ DayOfMonth.new(25, 12),
16
+
17
+ Easter,
18
+ HolyThursday,
19
+ HolyFriday,
20
+ CarnivalMonday,
21
+ CarnivalTuesday,
22
+
23
+ FixWeekDay.new(3, 1, 8),
24
+ FixWeekDay.new(2, 1, 10),
25
+ FixWeekDay.new(4, 1, 11),
26
+
27
+ FixDate.new(2012, 2, 27),
28
+ FixDate.new(2012, 9, 24),
29
+ FixDate.new(2012, 4, 30),
30
+ FixDate.new(2012, 12, 24),
31
+
32
+ FixDate.new(2013, 1, 31),
33
+ FixDate.new(2013, 2, 20),
34
+ FixDate.new(2013, 1, 31),
35
+ FixDate.new(2013, 4, 1),
36
+ FixDate.new(2013, 6, 21),
37
+
38
+ FixDate.new(2014, 5, 2),
39
+ FixDate.new(2014, 12, 26),
40
+
41
+ FixDate.new(2015, 3, 23),
42
+ FixDate.new(2015, 12, 7),
43
+
44
+ FixDate.new(2016, 7, 8),
45
+ FixDate.new(2016, 12, 9)
46
+ ]
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,23 @@
1
+ module Feriados
2
+ module Brazil
3
+ include Feriados::Rules
4
+
5
+ def self.rules
6
+ [
7
+ DayOfMonth.new(1, 1),
8
+ DayOfMonth.new(21, 4),
9
+ DayOfMonth.new(1, 5),
10
+ DayOfMonth.new(4, 6),
11
+ DayOfMonth.new(7, 9),
12
+ DayOfMonth.new(12, 10),
13
+ DayOfMonth.new(2, 11),
14
+ DayOfMonth.new(15, 11),
15
+ DayOfMonth.new(25, 12),
16
+
17
+ HolyFriday,
18
+ CarnivalMonday,
19
+ CarnivalTuesday
20
+ ]
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,38 @@
1
+ require 'set'
2
+ require 'date'
3
+
4
+ module Feriados
5
+ class Calendar
6
+ def initialize
7
+ @rules = Set.new
8
+ end
9
+
10
+ def holiday?(date)
11
+ @rules.any? { |rule| rule.holiday?(date) }
12
+ end
13
+
14
+ def add(rule)
15
+ @rules << rule
16
+ end
17
+
18
+ def remove(rule)
19
+ @rules.delete(rule)
20
+ end
21
+
22
+ def push(rules)
23
+ @rules = rules
24
+ end
25
+ end
26
+
27
+ refine Date do
28
+ def holiday?
29
+ @@calendar.holiday?(self)
30
+ end
31
+ end
32
+
33
+ refine Date.singleton_class do
34
+ def calendar=(calendar)
35
+ @@calendar = calendar
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,142 @@
1
+ module Feriados
2
+ module Rules
3
+ class RuleBase
4
+ def hash
5
+ state.hash
6
+ end
7
+
8
+ def eql?(other)
9
+ hash == other.hash
10
+ end
11
+
12
+ def ==(other)
13
+ eql?(other)
14
+ end
15
+ end
16
+
17
+ class DayOfWeek < RuleBase
18
+ def initialize(wday)
19
+ @wday = wday
20
+ end
21
+
22
+ def holiday?(date)
23
+ @wday == date.wday
24
+ end
25
+
26
+ protected
27
+
28
+ def state
29
+ [@wday]
30
+ end
31
+ end
32
+
33
+ class DayOfMonth < RuleBase
34
+ def initialize(day, month)
35
+ @day = day
36
+ @month = month
37
+ end
38
+
39
+ def holiday?(date)
40
+ @day == date.day && @month == date.month
41
+ end
42
+
43
+ protected
44
+
45
+ def state
46
+ [@day, @month]
47
+ end
48
+ end
49
+
50
+ class FixDate < RuleBase
51
+ def initialize(year, month, day)
52
+ @year = year
53
+ @month = month
54
+ @day = day
55
+ end
56
+
57
+ def holiday?(date)
58
+ @year == date.year && @month == date.month && @day = date.day
59
+ end
60
+
61
+ protected
62
+
63
+ def state
64
+ [@year, @month, @day]
65
+ end
66
+ end
67
+
68
+ class Easter
69
+ def initialize(year)
70
+ @year = year
71
+ end
72
+
73
+ def date
74
+ a = @year % 19
75
+ b = @year / 100
76
+ c = @year % 100
77
+ d = b / 4
78
+ e = b % 4
79
+ f = (b + 8) / 25
80
+ g = (b - f + 1) / 3
81
+ h = (19 * a + b - d - g + 15) % 30
82
+ i = c / 4
83
+ k = c % 4
84
+ l = (32 + 2 * e + 2 * i - h - k) % 7
85
+ m = (a + 11 * h + 22 * l) / 451
86
+ month = (h + l - 7 * m + 114) / 31
87
+ day = ((h + l - 7 * m + 114) % 31) + 1
88
+
89
+ Date.new(@year, month, day)
90
+ end
91
+
92
+ def self.holiday?(date)
93
+ easter = Easter.new(date.year)
94
+
95
+ easter.date == date
96
+ end
97
+ end
98
+
99
+ class HolyFriday
100
+ def self.holiday?(date)
101
+ Easter.new(date.year).date - 2 == date
102
+ end
103
+ end
104
+
105
+ class HolyThursday
106
+ def self.holiday?(date)
107
+ Easter.new(date.year).date - 3 == date
108
+ end
109
+ end
110
+
111
+ class CarnivalMonday
112
+ def self.holiday?(date)
113
+ Easter.new(date.year).date - 48 == date
114
+ end
115
+ end
116
+
117
+ class CarnivalTuesday
118
+ def self.holiday?(date)
119
+ Easter.new(date.year).date - 47 == date
120
+ end
121
+ end
122
+
123
+ class FixWeekDay < RuleBase
124
+ def initialize(week, wday, month)
125
+ @week = week
126
+ @wday = wday
127
+ @month = month
128
+ end
129
+
130
+ def holiday?(date)
131
+ week = (date.day - 1) / 7 + 1
132
+ @wday == date.wday && @month == date.month && @week == week
133
+ end
134
+
135
+ protected
136
+
137
+ def state
138
+ [@week, @wday, @month]
139
+ end
140
+ end
141
+ end
142
+ end
data/lib/feriados.rb ADDED
@@ -0,0 +1,4 @@
1
+ require_relative './feriados/calendar'
2
+ require_relative './feriados/rules'
3
+ require_relative './feriados/argentina'
4
+ require_relative './feriados/brazil'
data/test/date_test.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class DateTest < Minitest::Test
4
+ include Feriados::Rules
5
+ include Feriados
6
+ using Feriados
7
+
8
+ def test_using_refinements
9
+ calendar = Calendar.new
10
+ Date.calendar = calendar
11
+
12
+ date = Date.new(2016, 5, 2)
13
+
14
+ calendar.add(FixDate.new(date.year, date.month, date.day))
15
+
16
+ assert calendar.holiday?(date)
17
+
18
+ assert date.holiday?
19
+ end
20
+ end
@@ -0,0 +1,287 @@
1
+ require 'test_helper'
2
+
3
+ class CalendarTest < Minitest::Test
4
+ include Feriados::Rules
5
+ include Feriados
6
+
7
+ def test_a_day_of_week_can_be_a_holiday
8
+ calendar = Calendar.new
9
+
10
+ a_saturday = Date.new(2016, 4, 30)
11
+
12
+ calendar.add(DayOfWeek.new(a_saturday.wday))
13
+
14
+ assert calendar.holiday?(a_saturday)
15
+ end
16
+
17
+ def test_a_day_of_week_can_be_not_a_holiday
18
+ calendar = Calendar.new
19
+
20
+ a_monday = Date.new(2016, 5, 2)
21
+
22
+ refute calendar.holiday?(a_monday)
23
+ end
24
+
25
+ def test_a_bunch_of_days_can_be_a_holiday
26
+ calendar = Calendar.new
27
+
28
+ a_saturday = Date.new(2016, 4, 30)
29
+ a_sunday = Date.new(2016, 5, 1)
30
+
31
+ calendar.add(DayOfWeek.new(a_saturday.wday))
32
+ calendar.add(DayOfWeek.new(a_sunday.wday))
33
+
34
+ assert calendar.holiday?(a_sunday)
35
+ assert calendar.holiday?(a_saturday)
36
+ end
37
+
38
+ def test_a_day_of_week_rule_can_be_deleted
39
+ calendar = Calendar.new
40
+
41
+ date = Date.new(2016, 5, 1)
42
+
43
+ calendar.add(DayOfWeek.new(date.wday))
44
+
45
+ assert calendar.holiday?(date)
46
+
47
+ calendar.remove(DayOfWeek.new(date.wday))
48
+
49
+ refute calendar.holiday?(date)
50
+ end
51
+
52
+ def test_a_date_of_month_can_be_a_holiday
53
+ calendar = Calendar.new
54
+
55
+ a_january_first = Date.new(2016, 1, 1)
56
+
57
+ calendar.add(DayOfMonth.new(1, 1))
58
+
59
+ assert calendar.holiday?(a_january_first)
60
+ end
61
+
62
+ def test_a_day_of_month_can_be_not_a_holiday
63
+ calendar = Calendar.new
64
+
65
+ a_day = Date.new(2016, 1, 2)
66
+
67
+ calendar.add(DayOfMonth.new(1, 1))
68
+
69
+ refute calendar.holiday?(a_day)
70
+ end
71
+
72
+ def test_a_bunch_of_days_of_month_can_be_a_holiday
73
+ calendar = Calendar.new
74
+
75
+ a_christmas = Date.new(2016, 12, 25)
76
+ a_january_first = Date.new(2016, 1, 1)
77
+
78
+ calendar.add(DayOfMonth.new(1, 1))
79
+ calendar.add(DayOfMonth.new(25, 12))
80
+
81
+ assert calendar.holiday?(a_christmas)
82
+ assert calendar.holiday?(a_january_first)
83
+ end
84
+
85
+ def test_a_day_of_month_rule_can_be_deleted
86
+ calendar = Calendar.new
87
+
88
+ date = Date.new(2016, 5, 1)
89
+
90
+ calendar.add(DayOfMonth.new(date.day, date.month))
91
+
92
+ assert calendar.holiday?(date)
93
+
94
+ calendar.remove(DayOfMonth.new(date.day, date.month))
95
+
96
+ refute calendar.holiday?(date)
97
+ end
98
+
99
+ def test_a_date_can_be_a_holiday
100
+ calendar = Calendar.new
101
+
102
+ date = Date.new(2016, 1, 1)
103
+
104
+ calendar.add(FixDate.new(2016, 1, 1))
105
+
106
+ assert calendar.holiday?(date)
107
+ end
108
+
109
+ def test_a_date_can_be_not_a_holiday
110
+ calendar = Calendar.new
111
+
112
+ refute calendar.holiday?(Date.new(2016, 1, 1))
113
+ end
114
+
115
+ def test_a_bunch_of_dates_can_be_a_holiday
116
+ calendar = Calendar.new
117
+
118
+ date = Date.new(2016, 4, 29)
119
+ another_date = Date.new(2016, 4, 30)
120
+
121
+ calendar.add(FixDate.new(2016, 4, 29))
122
+ calendar.add(FixDate.new(2016, 4, 30))
123
+
124
+ assert calendar.holiday?(date)
125
+ assert calendar.holiday?(another_date)
126
+ end
127
+
128
+ def test_a_date_rule_can_be_deleted
129
+ calendar = Calendar.new
130
+
131
+ date = Date.new(2016, 5, 1)
132
+
133
+ calendar.add(FixDate.new(date.year, date.month, date.day))
134
+
135
+ assert calendar.holiday?(date)
136
+
137
+ calendar.remove(FixDate.new(date.year, date.month, date.day))
138
+
139
+ refute calendar.holiday?(date)
140
+ end
141
+
142
+ def test_easter
143
+ calendar = Calendar.new
144
+
145
+ date = Date.new(2016, 3, 27)
146
+
147
+ calendar.add(Easter)
148
+
149
+ assert calendar.holiday?(date)
150
+ end
151
+
152
+ def test_not_easter
153
+ calendar = Calendar.new
154
+
155
+ date = Date.new(2016, 3, 26)
156
+
157
+ calendar.add(Easter)
158
+
159
+ refute calendar.holiday?(date)
160
+ end
161
+
162
+ def test_remove_easter
163
+ calendar = Calendar.new
164
+
165
+ date = Date.new(2016, 3, 27)
166
+
167
+ calendar.add(Easter)
168
+
169
+ assert calendar.holiday?(date)
170
+
171
+ calendar.remove(Easter)
172
+
173
+ refute calendar.holiday?(date)
174
+ end
175
+
176
+ def test_holy_friday
177
+ calendar = Calendar.new
178
+
179
+ date = Date.new(2016, 3, 25)
180
+
181
+ calendar.add(HolyFriday)
182
+
183
+ assert calendar.holiday?(date)
184
+ end
185
+
186
+ def test_not_holy_friday
187
+ calendar = Calendar.new
188
+
189
+ date = Date.new(2016, 3, 20)
190
+
191
+ calendar.add(HolyFriday)
192
+
193
+ refute calendar.holiday?(date)
194
+ end
195
+
196
+ def test_remove_holy_friday
197
+ calendar = Calendar.new
198
+
199
+ date = Date.new(2016, 3, 25)
200
+
201
+ calendar.add(HolyFriday)
202
+
203
+ assert calendar.holiday?(date)
204
+
205
+ calendar.remove(HolyFriday)
206
+
207
+ refute calendar.holiday?(date)
208
+ end
209
+
210
+ def test_holy_thursday
211
+ calendar = Calendar.new
212
+
213
+ date = Date.new(2016, 3, 24)
214
+
215
+ calendar.add(HolyThursday)
216
+
217
+ assert calendar.holiday?(date)
218
+ end
219
+
220
+ def test_fix_week_day_rule
221
+ calendar = Calendar.new
222
+
223
+ calendar.add(FixWeekDay.new(4, 1, 11))
224
+ calendar.add(FixWeekDay.new(3, 1, 8))
225
+
226
+ assert calendar.holiday?(Date.new(2016, 11, 28))
227
+ assert calendar.holiday?(Date.new(2016, 8, 15))
228
+ end
229
+
230
+ def test_not_fix_week_day_rule
231
+ calendar = Calendar.new
232
+
233
+ date = Date.new(2016, 5, 2)
234
+
235
+ calendar.add(FixWeekDay.new(2, 1, 5))
236
+
237
+ refute calendar.holiday?(date)
238
+ end
239
+
240
+ def test_remove_fix_week_day_rule
241
+ calendar = Calendar.new
242
+
243
+ date = Date.new(2016, 11, 28)
244
+
245
+ calendar.add(FixWeekDay.new(4, 1, 11))
246
+
247
+ assert calendar.holiday?(date)
248
+
249
+ calendar.remove(FixWeekDay.new(4, 1, 11))
250
+
251
+ refute calendar.holiday?(date)
252
+ end
253
+
254
+ def test_load_argentinian_holidays
255
+ calendar = Calendar.new
256
+
257
+ Feriados::Argentina.rules.each { |rule| calendar.add rule }
258
+
259
+ assert calendar.holiday?(Date.new(2016, 1, 1))
260
+
261
+ assert calendar.holiday?(Date.new(2016, 2, 8))
262
+ assert calendar.holiday?(Date.new(2016, 2, 9))
263
+
264
+ assert calendar.holiday?(Date.new(2016, 3, 24))
265
+ assert calendar.holiday?(Date.new(2016, 3, 25))
266
+ assert calendar.holiday?(Date.new(2016, 3, 27))
267
+
268
+ assert calendar.holiday?(Date.new(2016, 4, 2))
269
+
270
+ assert calendar.holiday?(Date.new(2016, 5, 1))
271
+ assert calendar.holiday?(Date.new(2016, 5, 25))
272
+
273
+ assert calendar.holiday?(Date.new(2016, 6, 20))
274
+
275
+ assert calendar.holiday?(Date.new(2016, 7, 8))
276
+ assert calendar.holiday?(Date.new(2016, 7, 9))
277
+
278
+ assert calendar.holiday?(Date.new(2016, 8, 15))
279
+
280
+ assert calendar.holiday?(Date.new(2016, 10, 10))
281
+
282
+ assert calendar.holiday?(Date.new(2016, 11, 28))
283
+
284
+ assert calendar.holiday?(Date.new(2016, 12, 8))
285
+ assert calendar.holiday?(Date.new(2016, 12, 25))
286
+ end
287
+ end
data/test/runner.rb ADDED
@@ -0,0 +1,10 @@
1
+ ARGV.select do |argument|
2
+ case argument
3
+ when /\*/ then
4
+ Dir.glob(argument).each do |file|
5
+ require File.expand_path(file)
6
+ end
7
+ else
8
+ require File.expand_path(argument)
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/reporters'
3
+
4
+ Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(color: true)]
5
+
6
+ require_relative '../lib/feriados'
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: holidays_rules
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sebastian Rabuini
8
+ - Rubens Praser Júnior
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-09-09 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Make holidays calendars from rules, based on the gem Feriados by Sebastian
15
+ Rabuini
16
+ email:
17
+ - srabuini@gmail.com
18
+ - praser@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - lib/feriados.rb
24
+ - lib/feriados/argentina.rb
25
+ - lib/feriados/brazil.rb
26
+ - lib/feriados/calendar.rb
27
+ - lib/feriados/rules.rb
28
+ - test/date_test.rb
29
+ - test/feriados_test.rb
30
+ - test/runner.rb
31
+ - test/test_helper.rb
32
+ homepage: https://github.com/praser/feriados
33
+ licenses:
34
+ - MIT
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 2.5.1
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: Holidays calendars
56
+ test_files:
57
+ - test/date_test.rb
58
+ - test/feriados_test.rb
59
+ - test/runner.rb
60
+ - test/test_helper.rb