feriados 3.0.2 → 4.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 0bd8a7c31bfc2d0719d2688b9d2a245de3e610f3
4
- data.tar.gz: db6ae5a808d827282e0a95c9b0c91da513cc4d14
2
+ SHA256:
3
+ metadata.gz: 2a0f8a45bd3052939f53aa3ed238852d161f0be93d8758e9e8bf8354b58a3969
4
+ data.tar.gz: 592ae7797b8f0999346996e0409163deaccd935c3967cb7c5e5067c13d3b31db
5
5
  SHA512:
6
- metadata.gz: 06a6072851baaaae0eb356890421d19f9fed0ab0ff07f806dd4174f893be28e8a00a9ec0b806fd1042b84917f6d89d64709054c7f93df6dec1029618a68c608e
7
- data.tar.gz: e3b0968dc9f3cd7cf4906add806c42952d7b5f660fa4ee501195d22001034513410cec70970ebd751ff30da0ec09f5ae4367eb211e14aacffe53e1299515e20f
6
+ metadata.gz: 1154029a3b4446ee620fde02254b31edf874374d9bbbdcce948ff86c9c796d83d321dcbb262c83a8e344962ab10e7be8a616096d7c44311627aa4880368f708b
7
+ data.tar.gz: dc180372559ca005f02d418a66d2869deeb0f56e5a9caacd0d4ed8ce47545da69278e519ebb54070907a4c96640c171f63ac932b340d99ac35963cd7fe78120f
@@ -1,91 +1,5 @@
1
- require 'date'
1
+ require 'yaml'
2
2
 
3
- module Feriados
4
- DAYS = { sunday: 7, monday: 1, tuesday: 2, wednesday: 3, thursday: 4,
5
- friday: 5, saturday: 6 }
6
-
7
- def self.add(description, options = {})
8
- month = options[:month] || 0
9
- @holidays_by_month ||= {}
10
- @holidays_by_month[month] ||= []
11
- @holidays_by_month[month] << { description: description }.merge!(options)
12
- end
13
-
14
- def self.calculated(th, day, month, year)
15
- n = { first: 1, second: 2, third: 3, fourth: 4 }
16
-
17
- week = n[th]
18
- date = Date.civil(year, month, 1)
19
- offset = DAYS[day] - date.wday
20
- offset += 7 if offset < 0
21
- offset += 7 * (week - 1)
22
- date + offset
23
- end
24
-
25
- def self.easter(year)
26
- a = year % 19
27
- b = year / 100
28
- c = year % 100
29
- d = b / 4
30
- e = b % 4
31
- f = (b + 8) / 25
32
- g = (b - f + 1) / 3
33
- h = (19 * a + b - d - g + 15) % 30
34
- i = c / 4
35
- k = c % 4
36
- l = (32 + 2 * e + 2 * i - h - k) % 7
37
- m = (a + 11 * h + 22 * l) / 451
38
- month = (h + l - 7 * m + 114) / 31
39
- day = ((h + l - 7 * m + 114) % 31) + 1
40
-
41
- Date.civil(year, month, day)
42
- end
43
-
44
- def self.between(start_date, end_date)
45
- dates, list = {}, []
46
-
47
- (start_date..end_date).each do |date|
48
- dates[date.year] = [0] unless dates[date.year]
49
- unless dates[date.year].include?(date.month)
50
- dates[date.year] << date.month
51
- end
52
- end
53
-
54
- dates.each do |year, months|
55
- months.each do |month|
56
- next unless (holidays = @holidays_by_month[month])
57
- holidays.each do |holiday|
58
- description = holiday[:description]
59
- date = if holiday[:function]
60
- holiday[:function].call(year)
61
- elsif holiday[:fix_change]
62
- calculated(holiday[:fix_change][0], holiday[:fix_change][1],
63
- month, year)
64
- elsif holiday[:variable_change]
65
- holiday[:variable_change].call(Date.civil(year, month,
66
- holiday[:day]))
67
- else
68
- Date.civil(holiday[:year] || year, month, holiday[:day])
69
- end
70
-
71
- if date.between?(start_date, end_date) && date.year == year
72
- list << { date: date, description: description }
73
- end
74
- end
75
- end
76
- end
77
-
78
- list.sort_by { |holidays| holidays[:date] }
79
- end
80
- end
81
-
82
- class Date
83
- def holiday?
84
- holidays = Feriados.between(self, self)
85
- holidays && !holidays.empty?
86
- end
87
-
88
- def week_end?
89
- wday == 6 || wday == 0
90
- end
91
- end
3
+ require_relative './feriados/rules'
4
+ require_relative './feriados/calendar'
5
+ require_relative './feriados/loader'
@@ -0,0 +1,56 @@
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 holiday_name(date)
15
+ rules.find { |rule| rule.holiday?(date) }&.name
16
+ end
17
+
18
+ def add(rule)
19
+ rules << rule
20
+ end
21
+
22
+ def remove(rule)
23
+ rules.delete(rule)
24
+ end
25
+
26
+ def load(rules)
27
+ Loader.new(rules, self).load
28
+ end
29
+
30
+ def eql?(other)
31
+ rules == other.rules
32
+ end
33
+
34
+ def ==(other)
35
+ eql?(other)
36
+ end
37
+
38
+ protected
39
+
40
+ attr_accessor :rules
41
+ end
42
+
43
+ refine Date do
44
+ @calendar = nil
45
+
46
+ def holiday?
47
+ @@calendar.holiday?(self)
48
+ end
49
+ end
50
+
51
+ refine Date.singleton_class do
52
+ def calendar=(calendar)
53
+ @@calendar = calendar
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,20 @@
1
+ module Feriados
2
+ class Loader
3
+ include Rules
4
+
5
+ def initialize(rules, calendar)
6
+ @rules = rules
7
+ @calendar = calendar
8
+ end
9
+
10
+ def load
11
+ rules.each do |rule|
12
+ calendar.add(Rules.create_with(rule))
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :rules, :calendar
19
+ end
20
+ end
@@ -0,0 +1,73 @@
1
+ require 'ostruct'
2
+ require 'forwardable'
3
+
4
+ require_relative './rules/rule_base'
5
+ require_relative './rules/function'
6
+ require_relative './rules/carnival_monday'
7
+ require_relative './rules/carnival_tuesday'
8
+ require_relative './rules/day_of_month'
9
+ require_relative './rules/day_of_week'
10
+ require_relative './rules/easter'
11
+ require_relative './rules/fix_date'
12
+ require_relative './rules/fix_week_day'
13
+ require_relative './rules/holy_friday'
14
+ require_relative './rules/holy_thursday'
15
+
16
+ module Feriados
17
+ module Rules
18
+ class Rule
19
+ extend Forwardable
20
+
21
+ def_delegators :@rule, :day, :month, :year, :week
22
+
23
+ attr_reader :rule
24
+
25
+ def initialize(rule)
26
+ @rule = OpenStruct.new(rule)
27
+ end
28
+
29
+ def week_day?
30
+ rule.day && rule.month && rule.week
31
+ end
32
+
33
+ def fix_date?
34
+ rule.day && rule.month && rule.year
35
+ end
36
+
37
+ def day_of_month?
38
+ rule.day && rule.month && !fix_date? && !week_day?
39
+ end
40
+
41
+ def function?
42
+ functions = %i[easter holy_thursday holy_friday carnival_monday
43
+ carnival_tuesday]
44
+
45
+ functions.map { |e| rule.respond_to?(e) }.any?
46
+ end
47
+
48
+ def name
49
+ rule[:name] || rule.to_h.values.first
50
+ end
51
+
52
+ def function_name
53
+ rule.to_h.keys.first.to_s.split('_').collect(&:capitalize).join
54
+ end
55
+ end
56
+
57
+ def self.create_with(rule)
58
+ rule = Rule.new(rule)
59
+ return DayOfMonth.new(rule.day, rule.month, rule.name) if rule.day_of_month?
60
+ return FixWeekDay.new(rule.week, rule.day, rule.month, rule.name) if rule.week_day?
61
+ return FixDate.new(rule.year, rule.month, rule.day, rule.name) if rule.fix_date?
62
+
63
+ raise ArgumentError, "Argument #{rule} is not a valid rule" unless rule.function?
64
+
65
+ function_name = rule.function_name
66
+ class_name = "Feriados::Rules::#{function_name}".split('::')
67
+ klass = class_name.inject(Object) { |obj, const| obj.const_get(const) }
68
+ klass.name = rule.name
69
+
70
+ klass
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,9 @@
1
+ module Feriados
2
+ module Rules
3
+ class CarnivalMonday < Function
4
+ def self.holiday?(date)
5
+ Easter.new(date.year).date - 48 == date
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Feriados
2
+ module Rules
3
+ class CarnivalTuesday < Function
4
+ def self.holiday?(date)
5
+ Easter.new(date.year).date - 47 == date
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ module Feriados
2
+ module Rules
3
+ class DayOfMonth < RuleBase
4
+ def initialize(day, month, name = nil)
5
+ super(name)
6
+ @day = day
7
+ @month = month
8
+ end
9
+
10
+ def holiday?(date)
11
+ day == date.day && month == date.month
12
+ end
13
+
14
+ protected
15
+
16
+ attr_reader :day, :month
17
+
18
+ def state
19
+ [day, month]
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ module Feriados
2
+ module Rules
3
+ class DayOfWeek < RuleBase
4
+ def initialize(wday, name = nil)
5
+ super(name)
6
+ @wday = wday
7
+ end
8
+
9
+ def holiday?(date)
10
+ wday == date.wday
11
+ end
12
+
13
+ protected
14
+
15
+ attr_reader :wday
16
+
17
+ def state
18
+ [wday]
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,78 @@
1
+ module Feriados
2
+ module Rules
3
+ class Easter < Function
4
+ @name = nil
5
+
6
+ def initialize(year)
7
+ @year = year
8
+ end
9
+
10
+ def date
11
+ result = h + l - 7 * m + 114
12
+
13
+ month = result / 31
14
+ day = (result % 31) + 1
15
+
16
+ Date.new(year, month, day)
17
+ end
18
+
19
+ def self.holiday?(date)
20
+ easter = Easter.new(date.year)
21
+
22
+ easter.date == date
23
+ end
24
+
25
+ private
26
+
27
+ def a
28
+ year % 19
29
+ end
30
+
31
+ def b
32
+ year / 100
33
+ end
34
+
35
+ def c
36
+ year % 100
37
+ end
38
+
39
+ def d
40
+ b / 4
41
+ end
42
+
43
+ def e
44
+ b % 4
45
+ end
46
+
47
+ def f
48
+ (b + 8) / 25
49
+ end
50
+
51
+ def g
52
+ (b - f + 1) / 3
53
+ end
54
+
55
+ def h
56
+ (19 * a + b - d - g + 15) % 30
57
+ end
58
+
59
+ def i
60
+ c / 4
61
+ end
62
+
63
+ def k
64
+ c % 4
65
+ end
66
+
67
+ def l
68
+ (32 + 2 * e + 2 * i - h - k) % 7
69
+ end
70
+
71
+ def m
72
+ (a + 11 * h + 22 * l) / 451
73
+ end
74
+
75
+ attr_reader :year
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,24 @@
1
+ module Feriados
2
+ module Rules
3
+ class FixDate < RuleBase
4
+ def initialize(year, month, day, name = nil)
5
+ super(name)
6
+ @year = year
7
+ @month = month
8
+ @day = day
9
+ end
10
+
11
+ def holiday?(date)
12
+ year == date.year && month == date.month && day == date.day
13
+ end
14
+
15
+ protected
16
+
17
+ attr_reader :day, :month, :year
18
+
19
+ def state
20
+ [year, month, day]
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ module Feriados
2
+ module Rules
3
+ class FixWeekDay < RuleBase
4
+ def initialize(week, wday, month, name = nil)
5
+ super(name)
6
+ @week = week
7
+ @wday = wday
8
+ @month = month
9
+ end
10
+
11
+ def holiday?(date)
12
+ date_from_week = (date.day - 1) / 7 + 1
13
+ wday == date.wday && month == date.month && week == date_from_week
14
+ end
15
+
16
+ protected
17
+
18
+ attr_reader :week, :wday, :month
19
+
20
+ def state
21
+ [week, wday, month]
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ module Feriados
2
+ module Rules
3
+ class Function
4
+ class << self
5
+ attr_accessor :name
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Feriados
2
+ module Rules
3
+ class HolyFriday < Function
4
+ def self.holiday?(date)
5
+ Easter.new(date.year).date - 2 == date
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Feriados
2
+ module Rules
3
+ class HolyThursday < Function
4
+ def self.holiday?(date)
5
+ Easter.new(date.year).date - 3 == date
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ module Feriados
2
+ module Rules
3
+ class RuleBase
4
+ attr_reader :name
5
+
6
+ def initialize(name = nil)
7
+ @name = name
8
+ end
9
+
10
+ def hash
11
+ state.hash
12
+ end
13
+
14
+ def eql?(other)
15
+ hash == other.hash
16
+ end
17
+
18
+ def ==(other)
19
+ eql?(other)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,80 +1,20 @@
1
- # encoding: UTF-8
1
+ require 'test_helper'
2
2
 
3
- require 'test_helper.rb'
3
+ class DateTest < Minitest::Test
4
+ include Feriados::Rules
5
+ include Feriados
6
+ using Feriados
4
7
 
5
- class DateTests < MiniTest::Test
6
- def test_extending_date_class
7
- @date = Date.civil(2012, 1, 1)
8
+ def test_using_refinements
9
+ calendar = Calendar.new
10
+ Date.calendar = calendar
8
11
 
9
- assert @date.respond_to?('holiday?')
10
- assert @date.respond_to?('week_end?')
11
- end
12
-
13
- def test_week_end
14
- (2012..2015).each do |year|
15
- (1..12).each do |month|
16
- nths.each do |n|
17
- regulars.each do |day|
18
- refute Feriados.calculated(n, day, month, year).week_end?
19
- end
20
- week_end.each do |day|
21
- assert Feriados.calculated(n, day, month, year).week_end?
22
- end
23
- end
24
- end
25
- end
26
- end
27
-
28
- def test_calculated
29
- # Dia de la bandera.
30
- assert_equal 18, Feriados.calculated(:third, :monday, 6, 2012).day
31
- assert_equal 17, Feriados.calculated(:third, :monday, 6, 2013).day
32
- assert_equal 16, Feriados.calculated(:third, :monday, 6, 2014).day
33
- assert_equal 15, Feriados.calculated(:third, :monday, 6, 2015).day
12
+ date = Date.new(2016, 5, 2)
34
13
 
35
- # Diecisiete de Agosto.
36
- assert_equal 20, Feriados.calculated(:third, :monday, 8, 2012).day
37
- assert_equal 19, Feriados.calculated(:third, :monday, 8, 2013).day
38
- assert_equal 18, Feriados.calculated(:third, :monday, 8, 2014).day
39
- assert_equal 17, Feriados.calculated(:third, :monday, 8, 2015).day
14
+ calendar.add(FixDate.new(date.year, date.month, date.day))
40
15
 
41
- # Easter.
42
- assert_equal 8, Feriados.easter(2012).day
43
- assert_equal 31, Feriados.easter(2013).day
44
- assert_equal 20, Feriados.easter(2014).day
45
- assert_equal 5, Feriados.easter(2015).day
46
-
47
- # Carnaval
48
- assert_equal 11, (Feriados.easter(2013) - 48).day
49
- assert_equal 3, (Feriados.easter(2014) - 48).day
50
- assert_equal 16, (Feriados.easter(2015) - 48).day
51
-
52
- assert_equal 12, (Feriados.easter(2013) - 47).day
53
- assert_equal 4, (Feriados.easter(2014) - 47).day
54
- assert_equal 17, (Feriados.easter(2015) - 47).day
55
- end
56
-
57
- def test_fix_holidays
58
- 2012.upto(2015) do |year|
59
- assert Date.civil(year, 1, 1).holiday?
60
- assert Date.civil(year, 3, 24).holiday?
61
- assert Date.civil(year, 4, 2).holiday?
62
- assert Date.civil(year, 5, 1).holiday?
63
- assert Date.civil(year, 7, 9).holiday?
64
- assert Date.civil(year, 12, 8).holiday?
65
- assert Date.civil(year, 12, 25).holiday?
66
- end
67
- end
68
-
69
- def regulars
70
- [:monday, :tuesday, :wednesday, :thursday, :friday]
71
- end
72
-
73
- def week_end
74
- [:saturday, :sunday]
75
- end
16
+ assert calendar.holiday?(date)
76
17
 
77
- def nths
78
- [:first, :second, :third, :fourth]
18
+ assert date.holiday?
79
19
  end
80
20
  end
@@ -0,0 +1,63 @@
1
+ require 'test_helper'
2
+
3
+ class DayOfMonthTest < Minitest::Test
4
+ include Feriados::Rules
5
+ include Feriados
6
+
7
+ def test_a_date_of_month_can_be_a_holiday
8
+ calendar = Calendar.new
9
+
10
+ a_january_first = Date.new(2016, 1, 1)
11
+
12
+ calendar.add(DayOfMonth.new(1, 1))
13
+
14
+ assert calendar.holiday?(a_january_first)
15
+ end
16
+
17
+ def test_a_date_of_month_can_have_a_name
18
+ calendar = Calendar.new
19
+
20
+ a_january_first = Date.new(2016, 1, 1)
21
+
22
+ calendar.add(DayOfMonth.new(1, 1, 'new year'))
23
+
24
+ assert_equal 'new year', calendar.holiday_name(a_january_first)
25
+ end
26
+
27
+ def test_a_day_of_month_can_be_not_a_holiday
28
+ calendar = Calendar.new
29
+
30
+ a_day = Date.new(2016, 1, 2)
31
+
32
+ calendar.add(DayOfMonth.new(1, 1))
33
+
34
+ refute calendar.holiday?(a_day)
35
+ end
36
+
37
+ def test_a_bunch_of_days_of_month_can_be_a_holiday
38
+ calendar = Calendar.new
39
+
40
+ a_christmas = Date.new(2016, 12, 25)
41
+ a_january_first = Date.new(2016, 1, 1)
42
+
43
+ calendar.add(DayOfMonth.new(1, 1))
44
+ calendar.add(DayOfMonth.new(25, 12))
45
+
46
+ assert calendar.holiday?(a_christmas)
47
+ assert calendar.holiday?(a_january_first)
48
+ end
49
+
50
+ def test_a_day_of_month_rule_can_be_deleted
51
+ calendar = Calendar.new
52
+
53
+ date = Date.new(2016, 5, 1)
54
+
55
+ calendar.add(DayOfMonth.new(date.day, date.month))
56
+
57
+ assert calendar.holiday?(date)
58
+
59
+ calendar.remove(DayOfMonth.new(date.day, date.month))
60
+
61
+ refute calendar.holiday?(date)
62
+ end
63
+ end
@@ -0,0 +1,71 @@
1
+ require 'test_helper'
2
+
3
+ class DayOfWeekTest < 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__a_name
18
+ calendar = Calendar.new
19
+
20
+ a_saturday = Date.new(2016, 4, 30)
21
+
22
+ calendar.add(DayOfWeek.new(a_saturday.wday, 'saturday night live'))
23
+
24
+ assert_equal 'saturday night live', calendar.holiday_name(a_saturday)
25
+ end
26
+
27
+ def test_a_day_of_week_with_no_a_name
28
+ calendar = Calendar.new
29
+
30
+ a_saturday = Date.new(2016, 4, 30)
31
+
32
+ calendar.add(DayOfWeek.new(a_saturday.wday))
33
+
34
+ assert_nil calendar.holiday_name(a_saturday)
35
+ end
36
+
37
+ def test_a_day_of_week_can_be_not_a_holiday
38
+ calendar = Calendar.new
39
+
40
+ a_monday = Date.new(2016, 5, 2)
41
+
42
+ refute calendar.holiday?(a_monday)
43
+ end
44
+
45
+ def test_a_bunch_of_days_can_be_a_holiday
46
+ calendar = Calendar.new
47
+
48
+ a_saturday = Date.new(2016, 4, 30)
49
+ a_sunday = Date.new(2016, 5, 1)
50
+
51
+ calendar.add(DayOfWeek.new(a_saturday.wday))
52
+ calendar.add(DayOfWeek.new(a_sunday.wday))
53
+
54
+ assert calendar.holiday?(a_sunday)
55
+ assert calendar.holiday?(a_saturday)
56
+ end
57
+
58
+ def test_a_day_of_week_rule_can_be_deleted
59
+ calendar = Calendar.new
60
+
61
+ date = Date.new(2016, 5, 1)
62
+
63
+ calendar.add(DayOfWeek.new(date.wday))
64
+
65
+ assert calendar.holiday?(date)
66
+
67
+ calendar.remove(DayOfWeek.new(date.wday))
68
+
69
+ refute calendar.holiday?(date)
70
+ end
71
+ end
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ class EasterTest < Minitest::Test
4
+ include Feriados::Rules
5
+ include Feriados
6
+
7
+ def test_easter
8
+ calendar = Calendar.new
9
+
10
+ date = Date.new(2016, 3, 27)
11
+
12
+ calendar.add(Easter)
13
+
14
+ assert calendar.holiday?(date)
15
+ end
16
+
17
+ def test_easter_can_have_name
18
+ calendar = Calendar.new
19
+
20
+ date = Date.new(2016, 3, 27)
21
+
22
+ Easter.name = 'pascua'
23
+
24
+ calendar.add(Easter)
25
+
26
+ assert_equal 'pascua', calendar.holiday_name(date)
27
+ end
28
+
29
+ def test_not_easter
30
+ calendar = Calendar.new
31
+
32
+ date = Date.new(2016, 3, 26)
33
+
34
+ calendar.add(Easter)
35
+
36
+ refute calendar.holiday?(date)
37
+ end
38
+
39
+ def test_remove_easter
40
+ calendar = Calendar.new
41
+
42
+ date = Date.new(2016, 3, 27)
43
+
44
+ calendar.add(Easter)
45
+
46
+ assert calendar.holiday?(date)
47
+
48
+ calendar.remove(Easter)
49
+
50
+ refute calendar.holiday?(date)
51
+ end
52
+ end
@@ -0,0 +1,66 @@
1
+ require 'test_helper'
2
+
3
+ class CalendarTest < Minitest::Test
4
+ include Feriados::Rules
5
+ include Feriados
6
+
7
+ def test_load_argentinian_holidays
8
+ calendar = Calendar.new
9
+
10
+ file = File.join(__dir__, './argentina.yml')
11
+ rules = YAML.load_file(file)
12
+
13
+ calendar.load(rules)
14
+
15
+ holidays2020_data = [
16
+ [1, 1, 'Año nuevo'],
17
+ [2, 24, 'Lunes de carnaval'],
18
+ [2, 25, 'Martes de carnaval'],
19
+ [3, 23, 'Feriado con fines turísticos'],
20
+ [3, 24, 'Día Nacional de la Memoria por la Verdad y la Justicia'],
21
+ [4, 2, 'Día del Veterano y de los Caídos en la Guerra de Malvinas'],
22
+ [4, 9, 'Jueves Santo'],
23
+ [4, 10, 'Viernes Santo'],
24
+ [4, 12, 'Pascua'],
25
+ [5, 1, 'Día del Trabajador'],
26
+ [5, 25, 'Día de la Revolución de Mayo'],
27
+ [6, 15, 'Día Paso a la Inmortalidad del General Martín Miguel de Güemes'],
28
+ [6, 20, 'Día de la Bandera'],
29
+ [7, 9, 'Día de la Independencia'],
30
+ [7, 10, 'Feriado con fines turísticos'],
31
+ [8, 17, 'Paso a la Inmortalidad del Gral. José de San Martín'],
32
+ [10, 12, 'Día del Respeto a la Diversidad Cultural'],
33
+ [11, 23, 'Día de la Soberanía Nacional'],
34
+ [12, 7, 'Feriado con fines turísticos'],
35
+ [12, 8, 'Inmaculada Concepción de María'],
36
+ [12, 25, 'Navidad']
37
+ ]
38
+
39
+ holidays2020_data.each do |month, day, description|
40
+ date = Date.new(2020, month, day)
41
+
42
+ assert calendar.holiday?(date), date
43
+
44
+ assert_equal description, calendar.holiday_name(date), date
45
+ end
46
+
47
+ date = Date.new(2020, 1, 1)
48
+ detected_holidays = []
49
+
50
+ 0.upto(365) do |day|
51
+ detected_holidays << calendar.holiday?(date + day)
52
+ end
53
+
54
+ assert_equal(holidays2020_data.count, detected_holidays.count { |e| e })
55
+ end
56
+
57
+ def test_load_incomplete_rules
58
+ rules = {}
59
+
60
+ calendar = Calendar.new
61
+
62
+ calendar.load(rules)
63
+
64
+ assert_equal calendar, Calendar.new
65
+ end
66
+ end
@@ -0,0 +1,59 @@
1
+ require 'test_helper'
2
+
3
+ class FixDateTest < Minitest::Test
4
+ include Feriados::Rules
5
+ include Feriados
6
+
7
+ def test_a_fix_date_can_be_a_holiday
8
+ calendar = Calendar.new
9
+
10
+ date = Date.new(2016, 1, 1)
11
+
12
+ calendar.add(FixDate.new(2016, 1, 1))
13
+
14
+ assert calendar.holiday?(date)
15
+ end
16
+
17
+ def test_a_fix_date_can_have_a_name
18
+ calendar = Calendar.new
19
+
20
+ date = Date.new(2016, 1, 1)
21
+
22
+ calendar.add(FixDate.new(2016, 1, 1, 'new year'))
23
+
24
+ assert calendar.holiday_name(date)
25
+ end
26
+
27
+ def test_a_fix_date_can_be_not_a_holiday
28
+ calendar = Calendar.new
29
+
30
+ refute calendar.holiday?(Date.new(2016, 1, 1))
31
+ end
32
+
33
+ def test_a_bunch_of_dates_can_be_a_holiday
34
+ calendar = Calendar.new
35
+
36
+ date = Date.new(2016, 4, 29)
37
+ another_date = Date.new(2016, 4, 30)
38
+
39
+ calendar.add(FixDate.new(2016, 4, 29))
40
+ calendar.add(FixDate.new(2016, 4, 30))
41
+
42
+ assert calendar.holiday?(date)
43
+ assert calendar.holiday?(another_date)
44
+ end
45
+
46
+ def test_a_fix_date_rule_can_be_deleted
47
+ calendar = Calendar.new
48
+
49
+ date = Date.new(2016, 5, 1)
50
+
51
+ calendar.add(FixDate.new(date.year, date.month, date.day))
52
+
53
+ assert calendar.holiday?(date)
54
+
55
+ calendar.remove(FixDate.new(date.year, date.month, date.day))
56
+
57
+ refute calendar.holiday?(date)
58
+ end
59
+ end
@@ -0,0 +1,48 @@
1
+ require 'test_helper'
2
+
3
+ class FixWeekDayTest < Minitest::Test
4
+ include Feriados::Rules
5
+ include Feriados
6
+
7
+ def test_fix_week_day_rule
8
+ calendar = Calendar.new
9
+
10
+ calendar.add(FixWeekDay.new(4, 1, 11))
11
+ calendar.add(FixWeekDay.new(3, 1, 8))
12
+
13
+ assert calendar.holiday?(Date.new(2016, 11, 28))
14
+ assert calendar.holiday?(Date.new(2016, 8, 15))
15
+ end
16
+
17
+ def test_fix_week_can_have_a_name
18
+ calendar = Calendar.new
19
+
20
+ calendar.add(FixWeekDay.new(3, 1, 8, 'nice holiday'))
21
+
22
+ assert_equal 'nice holiday', calendar.holiday_name(Date.new(2016, 8, 15))
23
+ end
24
+
25
+ def test_not_fix_week_day_rule
26
+ calendar = Calendar.new
27
+
28
+ date = Date.new(2016, 5, 2)
29
+
30
+ calendar.add(FixWeekDay.new(2, 1, 5))
31
+
32
+ refute calendar.holiday?(date)
33
+ end
34
+
35
+ def test_remove_fix_week_day_rule
36
+ calendar = Calendar.new
37
+
38
+ date = Date.new(2016, 11, 28)
39
+
40
+ calendar.add(FixWeekDay.new(4, 1, 11))
41
+
42
+ assert calendar.holiday?(date)
43
+
44
+ calendar.remove(FixWeekDay.new(4, 1, 11))
45
+
46
+ refute calendar.holiday?(date)
47
+ end
48
+ end
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ class HolyFridayTest < Minitest::Test
4
+ include Feriados::Rules
5
+ include Feriados
6
+
7
+ def test_holy_friday
8
+ calendar = Calendar.new
9
+
10
+ date = Date.new(2016, 3, 25)
11
+
12
+ calendar.add(HolyFriday)
13
+
14
+ assert calendar.holiday?(date)
15
+ end
16
+
17
+ def test_holy_friday_has_name
18
+ calendar = Calendar.new
19
+
20
+ date = Date.new(2016, 3, 25)
21
+
22
+ calendar.add(HolyFriday)
23
+
24
+ HolyFriday.name = 'viernes santo'
25
+
26
+ assert_equal 'viernes santo', calendar.holiday_name(date)
27
+ end
28
+
29
+ def test_not_holy_friday
30
+ calendar = Calendar.new
31
+
32
+ date = Date.new(2016, 3, 20)
33
+
34
+ calendar.add(HolyFriday)
35
+
36
+ refute calendar.holiday?(date)
37
+ end
38
+
39
+ def test_remove_holy_friday
40
+ calendar = Calendar.new
41
+
42
+ date = Date.new(2016, 3, 25)
43
+
44
+ calendar.add(HolyFriday)
45
+
46
+ assert calendar.holiday?(date)
47
+
48
+ calendar.remove(HolyFriday)
49
+
50
+ refute calendar.holiday?(date)
51
+ end
52
+ end
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ class HolyThursdayTest < Minitest::Test
4
+ include Feriados::Rules
5
+ include Feriados
6
+
7
+ def test_holy_thursday
8
+ calendar = Calendar.new
9
+
10
+ date = Date.new(2016, 3, 24)
11
+
12
+ calendar.add(HolyThursday)
13
+
14
+ assert calendar.holiday?(date)
15
+ end
16
+
17
+ def test_holy_thursday_has_name
18
+ calendar = Calendar.new
19
+
20
+ date = Date.new(2016, 3, 24)
21
+
22
+ calendar.add(HolyThursday)
23
+
24
+ HolyThursday.name = 'jueves santo'
25
+
26
+ assert_equal 'jueves santo', calendar.holiday_name(date)
27
+ end
28
+ end
@@ -0,0 +1,10 @@
1
+ ARGV.select do |argument|
2
+ case argument
3
+ when /\*/
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
@@ -1,4 +1,6 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
1
4
  require 'minitest/autorun'
5
+
2
6
  require 'feriados'
3
- require 'feriados/argentina'
4
- require 'date'
metadata CHANGED
@@ -1,16 +1,44 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: feriados
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.2
4
+ version: 4.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Rabuini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-25 00:00:00.000000000 Z
12
- dependencies: []
13
- description: Feriados argentinos. Adaptable to other countries
11
+ date: 2020-07-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: simplecov
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.17'
41
+ description: Make holidays calendars from rules
14
42
  email:
15
43
  - srabuini@gmail.com
16
44
  executables: []
@@ -18,8 +46,30 @@ extensions: []
18
46
  extra_rdoc_files: []
19
47
  files:
20
48
  - lib/feriados.rb
21
- - lib/feriados/argentina.rb
49
+ - lib/feriados/calendar.rb
50
+ - lib/feriados/loader.rb
51
+ - lib/feriados/rules.rb
52
+ - lib/feriados/rules/carnival_monday.rb
53
+ - lib/feriados/rules/carnival_tuesday.rb
54
+ - lib/feriados/rules/day_of_month.rb
55
+ - lib/feriados/rules/day_of_week.rb
56
+ - lib/feriados/rules/easter.rb
57
+ - lib/feriados/rules/fix_date.rb
58
+ - lib/feriados/rules/fix_week_day.rb
59
+ - lib/feriados/rules/function.rb
60
+ - lib/feriados/rules/holy_friday.rb
61
+ - lib/feriados/rules/holy_thursday.rb
62
+ - lib/feriados/rules/rule_base.rb
22
63
  - test/date_test.rb
64
+ - test/day_of_month_test.rb
65
+ - test/day_off_week_test.rb
66
+ - test/easter_test.rb
67
+ - test/feriados_test.rb
68
+ - test/fix_date_test.rb
69
+ - test/fix_week_day_test.rb
70
+ - test/holy_friday_test.rb
71
+ - test/holy_thursday_test.rb
72
+ - test/runner.rb
23
73
  - test/test_helper.rb
24
74
  homepage: https://github.com/srabuini/feriados
25
75
  licenses:
@@ -33,19 +83,26 @@ required_ruby_version: !ruby/object:Gem::Requirement
33
83
  requirements:
34
84
  - - ">="
35
85
  - !ruby/object:Gem::Version
36
- version: '0'
86
+ version: '2.3'
37
87
  required_rubygems_version: !ruby/object:Gem::Requirement
38
88
  requirements:
39
89
  - - ">="
40
90
  - !ruby/object:Gem::Version
41
91
  version: '0'
42
92
  requirements: []
43
- rubyforge_project:
44
- rubygems_version: 2.4.5
93
+ rubygems_version: 3.0.3
45
94
  signing_key:
46
95
  specification_version: 4
47
- summary: Feriados argentinos
96
+ summary: Holidays calendars
48
97
  test_files:
98
+ - test/holy_thursday_test.rb
99
+ - test/easter_test.rb
49
100
  - test/date_test.rb
