biz 1.0.0 → 1.1.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
  SHA1:
3
- metadata.gz: 29c21b59e6fde3b429ebf855ccebec03742d0619
4
- data.tar.gz: 8adbadda565386c89c078b146a69ccbf0e45972f
3
+ metadata.gz: f865f5197acf9cc026d7d77c15626a7d03a5e8c1
4
+ data.tar.gz: 5d150d70060e27513f373c3b32e96dbd0e4f68fc
5
5
  SHA512:
6
- metadata.gz: 171eb369d1f45cba0e0e7e8a8d3c8d19043b18b25830f900df21030fc2b7c110c2d97d9ebcada7cb927ad704a961a0c1bcf532c56c5f630d09e9623e823607c8
7
- data.tar.gz: 7a5ae606a3ce3bed526007dab290e301271ad5eb1829c485984b70d35bb78533e3477180e4e01807a9acc1bf63f9239ae13a5f034fcdb3389c4d7dcea1652b21
6
+ metadata.gz: a3eb38f1f622c23d1d1524ad053ae07bd789277d0a3b5b5a8bb42a2aef7453d8a976eed7878e97b8207861b0e58860d6a6e5950bc5507468e44ea62ba82c6e3e
7
+ data.tar.gz: 6871ae32bf137626682626e80f8f4ae115b162b1d7ac104ed1b615e0a3b06baa7979a3ea79ab5628d25c79b54d42910ddd415e28b2ce6e0bfc28de1a44b1a7fc
data/README.md CHANGED
@@ -1,16 +1,21 @@
1
1
  # biz
