roxbury 0.1.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 95a78111c7254e8b3e5f35c69e781f16f971ad839c0e7c76c42b4f41ea681bd7
4
- data.tar.gz: 9d76e376abecaa9796dd40f532b66d69e623876a0efc9360cf70976d4a4a9359
3
+ metadata.gz: 648f2d7a269ddbb4312323a30b7cd28053d64e2cadd2ba8da5464ea63a7333e6
4
+ data.tar.gz: 833a25391504a46b2b2e99a46c874fae76f2c77c7a14df0c662aecb97af3bcda
5
5
  SHA512:
6
- metadata.gz: db1b169f1db6ce10e4a9831a2400a3ff914f0bb5c7ba31d0bca9527fa144ded503200b78e2583bc1d619337862aa7126134f8cd852a54566c8efaef1f455fc32
7
- data.tar.gz: 12b76c182370f9b382cdb15f73c7eb3afebdf1300c94973fc0bec8d1a56178a280323ac0bd3b2d3ff9ed6908ec6b9d4e13dcf4c59e04759f9f75fb502951a10b
6
+ metadata.gz: 7bc1293b16b8197be0c7a7d635f5553b2902b0dd40d4b00d86a393a9ec44da6b925485e7d749c86a14d3257aa68c922e57f2a3376aace81885d695b254ad3b6d
7
+ data.tar.gz: 71d00ab54a4f6e4e059ca8e6eaa1ba7a522fb1075d608f67f9a973dbd65d41927baa2e261a9ab5197294d7e9cfc92f804b7d767e298cf99e2c1d34e10209b9ca
data/.envrc ADDED
@@ -0,0 +1,2 @@
1
+ layout ruby
2
+ use nix
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  /.bundle/
2
2
  /.yardoc
3
+ /.direnv/
3
4
  /_yardoc/
4
5
  /coverage/
5
6
  /doc/
data/.travis.yml CHANGED
@@ -2,6 +2,4 @@
2
2
  sudo: false
3
3
  language: ruby
4
4
  cache: bundler
5
- rvm:
6
- - 2.5.0
7
5
  before_install: gem install bundler -v 2.0.2
data/Gemfile.lock CHANGED
@@ -1,31 +1,31 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- roxbury (0.1.1)
4
+ roxbury (0.3.0)
5
5
  activesupport (>= 4.2)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activesupport (6.0.0)
10
+ activesupport (6.0.3.1)
11
11
  concurrent-ruby (~> 1.0, >= 1.0.2)
12
12
  i18n (>= 0.7, < 2)
13
13
  minitest (~> 5.1)
14
14
  tzinfo (~> 1.1)
15
- zeitwerk (~> 2.1, >= 2.1.8)
15
+ zeitwerk (~> 2.2, >= 2.2.2)
16
16
  byebug (11.0.1)
17
- coderay (1.1.2)
18
- concurrent-ruby (1.1.5)
17
+ coderay (1.1.3)
18
+ concurrent-ruby (1.1.6)
19
19
  diff-lcs (1.3)
20
- ffi (1.10.0)
21
- formatador (0.2.5)
22
- guard (2.14.2)
20
+ ffi (1.15.5)
21
+ formatador (1.1.0)
22
+ guard (2.18.0)
23
23
  formatador (>= 0.2.4)
24
24
  listen (>= 2.7, < 4.0)
25
25
  lumberjack (>= 1.0.12, < 2.0)
26
26
  nenv (~> 0.1)
27
27
  notiffany (~> 0.0)
28
- pry (>= 0.9.12)
28
+ pry (>= 0.13.0)
29
29
  shellany (~> 0.0)
30
30
  thor (>= 0.18.1)
31
31
  guard-compat (1.2.1)
@@ -33,28 +33,27 @@ GEM
33
33
  guard (~> 2.1)
34
34
  guard-compat (~> 1.1)
35
35
  rspec (>= 2.99.0, < 4.0)
36
- i18n (1.6.0)
36
+ i18n (1.8.2)
37
37
  concurrent-ruby (~> 1.0)
38
- listen (3.1.5)
39
- rb-fsevent (~> 0.9, >= 0.9.4)
40
- rb-inotify (~> 0.9, >= 0.9.7)
41
- ruby_dep (~> 1.2)
42
- lumberjack (1.0.13)
43
- method_source (0.9.2)
44
- minitest (5.11.3)
38
+ listen (3.7.1)
39
+ rb-fsevent (~> 0.10, >= 0.10.3)
40
+ rb-inotify (~> 0.9, >= 0.9.10)
41
+ lumberjack (1.2.8)
42
+ method_source (1.0.0)
43
+ minitest (5.14.1)
45
44
  nenv (0.3.0)
