business-period 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7b20b17a7d1f22b7fb7d67ad3c7ddd5097dfb057
4
- data.tar.gz: f6d2e2bcfc457df2b7baa7186c4e73aa3384c2d4
3
+ metadata.gz: 0cc52e3e82cb1ac554f12bed26fd66fe4d8e7b9d
4
+ data.tar.gz: e9fe9d837ccf0d0fa83cb793ab401e35f42d5d86
5
5
  SHA512:
6
- metadata.gz: '02597830fa7f05d03c55b4549ea54f696e182f35c7239cba6322ba442e405f803870529c08a76421ce9e601156c37eb6443899d8aa0c2470d38746a5c82ded0e'
7
- data.tar.gz: e1283ffe9ef0f311e535f54b6d9794a5e8aa9125263b9d6eb2ab5c09f60283090a094f5b7838f857bd7f2de487214f7a4d95b2edadae8229a0d72a204e726923
6
+ metadata.gz: 21682102dc8ea3fb1b42b6e283b6c124eb63939d5d673c27000a55369fe5e6774fdb8a0002b629345f26b610bd23168fdc7a466c9b6c36c23a414f455952396a
7
+ data.tar.gz: b0a9d0ab3b195e01e313293aa62c4defe9d79bcd111d4a8680be59f96f2509e8e247ea25801b1c234a52a06c1f26d992f0cb12de095bf38dc574a9b3fff1e9b4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- business-period (0.1.0)
4
+ business-period (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -0,0 +1,30 @@
1
+ months:
2
+ 1:
3
+ - mday: 1
4
+ name: New Year's Day
5
+ 2:
6
+ - mday: 24
7
+ name: Independence Day
8
+ 4:
9
+ - mday: 19
10
+ name: Good Friday
11
+ - mday: 21
12
+ name: Easter Sunday
13
+ 5:
14
+ - mday: 1
15
+ name: Spring Day
16
+ 6:
17
+ - mday: 23
18
+ name: Victory Day
19
+ - mday: 24
20
+ name: St. John's Day
21
+ 8:
22
+ - mday: 20
23
+ name: Independence Restoration Day
24
+ 12:
25
+ - mday: 24
26
+ name: Christmas Eve
27
+ - mday: 25
28
+ name: Christmas Day
29
+ - mday: 26
30
+ name: Boxing Day
@@ -0,0 +1,31 @@
1
+ months:
2
+ 1:
3
+ - mday: 1
4
+ name: New Year's Day
5
+ 4:
6
+ - mday: 19
7
+ name: Good Friday
8
+ - mday: 22
9
+ name: Easter Monday
10
+ 5:
11
+ - mday: 1
12
+ name: Labour Day
13
+ - mday: 6
14
+ name: Declaration of Independence Day
15
+ 6:
16
+ - mday: 23
17
+ name: Midsummer Eve
18
+ - mday: 24
19
+ name: St. John's Day
20
+ 11:
21
+ - mday: 18
22
+ name: Latvian National Day
23
+ 12:
24
+ - mday: 24
25
+ name: Christmas Eve
26
+ - mday: 25
27
+ name: Christmas Day
28
+ - mday: 26
29
+ name: Second day of Christmas
30
+ - mday: 31
31
+ name: New Year's Eve
@@ -7,7 +7,7 @@ module BusinessPeriod
7
7
  end
8
8
 
9
9
  def calculate_period(to_date)
10
- magic_number = 10 - config.work_days.size
10
+ magic_number = 15 - config.work_days.size
11
11
  days_to_add = days_to_seconds(to_date * magic_number)
12
12
  finish = Time.now + days_to_add
13
13
  Time.now.to_date..finish.to_date
@@ -7,24 +7,45 @@ module BusinessPeriod
7
7
  end
8
8
 
9
9
  def perform(from_date, to_date)
10
- return {} unless valid_params(from_date, to_date)
10
+ @from_date = from_date
11
+ @to_date = to_date
11
12
 
12
- period = calculate_period(to_date)
13
- days = business_days(period)
13
+ return {} unless valid_params
14
14
 
15
- check_holidays(days)
15
+ period = calculate_period(@to_date)
16
+ days = business_days(period)
17
+ result = extract_holidays(days).compact
16
18
 
17
19
  {
18
- from_date: days[from_date][:day],
19
- to_date: days[to_date][:day]
20
+ from_date: result[@from_date][:day],
21
+ to_date: result[@to_date][:day]
20
22
  }
21
23
  end
22
24
 
23
25
  private
24
26
 
25
- def valid_params(from_date, to_date)
26
- from_date.is_a?(Integer) && to_date.is_a?(Integer) &&
27
- from_date >= 0 && to_date >= 0
27
+ def valid_params
28
+ @from_date.is_a?(Integer) && @to_date.is_a?(Integer) &&
29
+ @from_date >= 0 && @to_date >= 0
30
+ end
31
+
32
+ def extract_holidays(days)
33
+ days.map.with_index do |day, idx|
34
+ next unless holidays[day[:day].month]
35
+
36
+ day unless first_day_is_holiday?(idx, day)
37
+ end
38
+ end
39
+
40
+ def first_day_is_holiday?(idx, day)
41
+ include_day = holidays[day[:day].month].map { |d| d['mday'] }
42
+
43
+ if idx.zero? && (include_day.include? day[:day].day)
44
+ @from_date -= 1
45
+ @to_date -= 1
46
+ end
47
+
48
+ (include_day.include? day[:day].day)
28
49
  end
29
50
 
30
51
  def business_days(period)
@@ -38,19 +59,5 @@ module BusinessPeriod
38
59
  }
39
60
  end
40
61
  end
41
-
42
- def check_holidays(business_days)
43
- business_days.each_with_index do |day, index|
44
- next unless holidays[day[:month]]
45
-
46
- extract_holidays(business_days, day, index)
47
- end
48
- end
49
-
50
- def extract_holidays(business_days, day, index)
51
- holidays[day[:month]].each do |holiday|
52
- business_days.delete_at(index) if holiday['mday'] == day[:day].mday
53
- end
54
- end
55
62
  end
56
63
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BusinessPeriod
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: business-period
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - matas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-10 00:00:00.000000000 Z
11
+ date: 2018-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -42,7 +42,9 @@ files:
42
42
  - README.md
43
43
  - Rakefile
44
44
  - business-period.gemspec
45
+ - config/holidays/et.yml
45
46
  - config/holidays/lt.yml
47
+ - config/holidays/lv.yml
46
48
  - lib/business-period.rb
47
49
  - lib/business_period/base.rb
48
50
  - lib/business_period/config.rb