business-period 0.0.5 → 0.0.6
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/.codeclimate.yml +2 -0
- data/README.md +14 -15
- data/lib/business_period/base.rb +5 -6
- data/lib/business_period/days.rb +11 -7
- 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: 6910f7cb08ae02406dab9a48a5be8a9fa13fa4db
|
|
4
|
+
data.tar.gz: f29b0758ddc9d9f4736a8b3fa1c0fb1ce44e07b9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6a1ad16665a792b9cba88b700b2486cc162feb1b846246ce0cfa28fbed780ec6aaccfcbd0f4fcfa70268ed3cb651c7c98a13009c65d1eeeecbef654db4f9977e
|
|
7
|
+
data.tar.gz: 0b4cce9bd863d542a032c7be80e28e7cc5a8393f2eeab27c8e2072d0e12d9e4099e188ee54df394bdbe2e79c235e5374c4914f9a0ceeefca8a88d2aed563b11d
|
data/.codeclimate.yml
CHANGED
data/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
[](https://badge.fury.io/rb/business-period)
|
|
4
4
|
# Business period
|
|
5
5
|
|
|
6
|
-
**BusinessPeriod** is a ruby library that calculates business period by given
|
|
6
|
+
**BusinessPeriod** is a ruby library that calculates business period by given hash.
|
|
7
7
|
This library was designed for lithuanian, latvian and estonian unemployment days.
|
|
8
8
|
|
|
9
9
|
## Installation
|
|
@@ -26,7 +26,7 @@ Or install it yourself as:
|
|
|
26
26
|
|
|
27
27
|
There are two ways to initialize:
|
|
28
28
|
|
|
29
|
-
#####
|
|
29
|
+
##### Default initialization:
|
|
30
30
|
|
|
31
31
|
```ruby
|
|
32
32
|
# config/initializers/business_period.rb
|
|
@@ -37,19 +37,18 @@ There are two ways to initialize:
|
|
|
37
37
|
locale = 'lt'
|
|
38
38
|
work_days = [1, 2, 3, 4, 5]
|
|
39
39
|
|
|
40
|
-
BusinessPeriod.
|
|
41
|
-
|
|
42
|
-
config.work_days = work_days
|
|
43
|
-
end
|
|
40
|
+
BusinessPeriod::Config.locale = locale
|
|
41
|
+
BusinessPeriod::Config.work_days = work_days
|
|
44
42
|
```
|
|
45
43
|
|
|
46
|
-
#####
|
|
44
|
+
##### Initialization with Proc:
|
|
47
45
|
|
|
48
46
|
```ruby
|
|
49
47
|
locale = 'lt'
|
|
50
48
|
work_days = [1, 2, 3, 4, 5]
|
|
51
49
|
|
|
52
|
-
BusinessPeriod::
|
|
50
|
+
BusinessPeriod::Config.locale = -> { locale }
|
|
51
|
+
BusinessPeriod::Config.work_days = -> { work_days }
|
|
53
52
|
```
|
|
54
53
|
|
|
55
54
|
## How it works
|
|
@@ -62,9 +61,8 @@ BusinessPeriod::Days.new(locale, work_days)
|
|
|
62
61
|
## Usage
|
|
63
62
|
|
|
64
63
|
```ruby
|
|
65
|
-
# set period
|
|
66
|
-
|
|
67
|
-
period = [2, 4]
|
|
64
|
+
# set period hash
|
|
65
|
+
period = { from_date: 2, to_date: 4 }
|
|
68
66
|
|
|
69
67
|
# call BusinessPeriod::Days class to calculate period
|
|
70
68
|
BusinessPeriod::Days.call(period)
|
|
@@ -75,7 +73,7 @@ Let's say we have no holidays this month and today is Wednesday
|
|
|
75
73
|
|
|
76
74
|
let `work_days = [1, 2, 3, 4, 5]` (all days except weekends)
|
|
77
75
|
|
|
78
|
-
let `period =
|
|
76
|
+
let `period = { from_date: 2, to_date: 4 }`
|
|
79
77
|
|
|
80
78
|
* Begins to count period from the coming day
|
|
81
79
|
* Tomorrow (Thursday) is the first valid day
|
|
@@ -85,13 +83,14 @@ let `period = [2, 4]`
|
|
|
85
83
|
```console
|
|
86
84
|
irb(main):001:0> Time.current
|
|
87
85
|
=> Wed, 12 Sep 2018 05:49:10 UTC +00:00
|
|
88
|
-
irb(main):002:0> period =
|
|
89
|
-
=>
|
|
86
|
+
irb(main):002:0> period = { from_date: 2, to_date: 4 }
|
|
87
|
+
=> {:from_date=>2, :to_date=>4}
|
|
90
88
|
irb(main):003:0> BusinessPeriod::Days.call(period)
|
|
91
|
-
=>
|
|
89
|
+
=> {:from_date=>Fri, 14 Sep 2018, :to_date=>Tue, 18 Sep 2018]
|
|
92
90
|
```
|
|
93
91
|
|
|
94
92
|
## Todo
|
|
93
|
+
- [ ] improve specs
|
|
95
94
|
- [ ] Add latvian config
|
|
96
95
|
- [ ] Add estonian config
|
|
97
96
|
|
data/lib/business_period/base.rb
CHANGED
|
@@ -7,10 +7,9 @@ module BusinessPeriod
|
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
# Returns range of days
|
|
10
|
-
|
|
11
|
-
def calculate_period(plot)
|
|
10
|
+
def calculate_period(from_date, to_date)
|
|
12
11
|
@calculate_period ||= begin
|
|
13
|
-
imaginable_last_day = calculate_days_to_add_to(
|
|
12
|
+
imaginable_last_day = calculate_days_to_add_to(from_date, to_date)
|
|
14
13
|
|
|
15
14
|
# converts days to seconds
|
|
16
15
|
days_to_add = days_to_seconds(imaginable_last_day)
|
|
@@ -28,8 +27,8 @@ module BusinessPeriod
|
|
|
28
27
|
@holidays ||= YAML.load_file(File.join(holiday_config)).fetch('months')
|
|
29
28
|
end
|
|
30
29
|
|
|
31
|
-
# Dynamically calculates how many days we have add to
|
|
32
|
-
def calculate_days_to_add_to(
|
|
30
|
+
# Dynamically calculates how many days we have add to `to_date` end
|
|
31
|
+
def calculate_days_to_add_to(from_date, to_date)
|
|
33
32
|
# Calculates subjective number
|
|
34
33
|
sum = (7 - config.work_days.size)
|
|
35
34
|
|
|
@@ -38,7 +37,7 @@ module BusinessPeriod
|
|
|
38
37
|
sum = 1 if sum.zero?
|
|
39
38
|
|
|
40
39
|
# Some magic
|
|
41
|
-
(
|
|
40
|
+
(to_date - from_date) * 2 * sum + 7
|
|
42
41
|
end
|
|
43
42
|
|
|
44
43
|
private
|
data/lib/business_period/days.rb
CHANGED
|
@@ -2,19 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
module BusinessPeriod
|
|
4
4
|
class Days < Base
|
|
5
|
-
def self.call(
|
|
6
|
-
new.perform(
|
|
5
|
+
def self.call(from_date:, to_date:)
|
|
6
|
+
new.perform(from_date, to_date)
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
-
def perform(
|
|
10
|
-
@period = calculate_period(
|
|
9
|
+
def perform(from_date, to_date)
|
|
10
|
+
@period = calculate_period(from_date, to_date)
|
|
11
11
|
@days = business_days
|
|
12
12
|
|
|
13
13
|
check_holidays
|
|
14
14
|
|
|
15
|
-
# Selects business days from given
|
|
16
|
-
# E.g
|
|
17
|
-
|
|
15
|
+
# Selects business days from given from_date and to_date values.
|
|
16
|
+
# E.g from_date = 2, to_date = 4
|
|
17
|
+
# selects second and fourth elements
|
|
18
|
+
{
|
|
19
|
+
from_date: @days[from_date][:day],
|
|
20
|
+
to_date: @days[to_date][:day]
|
|
21
|
+
}
|
|
18
22
|
end
|
|
19
23
|
|
|
20
24
|
private
|
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.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- matas
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2018-10-
|
|
11
|
+
date: 2018-10-04 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rspec
|