101
+ - test/fix_week_day_test.rb
102
+ - test/fix_date_test.rb
103
+ - test/feriados_test.rb
104
+ - test/day_off_week_test.rb
105
+ - test/day_of_month_test.rb
50
106
  - test/test_helper.rb
51
- has_rdoc:
107
+ - test/holy_friday_test.rb
108
+ - test/runner.rb
@@ -1,57 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- module Feriados
4
- module Argentina
5
- Feriados.add 'Año nuevo', day: 1, month: 1
6
- Feriados.add 'Día Nacional de la Memoria por la Verdad y la Justicia',
7
- day: 24, month: 3
8
- Feriados.add 'Día del Veterano y de los Caídos en la Guerra de Malvinas',
9
- day: 2, month: 4
10
- Feriados.add 'Día del trabajador', day: 1, month: 5
11
- Feriados.add 'Día de la Revolución de Mayo', day: 25, month: 5
12
- Feriados.add 'Paso a la Inmortalidad del General Manuel Belgrano',
13
- day: 20, month: 6
14
- Feriados.add 'Día de la Independencia', day: 9, month: 7
15
- Feriados.add 'Inmaculada Concepción de María', day: 8, month: 12
16
- Feriados.add 'Navidad', day: 25, month: 12
17
-
18
- Feriados.add 'easter', function: -> (year) { Feriados.easter(year) }
19
- Feriados.add 'Viernes Santo',
20
- function: -> (year) { Feriados.easter(year) - 2 }
21
- Feriados.add 'Jueves Santo',
22
- function: -> (year) { Feriados.easter(year) - 3 }
23
- Feriados.add 'Lunes de Carnaval',
24
- function: -> (year) { Feriados.easter(year) - 48 }
25
- Feriados.add 'Martes de Carnaval',
26
- function: -> (year) { Feriados.easter(year) - 47 }
27
-
28
- Feriados.add 'Paso a la Inmortalidad del General José de San Martín',
29
- day: 17, month: 8, fix_change: [:third, :monday]
30
- Feriados.add 'Día del Respeto a la Diversidad Cultural',
31
- day: 12, month: 10, fix_change: [:second, :monday]
32
- Feriados.add 'Día de la Soberanía Nacional',
33
- day: 20, month: 11, fix_change: [:fourth, :monday]
34
-
35
- Feriados.add 'Día del Bicentenario de la Creación y Primera Jura de la'\
36
- 'Bandera Argentina', day: 27, month: 2, year: 2012
37
- Feriados.add 'Día del Bicentenario de la Batalla de Tucumán',
38
- day: 24, month: 9, year: 2012
39
- Feriados.add 'Feriado puente', day: 30, month: 4, year: 2012
40
- Feriados.add 'Feriado puente', day: 24, month: 12, year: 2012
41
-
42
- Feriados.add 'Bicentenario de la Asamblea General Constituyente de 1813',
43
- day: 31, month: 1, year: 2013
44
- Feriados.add 'Día de la Batalla de Salta', day: 20, month: 2, year: 2013
45
- Feriados.add 'Feriado puente', day: 1, month: 4, year: 2013
46
- Feriados.add 'Feriado puente', day: 21, month: 6, year: 2013
47
-
48
- Feriados.add 'Feriado puente', day: 2, month: 5, year: 2014
49
- Feriados.add 'Feriado puente', day: 26, month: 12, year: 2014
50
-
51
- Feriados.add 'Feriado puente', day: 23, month: 3, year: 2015
52
- Feriados.add 'Feriado puente', day: 7, month: 12, year: 2015
53
-
54
- Feriados.add 'Feriado puente', day: 8, month: 7, year: 2016
55
- Feriados.add 'Feriado puente', day: 9, month: 12, year: 2016
56
- end
57
- end