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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d4c8bd4b3bc40d590d96ae851d29030ff020b58
4
- data.tar.gz: a561843686e1136e2608fab2df7bd2d3442f2a8c
3
+ metadata.gz: b90def6c2e0624d299b2a28109bbf013fe61f05f
4
+ data.tar.gz: bdce177f079801e02ad7767fe51c6be716ede2e3
5
5
  SHA512:
6
- metadata.gz: a8cc4b9366d2d95dcaa659138d2a1c033b2ac72ef5529f4ad469a321ecb830e5156eb980463a57816ff942c4959097d43db36964502ccb4c680a55a72f6c7168
7
- data.tar.gz: ce829b8356cab36ab3e3e0f5698be6032e47444295747de5e00fdf4b87e577f3f975f56cbeffb74831ed859daeb8ed7100ea987e3db59a0e716455cc93b3571b
6
+ metadata.gz: '099bede4e8653320424c59b246a02483179f19700c642a832917d2bfa2f04d983ae298dababd54487f56b2afa95c27fe607e99bc43ad174f323bf83ee7cd9086'
7
+ data.tar.gz: 6c539ee970451a3ca75cf37f8d8835b0d6cdc6882aad61c0a171ecb0bc579b5d26787daa1add86a4da4959b89c9d496acb388947cfe4089e8c47bcf723303e6b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- business-period (0.0.8)
4
+ business-period (0.0.9)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -6,43 +6,19 @@ module BusinessPeriod
6
6
  @config ||= Config
7
7
  end
8
8
 
9
- # Returns range of days
10
- def calculate_period(from_date, to_date)
11
- @calculate_period ||= begin
12
- imaginable_last_day = calculate_days_to_add_to(from_date, to_date)
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
@@ -7,48 +7,49 @@ module BusinessPeriod
7
7
  end
8
8
 
9
9
  def perform(from_date, to_date)
10
- return {} if from_date.nil? || to_date.nil?
11
- return {} if from_date > to_date
10
+ return {} unless valid_params(from_date, to_date)
12
11
 
13
- @period = calculate_period(from_date, to_date)
14
- @days = business_days
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: @days[from_date][:day],
23
- to_date: @days[to_date][:day]
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
- # Maps business days to array by given calculated period
30
- def business_days
31
- @business_days ||= @period.map do |day|
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
- # Removes business days which are on holidays
40
- def check_holidays
41
- @days.each_with_index do |day, index|
42
- # Skips if no holiday in the current month
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
- @days.delete_at(index) if holiday['mday'] == day[:day].mday
52
+ business_days.delete_at(index) if holiday['mday'] == day[:day].mday
52
53
  end
53
54
  end
54
55
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BusinessPeriod
4
- VERSION = '0.0.8'
4
+ VERSION = '0.0.9'
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.8
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-05 00:00:00.000000000 Z
11
+ date: 2018-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec