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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5ad2e90243c135cdf7a6f7435370a29813ff9f49
4
- data.tar.gz: b3f3cfb88d70229f837ccffc1d08b21bcd3e7b8f
3
+ metadata.gz: 4f0bfdf5aafc3c2e22fb7f29ee4cf93bb2d98a94
4
+ data.tar.gz: 147439eed93a41cf7829b12041a3858c02822612
5
5
  SHA512:
6
- metadata.gz: 66865b24e8e13099f91d1d3044ceba82bcdc4a7d091bdcecfdf4edb28decb852db802ee05e0347b9be29f76f60867feb7f3f1bca53e8b5a0e88d5f726942c5df
7
- data.tar.gz: 721b82d582cfd2f9f71197ba4422658805cd9a3c700a6e2bc8ec0830959bea603fcd3d1bc4e9ad82bbbf2d622a135eeffb0312459fa13a27b65283186d35c5c4
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
- Calendários disponíveis london, new_york e brazil
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
 
@@ -0,0 +1,15 @@
1
+ {
2
+ "brazil_holidays.json": [
3
+ "brasil",
4
+ "brazil",
5
+ "anbima"
6
+ ],
7
+ "london_holidays.json": [
8
+ "london",
9
+ "londres"
10
+ ],
11
+ "new_york_holidays.json": [
12
+ "new_york",
13
+ "nova_iorque"
14
+ ]
15
+ }
data/lib/holiday.rb CHANGED
@@ -4,38 +4,61 @@ require 'json'
4
4
  require 'singleton'
5
5
 
6
6
  class Holiday
7
- attr_reader :holidays, :calendars
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
- @holidays = {}
22
+ # TODO: remove this in a major version
23
+ holidays
24
+ calendars
25
+ end
26
+
27
+ private
15
28
 
16
- calendars.each do |calendar|
17
- file = File.expand_path(
18
- File.dirname(__FILE__) + "/../data/#{calendar}_holidays.json"
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 in(year)
27
- load_data if holidays.nil?
28
- holidays[year]
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
- private
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 update_holidays(parsed)
34
- parsed.map do |str|
35
- str.split('-')
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class WorkDay
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
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.1.0
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-08-30 00:00:00.000000000 Z
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