46
- notiffany (0.1.1)
45
+ notiffany (0.1.3)
47
46
  nenv (~> 0.1)
48
47
  shellany (~> 0.0)
49
- pry (0.11.3)
50
- coderay (~> 1.1.0)
51
- method_source (~> 0.9.0)
48
+ pry (0.14.1)
49
+ coderay (~> 1.1)
50
+ method_source (~> 1.0)
52
51
  pry-byebug (3.7.0)
53
52
  byebug (~> 11.0)
54
53
  pry (~> 0.10)
55
- rake (10.5.0)
56
- rb-fsevent (0.10.3)
57
- rb-inotify (0.10.0)
54
+ rake (13.0.1)
55
+ rb-fsevent (0.11.1)
56
+ rb-inotify (0.10.1)
58
57
  ffi (~> 1.0)
59
58
  rspec (3.8.0)
60
59
  rspec-core (~> 3.8.0)
@@ -69,13 +68,12 @@ GEM
69
68
  diff-lcs (>= 1.2.0, < 2.0)
70
69
  rspec-support (~> 3.8.0)
71
70
  rspec-support (3.8.2)
72
- ruby_dep (1.5.0)
73
71
  shellany (0.0.1)
74
- thor (0.20.3)
72
+ thor (1.2.1)
75
73
  thread_safe (0.3.6)
76
- tzinfo (1.2.5)
74
+ tzinfo (1.2.7)
77
75
  thread_safe (~> 0.1)
78
- zeitwerk (2.1.9)
76
+ zeitwerk (2.3.0)
79
77
 
80
78
  PLATFORMS
81
79
  ruby
@@ -86,7 +84,7 @@ DEPENDENCIES
86
84
  guard-rspec
87
85
  pry
88
86
  pry-byebug
89
- rake (~> 10.0)
87
+ rake (~> 13.0)
90
88
  roxbury!
91
89
  rspec (~> 3.0)
92
90
 
data/README.md CHANGED
@@ -4,7 +4,7 @@ A Ruby library for handling business days calculations, e.g., working days/hours
4
4
 
5
5
  It was extracted from a production application that's been running for many years.
6
6
 
7
- [![Build Status](https://travis-ci.org/eeng/roxbury.svg?branch=master)](https://travis-ci.org/eeng/roxbury)
7
+ [![Build Status](https://app.travis-ci.com/eeng/roxbury.svg?branch=master)](https://app.travis-ci.com/eeng/roxbury)
8
8
 
9
9
  ## Installation
10
10
 
@@ -36,9 +36,12 @@ module Roxbury
36
36
  rolling_timestamp = roll_forward(to)
37
37
  remaining_hours = number_of_hours
38
38
 
39
- until (bday = business_day(rolling_timestamp)).include?(rolling_timestamp + remaining_hours.hours)
39
+ loop do
40
+ bday = business_day(rolling_timestamp)
41
+ break if bday.include?(rolling_timestamp + remaining_hours.hours)
40
42
  remaining_hours -= bday.number_of_working_hours(from: rolling_timestamp)
41
- rolling_timestamp = at_beginning_of_next_business_day(rolling_timestamp)
43
+ break if remaining_hours < 0
44
+ rolling_timestamp = next_working_day(rolling_timestamp)
42
45
  end
43
46
 
44
47
  rolling_timestamp + remaining_hours.hours
@@ -64,7 +67,7 @@ module Roxbury
64
67
  remaining_days = number_of_days
65
68
  rolling_date = to
66
69
  loop do
67
- remaining_days -= business_day(rolling_date).number_of_working_hours * 1.0 / max_working_hours_in_a_day
70
+ remaining_days -= working_hours_percentage(rolling_date)
68
71
  break if remaining_days < 0
69
72
  rolling_date = roll_forward rolling_date.next
70
73
  end
@@ -86,9 +89,48 @@ module Roxbury
86
89
  end
87
90
  end
88
91
 
89
- # Snaps the date to the beginning of the next business day.
90
- def at_beginning_of_next_business_day date
91
- roll_forward date.tomorrow.beginning_of_day
92
+ # Snaps the date to the end of the previous business day, unless it is already within the working hours of a business day.
93
+ #
94
+ # @param date [Date, Time]
95
+ def roll_backward date
96
+ bday = business_day(date)
97
+ if bday.include?(date)
98
+ date
99
+ elsif bday.ends_before?(date)
100
+ bday.at_end
101
+ else
102
+ roll_backward(date.is_a?(Date) ? date.yesterday : date.yesterday.end_of_day)
103
+ end
104
+ end
105
+
106
+ # If a Date is given, returns then next business day.
107
+ # Otherwise if a Time is given, snaps the date to the beginning of the next business day.
108
+ def next_working_day date
109
+ case date
110
+ when Time
111
+ roll_forward date.tomorrow.beginning_of_day
112
+ when Date
113
+ roll_forward date.tomorrow
114
+ else
115
+ raise ArgumentError, 'only Date or Time instances are allowed'
116
+ end
117
+ end
118
+
119
+ # If a Date is given, returns then prev business day.
120
+ # Otherwise if a Time is given, snaps the date to the beginning of the prev business day.
121
+ def prev_working_day date
122
+ case date
123
+ when Time
124
+ roll_backward date.yesterday.end_of_day
125
+ when Date
126
+ roll_backward date.yesterday
127
+ else
128
+ raise ArgumentError, 'only Date or Time instances are allowed'
129
+ end
130
+ end
131
+
132
+ def working_hours_percentage date
133
+ business_day(date).number_of_working_hours * 1.0 / max_working_hours_in_a_day
92
134
  end
93
135
 
94
136
  def holiday? date_or_time
@@ -22,6 +22,10 @@ module Roxbury
22
22
  same_day?(timestamp) && @working_hours.starts_after?(timestamp)
23
23
  end
24
24
 
25
+ def ends_before? timestamp
26
+ same_day?(timestamp) && @working_hours.ends_before?(timestamp)
27
+ end
28
+
25
29
  def at_beginning
26
30
  @working_hours.at_beginning @date
27
31
  end
@@ -13,6 +13,10 @@ module Roxbury
13
13
  false
14
14
  end
15
15
 
16
+ def ends_before? _timestamp
17
+ false
18
+ end
19
+
16
20
  def non_working?
17
21
  true
18
22
  end
@@ -1,3 +1,3 @@
1
1
  module Roxbury
2
- VERSION = '0.1.1'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -35,6 +35,10 @@ module Roxbury
35
35
  timestamp < at_beginning(timestamp)
36
36
  end
37
37
 
38
+ def ends_before? timestamp
39
+ timestamp > at_end(timestamp)
40
+ end
41
+
38
42
  def at_beginning timestamp
39
43
  timestamp.change(hour: begins_at, min: 0, sec: 0)
40
44
  end
data/roxbury.gemspec CHANGED
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.add_dependency "activesupport", ">= 4.2"
26
26
 
27
27
  spec.add_development_dependency "bundler", "~> 2.0"
28
- spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rake", "~> 13.0"
29
29
  spec.add_development_dependency "rspec", "~> 3.0"
30
30
  spec.add_development_dependency "guard"
31
31
  spec.add_development_dependency "guard-rspec"
data/shell.nix ADDED
@@ -0,0 +1,9 @@
1
+ with import <nixpkgs> {};
2
+ mkShell {
3
+ nativeBuildInputs = [
4
+ ruby
5
+ ];
6
+ shellHook = ''
7
+ gem list -i '^bundler$' -v 2.0.2 >/dev/null || gem install bundler --version=2.0.2 --no-document
8
+ '';
9
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roxbury
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emmanuel Nicolau
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-28 00:00:00.000000000 Z
11
+ date: 1980-01-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: '13.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: '13.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -130,6 +130,7 @@ executables: []
130
130
  extensions: []
131
131
  extra_rdoc_files: []
132
132
  files:
133
+ - ".envrc"
133
134
  - ".gitignore"
134
135
  - ".rspec"
135
136
  - ".rubocop.yml"
@@ -152,11 +153,12 @@ files:
152
153
  - lib/roxbury/version.rb
153
154
  - lib/roxbury/working_hours.rb
154
155
  - roxbury.gemspec
156
+ - shell.nix
155
157
  homepage: https://github.com/eeng/roxbury
156
158
  licenses:
157
159
  - MIT
158
160
  metadata: {}
159
- post_install_message:
161
+ post_install_message:
160
162
  rdoc_options: []
161
163
  require_paths:
162
164
  - lib
@@ -171,9 +173,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
171
173
  - !ruby/object:Gem::Version
172
174
  version: '0'
173
175
  requirements: []
174
- rubyforge_project:
175
- rubygems_version: 2.7.3
176
- signing_key:
176
+ rubygems_version: 3.2.26
177
+ signing_key:
177
178
  specification_version: 4
178
179
  summary: Ruby library for business days calculations
179
180
  test_files: []