2
- [![Build Status](https://magnum.travis-ci.com/zendesk/biz.svg?token=FPvAz1WHPkjgRp2szEGq&branch=master)](https://magnum.travis-ci.com/zendesk/biz)
3
- [![Code Climate](https://codeclimate.com/repos/54ac74216956802dc40027d6/badges/591180c7fa5da2a8aa3d/gpa.svg)](https://codeclimate.com/repos/54ac74216956802dc40027d6/feed)
4
- [![Test Coverage](https://codeclimate.com/repos/54ac74216956802dc40027d6/badges/591180c7fa5da2a8aa3d/coverage.svg)](https://codeclimate.com/repos/54ac74216956802dc40027d6/feed)
2
+ [![Gem Version](https://badge.fury.io/rb/biz.svg)](http://badge.fury.io/rb/biz)
3
+ [![Build Status](https://travis-ci.org/zendesk/biz.svg?branch=master)](https://travis-ci.org/zendesk/biz)
4
+ [![Code Climate](https://codeclimate.com/github/zendesk/biz/badges/gpa.svg)](https://codeclimate.com/github/zendesk/biz)
5
+ [![Test Coverage](https://codeclimate.com/github/zendesk/biz/badges/coverage.svg)](https://codeclimate.com/github/zendesk/biz)
6
+ [![Dependency Status](https://gemnasium.com/zendesk/biz.svg)](https://gemnasium.com/zendesk/biz)
5
7
 
6
8
  Time calculations using business hours.
7
9
 
8
10
  ## Features
9
11
 
12
+ * Support for:
13
+ - Intervals spanning the entire day.
14
+ - Interday intervals and holidays.
15
+ - Multiple intervals per day.
16
+ - Multiple schedule configurations.
10
17
  * Second-level precision on all calculations.
11
18
  * Accurate handling of Daylight Saving Time.
12
- * Support for intervals spanning any period of the day, including the entirety.
13
- * Support for interday intervals and holidays.
14
19
  * Thread-safe.
15
20
 
16
21
  ## Anti-Features
@@ -36,7 +41,7 @@ Or install it yourself as:
36
41
 
37
42
  ```ruby
38
43
  Biz.configure do |config|
39
- config.business_hours = {
44
+ config.hours = {
40
45
  mon: {'09:00' => '17:00'},
41
46
  tue: {'00:00' => '24:00'},
42
47
  wed: {'09:00' => '17:00'},
@@ -58,6 +63,9 @@ Biz::Schedule.new do |config|
58
63
  end
59
64
  ```
60
65
 
66
+ Note that times must be specified in 24-hour clock format and time zones
67
+ must be [IANA identifiers](http://en.wikipedia.org/wiki/List_of_tz_database_time_zones).
68
+
61
69
  ## Usage
62
70
 
63
71
  ```ruby
@@ -71,7 +79,41 @@ Biz.time(2, :hours).after(Time.utc(2015, 12, 25, 9, 30))
71
79
  Biz.within(Time.utc(2015, 3, 7), Time.utc(2015, 3, 14)).in_seconds
72
80
 
73
81
  # Determine if a time is in business hours
74
- Biz.business_hours?(Time.utc(2015, 1, 10, 9))
82
+ Biz.in_hours?(Time.utc(2015, 1, 10, 9))
83
+ ```
84
+
85
+ Note that all returned times are in UTC.
86
+
87
+ By dropping down a level, you can get access to the underlying time segments,
88
+ which you can use to do your own custom calculations or just get a better idea
89
+ of what's happening under the hood:
90
+
91
+ ```ruby
92
+ Biz.periods
93
+ .after(Time.utc(2015, 1, 10, 10))
94
+ .timeline.forward
95
+ .until(Time.utc(2015, 1, 17, 10)).to_a
96
+
97
+ #=> [#<Biz::TimeSegment start_time=2015-01-10 18:00:00 UTC end_time=2015-01-10 22:00:00 UTC>,
98
+ # #<Biz::TimeSegment start_time=2015-01-12 17:00:00 UTC end_time=2015-01-13 01:00:00 UTC>,
99
+ # #<Biz::TimeSegment start_time=2015-01-13 08:00:00 UTC end_time=2015-01-14 08:00:00 UTC>,
100
+ # #<Biz::TimeSegment start_time=2015-01-14 17:00:00 UTC end_time=2015-01-15 01:00:00 UTC>,
101
+ # #<Biz::TimeSegment start_time=2015-01-15 17:00:00 UTC end_time=2015-01-15 20:00:00 UTC>,
102
+ # #<Biz::TimeSegment start_time=2015-01-15 21:00:00 UTC end_time=2015-01-16 01:00:00 UTC>]
103
+
104
+ Biz.periods
105
+ .before(Time.utc(2015, 5, 5, 12, 34, 57))
106
+ .timeline.backward
107
+ .for(Biz::Duration.minutes(3_598)).to_a
108
+
109
+ #=> [#<Biz::TimeSegment start_time=2015-05-05 07:00:00 UTC end_time=2015-05-05 12:34:57 UTC>,
110
+ # #<Biz::TimeSegment start_time=2015-05-04 16:00:00 UTC end_time=2015-05-05 00:00:00 UTC>,
111
+ # #<Biz::TimeSegment start_time=2015-05-02 17:00:00 UTC end_time=2015-05-02 21:00:00 UTC>,
112
+ # #<Biz::TimeSegment start_time=2015-04-30 20:00:00 UTC end_time=2015-05-01 00:00:00 UTC>,
113
+ # #<Biz::TimeSegment start_time=2015-04-30 16:00:00 UTC end_time=2015-04-30 19:00:00 UTC>,
114
+ # #<Biz::TimeSegment start_time=2015-04-29 16:00:00 UTC end_time=2015-04-30 00:00:00 UTC>,
115
+ # #<Biz::TimeSegment start_time=2015-04-28 07:00:00 UTC end_time=2015-04-29 07:00:00 UTC>,
116
+ # #<Biz::TimeSegment start_time=2015-04-27 20:36:57 UTC end_time=2015-04-28 00:00:00 UTC>]
75
117
  ```
76
118
 
77
119
  ## Core Extensions
@@ -114,12 +156,17 @@ To open a console with the gem and sample schedule loaded:
114
156
 
115
157
  script/console
116
158
 
159
+ ## Alternatives
160
+
161
+ * [`business_time`](https://github.com/bokmann/business_time)
162
+ * [`working_hours`](https://github.com/Intrepidd/working_hours)
163
+
117
164
  ## Copyright and license
118
165
 
119
166
  Copyright 2015 Zendesk
120
167
 
121
168
  Licensed under the Apache License, Version 2.0 (the "License"); you may not use
122
- this file except in compliance with the License.
169
+ this gem except in compliance with the License.
123
170
 
124
171
  You may obtain a copy of the License at
125
172
  http://www.apache.org/licenses/LICENSE-2.0.
data/lib/biz.rb CHANGED
@@ -24,6 +24,7 @@ module Biz
24
24
  periods
25
25
  time
26
26
  within
27
+ in_hours?
27
28
  business_hours?
28
29
  ] => :schedule
29
30
 
@@ -2,9 +2,9 @@ module Biz
2
2
  module Calculation
3
3
  class DurationWithin < SimpleDelegator
4
4
 
5
- def initialize(schedule, calculation_period)
5
+ def initialize(periods, calculation_period)
6
6
  super(
7
- schedule.periods.after(calculation_period.start_time)
7
+ periods.after(calculation_period.start_time)
8
8
  .timeline.forward
9
9
  .until(calculation_period.end_time)
10
10
  .map(&:duration)
@@ -2,27 +2,27 @@ module Biz
2
2
  module Calculation
3
3
  class ForDuration
4
4
 
5
- attr_reader :schedule,
5
+ attr_reader :periods,
6
6
  :duration
7
7
 
8
- def initialize(schedule, duration)
8
+ def initialize(periods, duration)
9
9
  unless duration.positive?
10
10
  fail ArgumentError, 'Duration adjustment must be positive.'
11
11
  end
12
12
 
13
- @schedule = schedule
13
+ @periods = periods
14
14
  @duration = duration
15
15
  end
16
16
 
17
17
  def before(time)
18
- schedule.periods.before(time)
18
+ periods.before(time)
19
19
  .timeline.backward
20
20
  .for(duration).to_a
21
21
  .last.start_time
22
22
  end
23
23
 
24
24
  def after(time)
25
- schedule.periods.after(time)
25
+ periods.after(time)
26
26
  .timeline.forward
27
27
  .for(duration).to_a
28
28
  .last.end_time
@@ -8,7 +8,7 @@ module Biz
8
8
  end
9
9
 
10
10
  def intervals
11
- raw.business_hours.flat_map { |weekday, hours|
11
+ raw.hours.flat_map { |weekday, hours|
12
12
  weekday_intervals(weekday, hours)
13
13
  }.sort_by(&:start_time)
14
14
  end
@@ -46,28 +46,30 @@ module Biz
46
46
  memoize :intervals,
47
47
  :holidays
48
48
 
49
- Raw = Struct.new(:business_hours, :holidays, :time_zone) do
49
+ Raw = Struct.new(:hours, :holidays, :time_zone) do
50
50
  module Default
51
51
 
52
- BUSINESS_HOURS = {
52
+ HOURS = {
53
53
  mon: {'09:00' => '17:00'},
54
54
  tue: {'09:00' => '17:00'},
55
55
  wed: {'09:00' => '17:00'},
56
56
  thu: {'09:00' => '17:00'},
57
57
  fri: {'09:00' => '17:00'}
58
58
  }
59
- HOLIDAYS = []
60
- TIME_ZONE = 'Etc/UTC'
59
+ HOLIDAYS = []
60
+ TIME_ZONE = 'Etc/UTC'
61
61
 
62
62
  end
63
63
 
64
64
  def initialize(*)
65
65
  super
66
66
 
67
- self.business_hours ||= Default::BUSINESS_HOURS
68
- self.holidays ||= Default::HOLIDAYS
69
- self.time_zone ||= Default::TIME_ZONE
67
+ self.hours ||= Default::HOURS
68
+ self.holidays ||= Default::HOLIDAYS
69
+ self.time_zone ||= Default::TIME_ZONE
70
70
  end
71
+
72
+ alias_method :business_hours=, :hours=
71
73
  end
72
74
 
73
75
  end
@@ -3,7 +3,7 @@ module Biz
3
3
  module Time
4
4
 
5
5
  def business_hours?
6
- Biz.business_hours?(self)
6
+ Biz.in_hours?(self)
7
7
  end
8
8
 
9
9
  end
@@ -18,17 +18,25 @@ module Biz
18
18
  end
19
19
 
20
20
  def time(scalar, unit)
21
- Calculation::ForDuration.new(self, Duration.with_unit(scalar, unit))
21
+ Calculation::ForDuration.new(
22
+ periods,
23
+ Duration.with_unit(scalar, unit)
24
+ )
22
25
  end
23
26
 
24
27
  def within(origin, terminus)
25
- Calculation::DurationWithin.new(self, TimeSegment.new(origin, terminus))
28
+ Calculation::DurationWithin.new(
29
+ periods,
30
+ TimeSegment.new(origin, terminus)
31
+ )
26
32
  end
27
33
 
28
- def business_hours?(time)
34
+ def in_hours?(time)
29
35
  Calculation::Active.new(self, time).active?
30
36
  end
31
37
 
38
+ alias_method :business_hours?, :in_hours?
39
+
32
40
  protected
33
41
 
34
42
  attr_reader :configuration
@@ -1,5 +1,5 @@
1
1
  module Biz
2
2
 
3
- VERSION = '1.0.0'
3
+ VERSION = '1.1.0'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: biz
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Craig Little
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-02-17 00:00:00.000000000 Z
12
+ date: 2015-02-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: abstract_type
@@ -140,7 +140,7 @@ files:
140
140
  - lib/biz/week_time/start.rb
141
141
  homepage: https://github.com/zendesk/biz
142
142
  licenses:
143
- - Copyright Zendesk. All Rights Reserved.
143
+ - Apache 2.0
144
144
  metadata: {}
145
145
  post_install_message:
146
146
  rdoc_options: []
@@ -150,7 +150,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
150
150
  requirements:
151
151
  - - ">="
152
152
  - !ruby/object:Gem::Version
153
- version: '0'
153
+ version: '2.0'
154
154
  required_rubygems_version: !ruby/object:Gem::Requirement
155
155
  requirements:
156
156
  - - ">="