biz 1.5.1 → 1.5.2

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: eb5d31807277182067ade3e60412dbf0be0e56ac
4
- data.tar.gz: ab55eb5a7aedc372212f52b8a73922ce0b953b9d
3
+ metadata.gz: 27809869df3ee991371e2258e892863d22b5ad2e
4
+ data.tar.gz: 184a044cb47ccdb0437c5227da62bcbde65534aa
5
5
  SHA512:
6
- metadata.gz: 3a3875e89f7ac619d8543b90c60aa4373ff1d8ee9144dc4fbb5edc2ce99c3018f80cd8aa5976cad5e3edc882aa37a4b3ebee9a216226ebf829cf50331590c6fe
7
- data.tar.gz: bae74d39ff90cc519412a36f340b98d86330517d455464cb8b28755ebc2ff4a9222e9e3fc32269291d2b243469774925dd3666a2c0f1e115ff188ca6216597af
6
+ metadata.gz: 5f9fef46fd079453c482963e9e89b8ef9368451275f4626567b923119bf97c68af7cda4d44f73dcae27cf7f458aeabb980a9d6130dcd5c093beade3c32b48df4
7
+ data.tar.gz: f2f7dace5f2eb6b7baff6f6dc275afc7edb606e349ac7cddd7b32273ca3498434e137a1cb6b01ff3bc6d11a2b906baed602a81ac6eb003d1b2b3168b2da86c52
data/lib/biz.rb CHANGED
@@ -31,7 +31,8 @@ module Biz
31
31
  private
32
32
 
33
33
  def schedule
34
- Thread.current[:biz_schedule] or fail 'Biz has not been configured.'
34
+ Thread.current[:biz_schedule] or
35
+ fail Error::Configuration, "#{name} not configured"
35
36
  end
36
37
 
37
38
  end
@@ -11,11 +11,9 @@ module Biz
11
11
  end
12
12
 
13
13
  def self.with_unit(schedule, scalar, unit)
14
- unless UNITS.include?(unit)
15
- fail ArgumentError, 'The unit is not supported.'
16
- end
14
+ fail ArgumentError, 'unsupported unit' unless UNITS.include?(unit)
17
15
 
18
- send(unit, schedule, scalar)
16
+ public_send(unit, schedule, scalar)
19
17
  end
20
18
 
21
19
  def self.unit
@@ -24,7 +22,9 @@ module Biz
24
22
 
25
23
  def initialize(schedule, scalar)
26
24
  @schedule = schedule
27
- @scalar = scalar
25
+ @scalar = Integer(scalar)
26
+
27
+ fail ArgumentError, 'negative scalar' if @scalar < 0
28
28
  end
29
29
 
30
30
  protected
@@ -38,28 +38,40 @@ module Biz
38
38
  self.class.unit
39
39
  end
40
40
 
