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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/business_period/base.rb +21 -1
- data/lib/business_period/days.rb +18 -46
- data/lib/business_period/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a4a2e8d7ba8e5c08ec71d3ed28455df5278c5c6
|
4
|
+
data.tar.gz: 0dad9bb70a00e1d27c3048d07e04f1b44ce3bcb7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 651756d9806ea5016008c463741024e0918022f10067918400410825b25209540c57fdad31efd9c1ec41e286ea3bd0491a6ca7b93f0ae5272f7c94a0488046b5
|
7
|
+
data.tar.gz: 5e15457ff1f28e3d59bcecf3052cc444b03cceb4a789e29060ee264cd43294e8c70f5c29c9fe2226d94e8cb9a8b9fd54d47cc5c76af91f18e935fb86d357e029
|
data/Gemfile.lock
CHANGED
data/lib/business_period/base.rb
CHANGED
@@ -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 ||=
|
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
|
data/lib/business_period/days.rb
CHANGED
@@ -7,69 +7,41 @@ module BusinessPeriod
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def perform(from_date, to_date, options)
|
10
|
-
|
11
|
-
@to_date = to_date
|
12
|
-
@options = options
|
10
|
+
return [] unless valid_params(from_date, to_date, options)
|
13
11
|
|
14
|
-
|
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[
|
22
|
-
to_date: result[
|
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
|
-
|
31
|
-
|
32
|
-
(
|
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
|
40
|
-
|
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
|
46
|
-
|
47
|
-
next unless holidays[day[:day].month]
|
34
|
+
def primary_day_present?(options)
|
35
|
+
return unless options.is_a?(Hash)
|
48
36
|
|
49
|
-
|
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
|
-
|
67
|
-
|
68
|
-
|
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
|
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
|
+
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:
|
11
|
+
date: 2019-01-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|