business-period 0.0.8 → 0.0.9
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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/business_period/base.rb +5 -29
- data/lib/business_period/days.rb +26 -25
- 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: b90def6c2e0624d299b2a28109bbf013fe61f05f
|
|
4
|
+
data.tar.gz: bdce177f079801e02ad7767fe51c6be716ede2e3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '099bede4e8653320424c59b246a02483179f19700c642a832917d2bfa2f04d983ae298dababd54487f56b2afa95c27fe607e99bc43ad174f323bf83ee7cd9086'
|
|
7
|
+
data.tar.gz: 6c539ee970451a3ca75cf37f8d8835b0d6cdc6882aad61c0a171ecb0bc579b5d26787daa1add86a4da4959b89c9d496acb388947cfe4089e8c47bcf723303e6b
|
data/Gemfile.lock
CHANGED
data/lib/business_period/base.rb
CHANGED
|
@@ -6,43 +6,19 @@ module BusinessPeriod
|
|
|
6
6
|
@config ||= Config
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
# converts days to seconds
|
|
15
|
-
days_to_add = days_to_seconds(imaginable_last_day)
|
|
16
|
-
|
|
17
|
-
# Adds expected last day to current day
|
|
18
|
-
finish = Time.now + days_to_add
|
|
19
|
-
|
|
20
|
-
# Builds an array from current time to imaginable end time
|
|
21
|
-
Time.now.to_date..finish.to_date
|
|
22
|
-
end
|
|
9
|
+
def calculate_period(to_date)
|
|
10
|
+
magic_number = 10 - config.work_days.size
|
|
11
|
+
days_to_add = days_to_seconds(to_date * magic_number)
|
|
12
|
+
finish = Time.now + days_to_add
|
|
13
|
+
Time.now.to_date..finish.to_date
|
|
23
14
|
end
|
|
24
15
|
|
|
25
|
-
# Sets holiday instance by given file location
|
|
26
16
|
def holidays
|
|
27
17
|
@holidays ||= YAML.load_file(File.join(holiday_config)).fetch('months')
|
|
28
18
|
end
|
|
29
19
|
|
|
30
|
-
# Dynamically calculates how many days we have add to `to_date` end
|
|
31
|
-
def calculate_days_to_add_to(from_date, to_date)
|
|
32
|
-
# Calculates subjective number
|
|
33
|
-
sum = (7 - config.work_days.size)
|
|
34
|
-
|
|
35
|
-
# sum cannot be zero.
|
|
36
|
-
# Relevant when when work_days array == [1, 2, 3, 4, 5, 6, 7]
|
|
37
|
-
sum = 1 if sum.zero?
|
|
38
|
-
|
|
39
|
-
# Some magic
|
|
40
|
-
(from_date + to_date) * sum + 7
|
|
41
|
-
end
|
|
42
|
-
|
|
43
20
|
private
|
|
44
21
|
|
|
45
|
-
# Gets path to config file by given locale
|
|
46
22
|
def holiday_config
|
|
47
23
|
[File.dirname(__FILE__), "../../config/holidays/#{config.locale}.yml"]
|
|
48
24
|
end
|
data/lib/business_period/days.rb
CHANGED
|
@@ -7,48 +7,49 @@ module BusinessPeriod
|
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
def perform(from_date, to_date)
|
|
10
|
-
return {}
|
|
11
|
-
return {} if from_date > to_date
|
|
10
|
+
return {} unless valid_params(from_date, to_date)
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
period = calculate_period(to_date)
|
|
13
|
+
days = business_days(period)
|
|
15
14
|
|
|
16
|
-
check_holidays
|
|
15
|
+
check_holidays(days)
|
|
17
16
|
|
|
18
|
-
# Selects business days from given from_date and to_date values.
|
|
19
|
-
# E.g from_date = 2, to_date = 4
|
|
20
|
-
# selects second and fourth elements
|
|
21
17
|
{
|
|
22
|
-
from_date:
|
|
23
|
-
to_date:
|
|
18
|
+
from_date: days[from_date][:day],
|
|
19
|
+
to_date: days[to_date][:day]
|
|
24
20
|
}
|
|
25
21
|
end
|
|
26
22
|
|
|
27
23
|
private
|
|
28
24
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
# checks if day is business day
|
|
33
|
-
if config.work_days.include?(day.wday)
|
|
34
|
-
{ day: day, month: day.to_time.month }
|
|
35
|
-
end
|
|
36
|
-
end.compact
|
|
25
|
+
def valid_params(from_date, to_date)
|
|
26
|
+
from_date.is_a?(Integer) && to_date.is_a?(Integer) &&
|
|
27
|
+
from_date < to_date
|
|
37
28
|
end
|
|
38
29
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
30
|
+
def business_days(period)
|
|
31
|
+
period.each_with_object([]) do |day, container|
|
|
32
|
+
next unless config.work_days.include?(day.wday)
|
|
33
|
+
|
|
34
|
+
container <<
|
|
35
|
+
{
|
|
36
|
+
day: day,
|
|
37
|
+
month: day.to_time.month
|
|
38
|
+
}
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def check_holidays(business_days)
|
|
43
|
+
business_days.each_with_index do |day, index|
|
|
43
44
|
next unless holidays[day[:month]]
|
|
44
45
|
|
|
45
|
-
extract_holidays(day, index)
|
|
46
|
+
extract_holidays(business_days, day, index)
|
|
46
47
|
end
|
|
47
48
|
end
|
|
48
49
|
|
|
49
|
-
def extract_holidays(day, index)
|
|
50
|
+
def extract_holidays(business_days, day, index)
|
|
50
51
|
holidays[day[:month]].each do |holiday|
|
|
51
|
-
|
|
52
|
+
business_days.delete_at(index) if holiday['mday'] == day[:day].mday
|
|
52
53
|
end
|
|
53
54
|
end
|
|
54
55
|
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.0.
|
|
4
|
+
version: 0.0.9
|
|
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-
|
|
11
|
+
date: 2018-12-06 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rspec
|