41
+ def moment_before(time)
42
+ schedule.periods.before(time).first.end_time
43
+ end
44
+
45
+ def moment_after(time)
46
+ schedule.periods.after(time).first.start_time
47
+ end
48
+
41
49
  [
42
50
  *%i[second seconds minute minutes hour hours].map { |unit|
43
51
  const_set(
44
52
  unit.to_s.capitalize,
45
53
  Class.new(self) do
46
54
  def before(time)
47
- timeline(:before, time).last.start_time
55
+ return moment_before(time) if scalar.zero?
56
+
57
+ advanced_periods(:before, time).last.start_time
48
58
  end
49
59
 
50
60
  def after(time)
51
- timeline(:after, time).last.end_time
61
+ return moment_after(time) if scalar.zero?
62
+
63
+ advanced_periods(:after, time).last.end_time
52
64
  end
53
65
 
54
66
  private
55
67
 
56
- def timeline(direction, time)
57
- schedule.periods.send(direction, time).timeline
58
- .for(duration).to_a
59
- end
60
-
61
- def duration
62
- Duration.send(unit, scalar)
68
+ def advanced_periods(direction, time)
69
+ schedule
70
+ .periods
71
+ .public_send(direction, time)
72
+ .timeline
73
+ .for(Duration.public_send(unit, scalar))
74
+ .to_a
63
75
  end
64
76
  end
65
77
  )
@@ -69,33 +81,32 @@ module Biz
69
81
  unit.to_s.capitalize,
70
82
  Class.new(self) do
71
83
  def before(time)
84
+ return moment_before(time) if scalar.zero?
85
+
72
86
  periods(:before, time).first.end_time
73
87
  end
74
88
 
75
89
  def after(time)
90
+ return moment_after(time) if scalar.zero?
91
+
76
92
  periods(:after, time).first.start_time
77
93
  end
78
94
 
79
95
  private
80
96
 
81
97
  def periods(direction, time)
82
- schedule.periods.send(direction, advanced_date(direction, time))
98
+ schedule.periods.public_send(
99
+ direction,
100
+ advanced_time(direction, schedule.in_zone.local(time))
101
+ )
83
102
  end
84
103
 
85
- def advanced_date(direction, time)
104
+ def advanced_time(direction, time)
86
105
  schedule.in_zone.on_date(
87
- schedule.dates.days(scalar).send(direction, local(time)),
88
- day_time(time)
106
+ schedule.dates.days(scalar).public_send(direction, time),
107
+ DayTime.from_time(time)
89
108
  )
90
109
  end
91
-
92
- def day_time(time)
93
- DayTime.from_time(local(time))
94
- end
95
-
96
- def local(time)
97
- schedule.in_zone.local(time)
98
- end
99
110
  end
100
111
  )
101
112
  }
@@ -52,7 +52,7 @@ module Biz
52
52
  @day_second = Integer(day_second)
53
53
 
54
54
  unless VALID_SECONDS.cover?(@day_second)
55
- fail ArgumentError, 'Invalid number of seconds for a day.'
55
+ fail ArgumentError, 'second not within a day'
56
56
  end
57
57
  end
58
58
 
@@ -48,17 +48,8 @@ module Biz
48
48
 
49
49
  def &(other)
50
50
  self.class.new do |config|
51
- config.hours = Interval.to_hours(
52
- intervals.flat_map { |interval|
53
- other
54
- .intervals
55
- .map { |other_interval| interval & other_interval }
56
- .reject(&:empty?)
57
- }
58
- )
59
-
60
- config.holidays = [*holidays, *other.holidays].map(&:to_date)
61
-
51
+ config.hours = Interval.to_hours(intersected_intervals(other))
52
+ config.holidays = [*holidays, *other.holidays].map(&:to_date)
62
53
  config.time_zone = time_zone.name
63
54
  end
64
55
  end
@@ -67,5 +58,16 @@ module Biz
67
58
 
68
59
  attr_reader :configuration
69
60
 
61
+ private
62
+
63
+ def intersected_intervals(other)
64
+ intervals.flat_map { |interval|
65
+ other
66
+ .intervals
67
+ .map { |other_interval| interval & other_interval }
68
+ .reject(&:empty?)
69
+ }
70
+ end
71
+
70
72
  end
71
73
  end
@@ -38,10 +38,10 @@ module Biz
38
38
  end
39
39
 
40
40
  RULES = [
41
- Rule.new('Hours must be hash-like.') { |raw|
41
+ Rule.new('hours not hash-like') { |raw|
42
42
  raw.hours.respond_to?(:to_h)
43
43
  },
44
- Rule.new('Hours must be provided.') { |raw|
44
+ Rule.new('hours not provided') { |raw|
45
45
  raw.hours.to_h.any?
46
46
  }
47
47
  ].freeze
@@ -1,3 +1,3 @@
1
1
  module Biz
2
- VERSION = '1.5.1'.freeze
2
+ VERSION = '1.5.2'.freeze
3
3
  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.5.1
4
+ version: 1.5.2
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: 2016-03-31 00:00:00.000000000 Z
12
+ date: 2016-04-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: clavius