business-period 0.0.2 → 0.0.2.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 +4 -4
- data/.circleci/config.yml +26 -0
- data/.codeclimate.yml +8 -0
- data/.gitignore +12 -0
- data/.rspec +4 -0
- data/.ruby-version +1 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +34 -0
- data/LICENSE.txt +21 -0
- data/README.md +92 -0
- data/Rakefile +8 -0
- data/business-period.gemspec +29 -0
- data/config/holidays/lt.yml +33 -0
- data/lib/business_period/base.rb +55 -0
- data/lib/business_period/configuration.rb +17 -0
- data/lib/business_period/days.rb +48 -0
- data/lib/business_period/version.rb +5 -0
- data/lib/business_period.rb +8 -0
- metadata +19 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3e02d1dcb164775cde27037f2e2df3787ce57e8a
|
|
4
|
+
data.tar.gz: 68d5ef403c026d8a3795080bae5e7b1b77bacb72
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4e60a333ad85b06ed62a4456cb8dd318ec3ee4b0e0fdc1fcbab548d957700d2332651e46ba064bb93540bacca7df5b2ac0746b63789eb13a1b8d25517432cdac
|
|
7
|
+
data.tar.gz: 3e6b2e12660e5db681bc45ddbc4340eca8f7779c1a2349bd33ad276bc8f35665ac466b49fa03234fa9c3d9886a31a2d73c3e089434c1d88d10aa14fbaebc78d7
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
version: 2.0
|
|
2
|
+
defaults: &defaults
|
|
3
|
+
working_directory: ~/business-period
|
|
4
|
+
docker:
|
|
5
|
+
- image: circleci/ruby:2.4.2-jessie-node-browsers
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
backend-tests:
|
|
9
|
+
<<: *defaults
|
|
10
|
+
steps:
|
|
11
|
+
- checkout
|
|
12
|
+
- run:
|
|
13
|
+
name: Setup dependencies
|
|
14
|
+
command: |
|
|
15
|
+
bundle install --without production --path=${BUNDLE_PATH:-vendor/bundle}
|
|
16
|
+
- run:
|
|
17
|
+
name: Run backend tests
|
|
18
|
+
command: |
|
|
19
|
+
bundle exec rake
|
|
20
|
+
|
|
21
|
+
workflows:
|
|
22
|
+
version: 2
|
|
23
|
+
|
|
24
|
+
commit:
|
|
25
|
+
jobs:
|
|
26
|
+
- backend-tests
|
data/.codeclimate.yml
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2.4.1
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
business-period (0.0.1)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
diff-lcs (1.3)
|
|
10
|
+
rake (12.3.1)
|
|
11
|
+
rspec (3.8.0)
|
|
12
|
+
rspec-core (~> 3.8.0)
|
|
13
|
+
rspec-expectations (~> 3.8.0)
|
|
14
|
+
rspec-mocks (~> 3.8.0)
|
|
15
|
+
rspec-core (3.8.0)
|
|
16
|
+
rspec-support (~> 3.8.0)
|
|
17
|
+
rspec-expectations (3.8.1)
|
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
19
|
+
rspec-support (~> 3.8.0)
|
|
20
|
+
rspec-mocks (3.8.0)
|
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
22
|
+
rspec-support (~> 3.8.0)
|
|
23
|
+
rspec-support (3.8.0)
|
|
24
|
+
|
|
25
|
+
PLATFORMS
|
|
26
|
+
ruby
|
|
27
|
+
|
|
28
|
+
DEPENDENCIES
|
|
29
|
+
business-period!
|
|
30
|
+
rake
|
|
31
|
+
rspec (~> 3.0)
|
|
32
|
+
|
|
33
|
+
BUNDLED WITH
|
|
34
|
+
1.16.4
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 matas
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
[](https://github.com/matass/business-period)
|
|
2
|
+
[](https://codeclimate.com/github/matass/business-period/maintainability)
|
|
3
|
+
|
|
4
|
+
# Business period
|
|
5
|
+
|
|
6
|
+
**BusinessPeriod** is a ruby library that calculates business period by given array.
|
|
7
|
+
This library was designed for lithuanian, latvian and estonian unemployment days.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
Add this line to your application's Gemfile:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
gem 'business-period'
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
And then execute:
|
|
18
|
+
|
|
19
|
+
$ bundle
|
|
20
|
+
|
|
21
|
+
Or install it yourself as:
|
|
22
|
+
|
|
23
|
+
$ gem install business-period
|
|
24
|
+
|
|
25
|
+
## Quickstart
|
|
26
|
+
|
|
27
|
+
##### Add an initializer file:
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
# config/initializers/business_period.rb
|
|
31
|
+
|
|
32
|
+
# set locale to get config file from config/holidays path
|
|
33
|
+
# set work_days to define which days of week are work days
|
|
34
|
+
|
|
35
|
+
BusinessPeriod.configure do |config|
|
|
36
|
+
config.locale = 'lt'
|
|
37
|
+
config.work_days = [1, 2, 3, 4, 5]
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## How it works
|
|
43
|
+
|
|
44
|
+
1. Dynamically calculates how many days we have to add to period end (Saturdays/Sundays/Holidays)
|
|
45
|
+
2. Generates new array
|
|
46
|
+
2. Extracts weekends and holidays from newly generated array
|
|
47
|
+
3. Generates result array
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
```ruby
|
|
52
|
+
# set period array
|
|
53
|
+
# 2 is our start day and 4 is our end day
|
|
54
|
+
period = [2, 4]
|
|
55
|
+
|
|
56
|
+
# call BusinessPeriod::Days class to calculate period
|
|
57
|
+
BusinessPeriod::Days.call(period)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Examples
|
|
61
|
+
Let's say we have no holidays this month and today is Wednesday
|
|
62
|
+
|
|
63
|
+
let `work_days = [1, 2, 3, 4, 5]` (all days except weekends)
|
|
64
|
+
|
|
65
|
+
let `period = [2, 4]`
|
|
66
|
+
|
|
67
|
+
* Begins to count period from the coming day
|
|
68
|
+
* Tomorrow (Thursday) is the first valid day
|
|
69
|
+
* The second valid day will be Fridary (first business day)
|
|
70
|
+
* Fourth business day will be Tuesday (Saturday and Sunday are not in scope)
|
|
71
|
+
|
|
72
|
+
```console
|
|
73
|
+
irb(main):001:0> Time.current
|
|
74
|
+
=> Wed, 12 Sep 2018 05:49:10 UTC +00:00
|
|
75
|
+
irb(main):002:0> period = [2, 4]
|
|
76
|
+
=> [2, 4]
|
|
77
|
+
irb(main):003:0> BusinessPeriod::Days.call(period)
|
|
78
|
+
=> [Fri, 14 Sep 2018, Tue, 18 Sep 2018]
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Todo
|
|
82
|
+
- [ ] Add latvian config
|
|
83
|
+
- [ ] Add estonian config
|
|
84
|
+
- [ ] Calculate Easter holidays
|
|
85
|
+
|
|
86
|
+
## Development
|
|
87
|
+
|
|
88
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
89
|
+
|
|
90
|
+
## License
|
|
91
|
+
|
|
92
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
|
+
|
|
6
|
+
require 'business_period/version'
|
|
7
|
+
|
|
8
|
+
Gem::Specification.new do |spec|
|
|
9
|
+
spec.name = 'business-period'
|
|
10
|
+
spec.version = BusinessPeriod::VERSION
|
|
11
|
+
spec.authors = ['matas']
|
|
12
|
+
spec.email = ['matas.simonaitis@ksdigital.lt']
|
|
13
|
+
|
|
14
|
+
spec.summary = '📅 calculate business period'
|
|
15
|
+
spec.description = ''
|
|
16
|
+
spec.homepage = 'https://github.com/matass/business-period'
|
|
17
|
+
spec.license = 'MIT'
|
|
18
|
+
|
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
20
|
+
f.match(%r{^(test|spec|features)/})
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
spec.platform = Gem::Platform::RUBY
|
|
24
|
+
|
|
25
|
+
spec.require_paths = ['lib']
|
|
26
|
+
spec.required_ruby_version = '>= 2.4.1'
|
|
27
|
+
|
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
|
29
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
months:
|
|
2
|
+
1:
|
|
3
|
+
- mday: 1
|
|
4
|
+
name: Naujieji metai
|
|
5
|
+
2:
|
|
6
|
+
- mday: 16
|
|
7
|
+
name: Valstybės atkūrimo diena
|
|
8
|
+
3:
|
|
9
|
+
- mday: 11
|
|
10
|
+
name: Nepriklausomybės atkūrimo diena
|
|
11
|
+
5:
|
|
12
|
+
- mday: 1
|
|
13
|
+
name: Darbininkų diena
|
|
14
|
+
6:
|
|
15
|
+
- mday: 24
|
|
16
|
+
name: Joninės
|
|
17
|
+
7:
|
|
18
|
+
- mday: 6
|
|
19
|
+
name: Valstybės diena
|
|
20
|
+
8:
|
|
21
|
+
- mday: 15
|
|
22
|
+
name: Žolinė
|
|
23
|
+
11:
|
|
24
|
+
- mday: 1
|
|
25
|
+
name: Visų šventųjų diena
|
|
26
|
+
12:
|
|
27
|
+
- mday: 24
|
|
28
|
+
name: Šv. Kūčios
|
|
29
|
+
- mday: 25
|
|
30
|
+
name: Šv. Kalėdos
|
|
31
|
+
- mday: 26
|
|
32
|
+
name: Antroji Kalėdų diena
|
|
33
|
+
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module BusinessPeriod
|
|
4
|
+
class Base
|
|
5
|
+
def config
|
|
6
|
+
@config ||= BusinessPeriod.configuration
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
# Returns range of days
|
|
10
|
+
# Plot is our given period. E.g. [2, 4]
|
|
11
|
+
def calculate_period(plot)
|
|
12
|
+
@calculate_period ||= begin
|
|
13
|
+
imaginable_last_day = calculate_days_to_add_to(plot)
|
|
14
|
+
|
|
15
|
+
# converts days to seconds
|
|
16
|
+
days_to_add = days_to_seconds(imaginable_last_day)
|
|
17
|
+
|
|
18
|
+
# Adds expected last day to current day
|
|
19
|
+
finish = Time.now + days_to_add
|
|
20
|
+
|
|
21
|
+
# Builds an array from current time to imaginable end time
|
|
22
|
+
Time.now.to_date..finish.to_date
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Sets holiday instance by given file location
|
|
27
|
+
def holidays
|
|
28
|
+
@holidays ||= YAML.load_file(File.join(holiday_config)).fetch('months')
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Dynamically calculates how many days we have add to plot end
|
|
32
|
+
def calculate_days_to_add_to(plot)
|
|
33
|
+
# Calculates subjective number
|
|
34
|
+
sum = (7 - config.work_days.size)
|
|
35
|
+
|
|
36
|
+
# sum cannot be zero.
|
|
37
|
+
# Relevant when when work_days array == [1, 2, 3, 4, 5, 6, 7]
|
|
38
|
+
sum = 1 if sum.zero?
|
|
39
|
+
|
|
40
|
+
# Some magic
|
|
41
|
+
(plot.last - plot.first) * 2 * sum + 7
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
# Gets path to config file by given locale
|
|
47
|
+
def holiday_config
|
|
48
|
+
[File.dirname(__FILE__), "../../config/holidays/#{config.locale}.yml"]
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def days_to_seconds(days)
|
|
52
|
+
days * 60 * 60 * 24
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module BusinessPeriod
|
|
4
|
+
attr_accessor :configuration
|
|
5
|
+
|
|
6
|
+
def self.configuration
|
|
7
|
+
@configuration ||= Configuration.new
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def self.configure
|
|
11
|
+
yield configuration
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class Configuration
|
|
15
|
+
attr_accessor :locale, :work_days
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module BusinessPeriod
|
|
4
|
+
class Days < Base
|
|
5
|
+
def self.call(plot)
|
|
6
|
+
new.perform(plot)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def perform(plot)
|
|
10
|
+
@period = calculate_period(plot)
|
|
11
|
+
@days = business_days
|
|
12
|
+
|
|
13
|
+
check_holidays
|
|
14
|
+
|
|
15
|
+
# Selects business days from given plot.
|
|
16
|
+
# E.g plot = [2, 4], selects second and fourth elements
|
|
17
|
+
plot.map { |param| @days[param][:day] }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
# Maps business days to array by given calculated period
|
|
23
|
+
def business_days
|
|
24
|
+
@business_days ||= @period.map do |day|
|
|
25
|
+
# checks if day is business day
|
|
26
|
+
if config.work_days.include?(day.wday)
|
|
27
|
+
{ day: day, month: day.to_time.month }
|
|
28
|
+
end
|
|
29
|
+
end.compact
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Removes business days which are on holidays
|
|
33
|
+
def check_holidays
|
|
34
|
+
@days.each_with_index do |day, index|
|
|
35
|
+
# Skips if no holiday in the current month
|
|
36
|
+
next unless holidays[day[:month]]
|
|
37
|
+
|
|
38
|
+
extract_holidays(day, index)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def extract_holidays(day, index)
|
|
43
|
+
holidays[day[:month]].each do |holiday|
|
|
44
|
+
@days.delete_at(index) if holiday['mday'] == day[:day].mday
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: business-period
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.2
|
|
4
|
+
version: 0.0.2.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- matas
|
|
@@ -30,7 +30,24 @@ email:
|
|
|
30
30
|
executables: []
|
|
31
31
|
extensions: []
|
|
32
32
|
extra_rdoc_files: []
|
|
33
|
-
files:
|
|
33
|
+
files:
|
|
34
|
+
- ".circleci/config.yml"
|
|
35
|
+
- ".codeclimate.yml"
|
|
36
|
+
- ".gitignore"
|
|
37
|
+
- ".rspec"
|
|
38
|
+
- ".ruby-version"
|
|
39
|
+
- Gemfile
|
|
40
|
+
- Gemfile.lock
|
|
41
|
+
- LICENSE.txt
|
|
42
|
+
- README.md
|
|
43
|
+
- Rakefile
|
|
44
|
+
- business-period.gemspec
|
|
45
|
+
- config/holidays/lt.yml
|
|
46
|
+
- lib/business_period.rb
|
|
47
|
+
- lib/business_period/base.rb
|
|
48
|
+
- lib/business_period/configuration.rb
|
|
49
|
+
- lib/business_period/days.rb
|
|
50
|
+
- lib/business_period/version.rb
|
|
34
51
|
homepage: https://github.com/matass/business-period
|
|
35
52
|
licenses:
|
|
36
53
|
- MIT
|