feriados 4.0.0 → 4.4.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: 238748dcfdbda85030648b930c354f509349e22a
4
- data.tar.gz: e5c7ba38d9aea3a7bfdbb057d9e992982ac0e1ae
2
+ SHA256:
3
+ metadata.gz: 7da9b7e3f668080aae4808490a75277a23b6b7ec3855a40101c3b9543aa56ea8
4
+ data.tar.gz: 149e631c50d584aba12bb97e36a0d3164ef933a31a704514644ea2f25578a98c
5
5
  SHA512:
6
- metadata.gz: fab654d36cfc9e4522d45afb3ca5e656c8db64831a1c7522ed71605f2d16d7a75aa49fc864ff42aaa5e9509c36d807c183a88142ce97bdfeed0f062778757c9c
7
- data.tar.gz: 4ee6370b10f3a098a7cb783bb4ece0d139f21a5532ff3fd15d7f706a2ae7ed2dd67ac45251d314b826a4009f396e51b96beef756a5b33837cdb94541ef995e0c
6
+ metadata.gz: 53f7a18b300b37dec9bc811157931ab13738cbf0b33144edfde675b881c4f496799c34ec160011d71dc274f4adba7645b0c98779e24b6bc5839eda15f853052a
7
+ data.tar.gz: 1a457ff1da1371d206ab34e2c49164d8035967c220e3d68d7b0b507be71988ba5ac20c2bf7fcce60faff71dcb055c35d28e60036c98414aea88b10a517b5949e
@@ -1,3 +1,5 @@
1
- require_relative './feriados/calendar'
1
+ require 'yaml'
2
+
2
3
  require_relative './feriados/rules'
3
- require_relative './feriados/argentina'
4
+ require_relative './feriados/calendar'
5
+ require_relative './feriados/loader'
@@ -8,22 +8,48 @@ module Feriados
8
8
  end
9
9
 
10
10
  def holiday?(date)
11
- @rules.any? { |rule| rule.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
12
16
  end
13
17
 
14
18
  def add(rule)
15
- @rules << rule
19
+ rules << rule
16
20
  end
17
21
 
18
22
  def remove(rule)
19
- @rules.delete(rule)
23
+ rules.delete(rule)
24
+ end
25
+
26
+ def load(rules)
27
+ Loader.new(rules, self).load
20
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
21
41
  end
22
42
 
23
43
  refine Date do
44
+ @calendar = nil
45
+
24
46
  def holiday?
25
47
  @@calendar.holiday?(self)
26
48
  end
49
+
50
+ def holiday_name
51
+ @@calendar.holiday_name(self)
52
+ end
27
53
  end
28
54
 
29
55
  refine Date.singleton_class do
@@ -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
@@ -1,142 +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
+
1
16
  module Feriados
2
17
  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
18
+ class Rule
19
+ extend Forwardable
42
20
 
43
- protected
21
+ def_delegators :@rule, :day, :month, :year, :week
44
22
 
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
23
+ attr_reader :rule
56
24
 
57
- def holiday?(date)
58
- @year == date.year && @month == date.month && @day = date.day
25
+ def initialize(rule)
26
+ @rule = OpenStruct.new(rule)
59
27
  end
60
28
 
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
29
+ def week_day?
30
+ rule.day && rule.month && rule.week
71
31
  end
72
32
 
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)
33
+ def fix_date?
34
+ rule.day && rule.month && rule.year
90
35
  end
91
36
 
92
- def self.holiday?(date)
93
- easter = Easter.new(date.year)
94
-
95
- easter.date == date
37
+ def day_of_month?
38
+ rule.day && rule.month && !fix_date? && !week_day?
96
39
  end
97
- end
98
40
 
99
- class HolyFriday
100
- def self.holiday?(date)
101
- Easter.new(date.year).date - 2 == date
102
- end
103
- end
41
+ def function?
42
+ functions = %i[easter holy_thursday holy_friday carnival_monday
43
+ carnival_tuesday]
104
44
 
105
- class HolyThursday
106
- def self.holiday?(date)
107
- Easter.new(date.year).date - 3 == date
45
+ functions.map { |e| rule.respond_to?(e) }.any?
108
46
  end
109
- end
110
47
 
111
- class CarnivalMonday
112
- def self.holiday?(date)
113
- Easter.new(date.year).date - 48 == date
48
+ def name
49
+ rule[:name] || rule.to_h.values.first
114
50
  end
115
- end
116
51
 
117
- class CarnivalTuesday
118
- def self.holiday?(date)
119
- Easter.new(date.year).date - 47 == date
52
+ def function_name
53
+ rule.to_h.keys.first.to_s.split('_').collect(&:capitalize).join
120
54
  end
121
55
  end
122
56
 
123
- class FixWeekDay < RuleBase
124
- def initialize(week, wday, month)
125
- @week = week
126
- @wday = wday
127
- @month = month
128
- end
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?
129
62
 
130
- def holiday?(date)
131
- week = (date.day - 1) / 7 + 1
132
- @wday == date.wday && @month == date.month && @week == week
133
- end
63
+ raise ArgumentError, "Argument #{rule} is not a valid rule" unless rule.function?
134
64
 
135
- protected
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
136
69
 
137
- def state
138
- [@week, @wday, @month]
139
- end
70
+ klass
140
71
  end
141
72
  end
142
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