work_day 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +5 -2
- data/data/calendars.json +15 -0
- data/lib/holiday.rb +44 -21
- data/lib/work_day/version.rb +1 -1
- data/lib/work_day.rb +2 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f0bfdf5aafc3c2e22fb7f29ee4cf93bb2d98a94
|
4
|
+
data.tar.gz: 147439eed93a41cf7829b12041a3858c02822612
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ff93b66391aa8f3e3980bab03bb4c7be1723e28da1d38934d5073e61b1d8564f08f5522b2d6126e0d4f51fae1c3497c4b1f8ff12c019ecbfbef0ed6e85af0eb
|
7
|
+
data.tar.gz: cbb9ac72f46f0100df91c9ddb09b4d6a7308d3a018aaa3aac86dd6c09b5a3097741a101b02c624a569bbd71797d1c5f9a8b4d8db1b981049918038e3954e6305
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [1.2.0] - 2018-09-10
|
10
|
+
### Added
|
11
|
+
- Feature to use the same calendar with different names
|
12
|
+
|
9
13
|
## [1.1.0] - 2018-08-29
|
10
14
|
### Added
|
11
15
|
- Feature to use diferent calendars in a block
|
data/README.md
CHANGED
@@ -108,8 +108,11 @@ Thu, 10 Jan 2013]
|
|
108
108
|
|
109
109
|
### Escolhendo calendários
|
110
110
|
|
111
|
-
Pode ser utilizado mais de um calendário ao mesmo tempo
|
112
|
-
|
111
|
+
Pode ser utilizado mais de um calendário ao mesmo tempo, através de qualquer um dos nomes disponíveis:
|
112
|
+
|
113
|
+
- `london`: `london`, `londres`
|
114
|
+
- `new_york`: `new_york`, `nova_iorque`
|
115
|
+
- `brazil`: `brazil`, `brasil`, `anbima`
|
113
116
|
|
114
117
|
###### Configurando os calendários default
|
115
118
|
|
data/data/calendars.json
ADDED
data/lib/holiday.rb
CHANGED
@@ -4,38 +4,61 @@ require 'json'
|
|
4
4
|
require 'singleton'
|
5
5
|
|
6
6
|
class Holiday
|
7
|
-
attr_reader :
|
7
|
+
attr_reader :calendars
|
8
|
+
|
9
|
+
CALENDARS_FILENAME = File.expand_path(
|
10
|
+
"#{File.dirname(__FILE__)}/../data/calendars.json"
|
11
|
+
)
|
8
12
|
|
9
13
|
def initialize(calendars)
|
10
14
|
@calendars = calendars
|
11
15
|
end
|
12
16
|
|
17
|
+
def in(year)
|
18
|
+
holidays[year]
|
19
|
+
end
|
20
|
+
|
13
21
|
def load_data
|
14
|
-
|
22
|
+
# TODO: remove this in a major version
|
23
|
+
holidays
|
24
|
+
calendars
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
15
28
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
)
|
20
|
-
raw_data = File.read(file)
|
21
|
-
parsed = JSON.parse(raw_data)
|
22
|
-
update_holidays(parsed)
|
23
|
-
end
|
29
|
+
def holidays
|
30
|
+
@holidays ||= calendars.flat_map(&method(:to_holidays)).uniq.
|
31
|
+
reduce({}, &method(:by_year))
|
24
32
|
end
|
25
33
|
|
26
|
-
def
|
27
|
-
|
28
|
-
|
34
|
+
def by_year(holidays_by_year, date)
|
35
|
+
holidays_by_year ||= {}
|
36
|
+
holidays_by_year[date.year] ||= []
|
37
|
+
holidays_by_year[date.year] << date
|
38
|
+
holidays_by_year
|
29
39
|
end
|
30
40
|
|
31
|
-
|
41
|
+
def to_holidays(calendar)
|
42
|
+
holiday_file(calendar).map(&:to_date)
|
43
|
+
end
|
44
|
+
|
45
|
+
def holiday_file(calendar)
|
46
|
+
filename = holiday_filename(calendar)
|
47
|
+
|
48
|
+
return if filename.nil?
|
49
|
+
|
50
|
+
path = File.dirname(__FILE__) + "/../data/#{filename}"
|
51
|
+
JSON.parse(File.read(File.expand_path(path)))
|
52
|
+
end
|
53
|
+
|
54
|
+
def holiday_filename(calendar)
|
55
|
+
holiday_files.select do |_filename, calendar_names|
|
56
|
+
calendar_names.include?(calendar)
|
57
|
+
end.keys.first
|
58
|
+
end
|
32
59
|
|
33
|
-
def
|
34
|
-
|
35
|
-
|
36
|
-
date = Date.parse(str)
|
37
|
-
@holidays[date.year] ||= []
|
38
|
-
@holidays[date.year] << date
|
39
|
-
end
|
60
|
+
def holiday_files
|
61
|
+
@holiday_files ||= JSON.parse(File.read(CALENDARS_FILENAME))
|
62
|
+
@holiday_files
|
40
63
|
end
|
41
64
|
end
|
data/lib/work_day/version.rb
CHANGED
data/lib/work_day.rb
CHANGED
@@ -18,12 +18,14 @@ class WorkDay
|
|
18
18
|
def work_day?(date)
|
19
19
|
date = date.try(:to_date)
|
20
20
|
return unless date
|
21
|
+
|
21
22
|
![0, 6].include?(date.wday) && !holidays_in(date.year).include?(date)
|
22
23
|
end
|
23
24
|
|
24
25
|
def last_until(date, days = 0)
|
25
26
|
date = date.try(:to_date)
|
26
27
|
return unless date
|
28
|
+
|
27
29
|
date -= 1 until work_day?(date)
|
28
30
|
days -= 1
|
29
31
|
days == -1 ? date : last_until(1.day.ago(date), days)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: work_day
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adriano Bacha
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -70,6 +70,7 @@ files:
|
|
70
70
|
- README.md
|
71
71
|
- bitbucket-pipelines.yml
|
72
72
|
- data/brazil_holidays.json
|
73
|
+
- data/calendars.json
|
73
74
|
- data/london_holidays.json
|
74
75
|
- data/new_york_holidays.json
|
75
76
|
- lib/holiday.rb
|