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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d341e7c30a7da55a96ae31a6312dc3aa0d8870a8
4
- data.tar.gz: 5fdc3d52b49f83cbb6b2857b4b92fca1785e0984
3
+ metadata.gz: 6910f7cb08ae02406dab9a48a5be8a9fa13fa4db
4
+ data.tar.gz: f29b0758ddc9d9f4736a8b3fa1c0fb1ce44e07b9
5
5
  SHA512:
6
- metadata.gz: dfbce184b7c997dfb249523dd9444b84fc27954dbd37ac77bd4e11099c91827801561473c161cbf1202dd98f54d848ab77d48d363aaacf7e03e186f892fbed12
7
- data.tar.gz: 14c41468341715c5e9162f017eb0bc548b782081f51fd54545e58e57b1cc9b166ea6b2f992346c145afc78f912a04c421b585eebb25512cee4881a50fe57e00e
6
+ metadata.gz: 6a1ad16665a792b9cba88b700b2486cc162feb1b846246ce0cfa28fbed780ec6aaccfcbd0f4fcfa70268ed3cb651c7c98a13009c65d1eeeecbef654db4f9977e
7
+ data.tar.gz: 0b4cce9bd863d542a032c7be80e28e7cc5a8393f2eeab27c8e2072d0e12d9e4099e188ee54df394bdbe2e79c235e5374c4914f9a0ceeefca8a88d2aed563b11d
data/.codeclimate.yml CHANGED
@@ -4,3 +4,5 @@ plugins:
4
4
  checks:
5
5
  Rubocop/Style/Documentation:
6
6
  enabled: false
7
+ Rubocop/Metrics/LineLength:
8
+ enabled: false
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/business-period.svg)](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 array.
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
- ##### Add an initializer file:
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.configure do |config|
41
- config.locale = locale
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
- ##### Initialize through params:
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::Days.new(locale, work_days)
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 array
66
- # 2 is our start day and 4 is our end day
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 = [2, 4]`
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 = [2, 4]
89
- => [2, 4]
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
- => [Fri, 14 Sep 2018, Tue, 18 Sep 2018]
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
 
@@ -7,10 +7,9 @@ module BusinessPeriod
7
7
  end
8
8
 
9
9
  # Returns range of days
10
- # Plot is our given period. E.g. [2, 4]
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(plot)
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 plot end
32
- def calculate_days_to_add_to(plot)
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
- (plot.last - plot.first) * 2 * sum + 7
40
+ (to_date - from_date) * 2 * sum + 7
42
41
  end
43
42
 
44
43
  private
@@ -2,19 +2,23 @@
2
2
 
3
3
  module BusinessPeriod
4
4
  class Days < Base
5
- def self.call(plot)
6
- new.perform(plot)
5
+ def self.call(from_date:, to_date:)
6
+ new.perform(from_date, to_date)
7
7
  end
8
8
 
9
- def perform(plot)
10
- @period = calculate_period(plot)
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 plot.
16
- # E.g plot = [2, 4], selects second and fourth elements
17
- plot.map { |param| @days[param][:day] }
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BusinessPeriod
4
- VERSION = '0.0.5'
4
+ VERSION = '0.0.6'
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.0.5
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-02 00:00:00.000000000 Z
11
+ date: 2018-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec