business-period 0.1.4 → 0.1.5

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: ae8d77f79fbae52d0e98e2eb490f869dc939b6a0
4
- data.tar.gz: 77584003f1eb7564bb403e0140adb011af1fff79
3
+ metadata.gz: 9a4a2e8d7ba8e5c08ec71d3ed28455df5278c5c6
4
+ data.tar.gz: 0dad9bb70a00e1d27c3048d07e04f1b44ce3bcb7
5
5
  SHA512:
6
- metadata.gz: f42cb7a31c9c18bb6797609d8c8b3d49c3cbe484e8bf2758ec580812d09f2ce3353ad14e057ab530437fdef9915883f3b5c89f01f8f1448496313f4fc4708c4a
7
- data.tar.gz: b78380a4f745c7ecf8353d59db0beca3bbf8c3660f569cb2f44a1588a86b6309534bf1edfe9d1171c31490bcb6d5ee79646c7c648581040ed5d8b19002d694f3
6
+ metadata.gz: 651756d9806ea5016008c463741024e0918022f10067918400410825b25209540c57fdad31efd9c1ec41e286ea3bd0491a6ca7b93f0ae5272f7c94a0488046b5
7
+ data.tar.gz: 5e15457ff1f28e3d59bcecf3052cc444b03cceb4a789e29060ee264cd43294e8c70f5c29c9fe2226d94e8cb9a8b9fd54d47cc5c76af91f18e935fb86d357e029
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- business-period (0.1.4)
4
+ business-period (0.1.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -2,6 +2,8 @@
2
2
 
3
3
  module BusinessPeriod
4
4
  class Base
5
+ SEPARATOR = '_'
6
+
5
7
  def config
6
8
  @config ||= Config
7
9
  end
@@ -15,11 +17,29 @@ module BusinessPeriod
15
17
  end
16
18
 
17
19
  def holidays
18
- @holidays ||= YAML.load_file(File.join(holiday_config)).fetch('months')
20
+ @holidays ||= begin
21
+ holidays_config.each_with_object([]) do |row, container|
22
+ container << row.last.map do |days|
23
+ holiday_month_with_day(row.first, days['mday'])
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ def holiday_month_with_day(month, day)
30
+ [month, day].join(SEPARATOR)
31
+ end
32
+
33
+ def day_is_holiday?(day)
34
+ holidays.flatten.include? holiday_month_with_day(day.month, day.day)
19
35
  end
20
36
 
21
37
  private
22
38
 
39
+ def holidays_config
40
+ @holidays_config ||= YAML.load_file(File.join(holiday_config)).fetch('months')
41
+ end
42
+
23
43
  def holiday_config
24
44
  [File.dirname(__FILE__), "../../config/holidays/#{config.locale}.yml"]
25
45
  end
@@ -7,69 +7,41 @@ module BusinessPeriod
7
7
  end
8
8
 
9
9
  def perform(from_date, to_date, options)
10
- @from_date = from_date
11
- @to_date = to_date
12
- @options = options
10
+ return [] unless valid_params(from_date, to_date, options)
13
11
 
14
- return [] unless valid_params
15
-
16
- period = calculate_period(@to_date, @options)
17
- days = business_days(period)
18
- result = extract_holidays(days).compact
12
+ period = calculate_period(to_date, options)
13
+ result = business_days(period)
19
14
 
20
15
  {
21
- from_date: result[@from_date][:day],
22
- to_date: result[@to_date][:day]
16
+ from_date: result[from_date],
17
+ to_date: result[to_date]
23
18
  }
24
19
  end
25
20
 
26
21
  private
27
22
 
28
- def valid_params
29
- valid_options &&
30
- @from_date.is_a?(Integer) && @to_date.is_a?(Integer) &&
31
- @from_date >= 0 && @to_date >= 0 &&
32
- (@from_date <= @to_date)
33
- end
34
-
35
- def valid_options
36
- @options ? primary_day_present? : true
23
+ def valid_params(from_date, to_date, options)
24
+ valid_options(options) &&
25
+ from_date.is_a?(Integer) && to_date.is_a?(Integer) &&
26
+ from_date >= 0 && to_date >= 0 &&
27
+ (from_date <= to_date)
37
28
  end
38
29
 
39
- def primary_day_present?
40
- return unless @options.is_a?(Hash)
41
-
42
- @options[:primary_day] ? (@options[:primary_day].methods.include? :strftime) : true
30
+ def valid_options(options)
31
+ options ? primary_day_present?(options) : true
43
32
  end
44
33
 
45
- def extract_holidays(days)
46
- days.map.with_index do |day, idx|
47
- next unless holidays[day[:day].month]
34
+ def primary_day_present?(options)
35
+ return unless options.is_a?(Hash)
48
36
 
49
- day unless first_day_is_holiday?(idx, day)
50
- end
51
- end
52
-
53
- def first_day_is_holiday?(idx, day)
54
- include_day = holidays[day[:day].month].map { |d| d['mday'] }
55
-
56
- if idx.zero? && (include_day.include? day[:day].day)
57
- @from_date -= 1
58
- @to_date -= 1
59
- end
60
-
61
- (include_day.include? day[:day].day)
37
+ options[:primary_day] ? (options[:primary_day].methods.include? :strftime) : true
62
38
  end
63
39
 
64
40
  def business_days(period)
65
41
  period.each_with_object([]) do |day, container|
66
- next unless config.work_days.include?(day.wday)
67
-
68
- container <<
69
- {
70
- day: day,
71
- month: day.to_time.month
72
- }
42
+ if config.work_days.include?(day.wday)
43
+ container << day unless day_is_holiday?(day)
44
+ end
73
45
  end
74
46
  end
75
47
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BusinessPeriod
4
- VERSION = '0.1.4'
4
+ VERSION = '0.1.5'
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.4
4
+ version: 0.1.5
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-21 00:00:00.000000000 Z
11
+ date: 2019-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec