tod 2.0.2 → 2.1.0

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: 95ba589cb9d03ce67ad336aa64c8484a78c3d672
4
- data.tar.gz: 193bf73e7b5a5382d5e71a40822e3d22663f8325
3
+ metadata.gz: 07c87817736be833dd3bc8ce83a55f075cdf79d9
4
+ data.tar.gz: 6eec299d0c6fe1b0c4c9385d555d9bf60e794ae1
5
5
  SHA512:
6
- metadata.gz: 2319de23fd179e408185c1fc0ac6aa9452090b87166f2bd590fd9611a6880f8257cad3eb0b7f3f0d1f789d6861ec99bd6eeaa3484e757823549e65c5e7a3bb9a
7
- data.tar.gz: 53df47600a0139bd3f30a3dd6931385b7850283d875c7988a90784b300b54a568e1e75f466d9dd21af309872ea347c01567dfb2e3c116f8208e53e1f353ed12b
6
+ metadata.gz: ac687afb2c4bf51aeb96143f955d68abcf5c1e4e9d82cd1fbd782131f4fbfa30c1992927f19715ef348fa05c8bb0732dc58a4cae24ea84dd41bc17f5bafbc06c
7
+ data.tar.gz: f1c51e6615f4b1961f5ce874a878a66cd258d30ff3069bd8b3fe3b265412e36273b333286c4ea7ed0ce3046255cbba64e307f4e113e72f06dde63419cdd9934b
data/.gitignore CHANGED
@@ -4,3 +4,4 @@ pkg/*
4
4
  nbproject
5
5
  *.swp
6
6
  .rvmrc
7
+ vendor/
@@ -1,9 +1,8 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.0
4
- - 2.1.5
5
- - 2.0.0
6
- - 1.9.3
3
+ - 2.3.1
4
+ - 2.2.5
5
+ - 2.1.10
7
6
  gemfile:
8
7
  - gemfiles/3.2.gemfile
9
8
  - gemfiles/4.0.gemfile
@@ -1,3 +1,15 @@
1
+ # 2.1.0 (May 9, 2016)
2
+
3
+ * Fix date extensions requiring date (ambirdsall)
4
+ * Add subtraction to TimeOfDay (Hiroki Shirai)
5
+ * Add equality comparison for shifts (Greg Beech)
6
+ * Add arel_extensions for TimeOfDay (Paul Tyng)
7
+ * Support for shifts that span to other days (kennyeni)
8
+
9
+ # 2.0.2 (May 21, 2015)
10
+
11
+ * Fix ActiveRecord serialization when core extensions not loaded
12
+
1
13
  # 2.0.1 (May 8, 2015)
2
14
 
3
15
  * Fix Tod::TimeOfDay() without core extensions
@@ -1,31 +1,31 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tod (2.0.2)
4
+ tod (2.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- activemodel (4.2.0)
10
- activesupport (= 4.2.0)
9
+ activemodel (4.2.3)
10
+ activesupport (= 4.2.3)
11
11
  builder (~> 3.1)
12
- activerecord (4.2.0)
13
- activemodel (= 4.2.0)
14
- activesupport (= 4.2.0)
12
+ activerecord (4.2.3)
13
+ activemodel (= 4.2.3)
14
+ activesupport (= 4.2.3)
15
15
  arel (~> 6.0)
16
- activesupport (4.2.0)
16
+ activesupport (4.2.3)
17
17
  i18n (~> 0.7)
18
18
  json (~> 1.7, >= 1.7.7)
19
19
  minitest (~> 5.1)
20
20
  thread_safe (~> 0.3, >= 0.3.4)
21
21
  tzinfo (~> 1.1)
22
- arel (6.0.0)
22
+ arel (6.0.2)
23
23
  builder (3.2.2)
24
24
  coderay (1.1.0)
25
25
  i18n (0.7.0)
26
- json (1.8.2)
26
+ json (1.8.3)
27
27
  method_source (0.8.2)
28
- minitest (5.5.1)
28
+ minitest (5.7.0)
29
29
  pry (0.10.1)
30
30
  coderay (~> 1.1.0)
31
31
  method_source (~> 0.8.1)
@@ -33,7 +33,7 @@ GEM
33
33
  rake (10.4.2)
34
34
  slop (3.6.0)
35
35
  sqlite3 (1.3.10)
36
- thread_safe (0.3.4)
36
+ thread_safe (0.3.5)
37
37
  tzinfo (1.2.2)
38
38
  thread_safe (~> 0.1)
39
39
 
@@ -42,9 +42,13 @@ PLATFORMS
42
42
 
43
43
  DEPENDENCIES
44
44
  activerecord (>= 3.0.0)
45
+ arel
45
46
  minitest
46
47
  pry
47
48
  rake
48
49
  sqlite3
49
50
  tod!
50
51
  tzinfo
52
+
53
+ BUNDLED WITH
54
+ 1.11.2
@@ -0,0 +1,25 @@
1
+ require 'arel'
2
+
3
+ module Tod
4
+ module ArelDepthFirstExtensions
5
+ def self.included(base)
6
+ base.send :alias_method, :visit_Tod_TimeOfDay, :terminal
7
+ end
8
+ end
9
+
10
+ module ArelDotExtensions
11
+ def self.included(base)
12
+ base.send :alias_method, :visit_Tod_TimeOfDay, :visit_String
13
+ end
14
+ end
15
+
16
+ module ArelToSqlExtensions
17
+ def visit_Tod_TimeOfDay(o, collector=nil)
18
+ quote Tod::TimeOfDay.dump(o)
19
+ end
20
+ end
21
+ end
22
+
23
+ Arel::Visitors::DepthFirst.send :include, Tod::ArelDepthFirstExtensions
24
+ Arel::Visitors::Dot.send :include, Tod::ArelDotExtensions
25
+ Arel::Visitors::ToSql.send :include, Tod::ArelToSqlExtensions
@@ -1,3 +1,5 @@
1
+ require 'date'
2
+
1
3
  module Tod
2
4
  module DateExtensions
3
5
  # Returns a local Time instance with this date and time_of_day
@@ -33,9 +33,20 @@ module Tod
33
33
 
34
34
  # Returns true if ranges overlap, false otherwise.
35
35
  def overlaps?(other)
36
+ max_seconds = TimeOfDay::NUM_SECONDS_IN_DAY
37
+
38
+ # Standard case, when Shifts are on the same day
36
39
  a, b = [self, other].map(&:range).sort_by(&:first)
37
40
  op = a.exclude_end? ? :> : :>=
38
- a.last.send(op, b.first)
41
+ return true if a.last.send(op, b.first)
42
+
43
+ # Special cases, when Shifts span to the next day
44
+ return false if (a.last < max_seconds) && (b.last < max_seconds)
45
+
46
+ a = Range.new(a.first, a.last - max_seconds, a.exclude_end?) if a.last > max_seconds
47
+ b = Range.new(b.first, b.last - max_seconds, b.exclude_end?) if b.last > max_seconds
48
+ a, b = [a, b].sort_by(&:last)
49
+ b.last.send(op, a.last) && a.last.send(op, b.first)
39
50
  end
40
51
 
41
52
  def contains?(shift)
@@ -51,5 +62,22 @@ module Tod
51
62
  def exclude_end?
52
63
  @exclude_end
53
64
  end
65
+
66
+ def ==(other)
67
+ @range == other.range
68
+ end
69
+
70
+ def eql?(other)
71
+ @range.eql?(other.range)
72
+ end
73
+
74
+ def hash
75
+ @range.hash
76
+ end
77
+
78
+ # Move start and end by a number of seconds and return new shift.
79
+ def slide(seconds)
80
+ self.class.new(beginning + seconds, ending + seconds, exclude_end?)
81
+ end
54
82
  end
55
83
  end
@@ -78,8 +78,12 @@ module Tod
78
78
 
79
79
  # Return a new TimeOfDay num_seconds less than self. It will wrap around
80
80
  # at midnight.
81
- def -(num_seconds)
82
- TimeOfDay.from_second_of_day @second_of_day - num_seconds
81
+ def -(other)
82
+ if other.instance_of?(TimeOfDay)
83
+ TimeOfDay.from_second_of_day @second_of_day - other.second_of_day
84
+ else
85
+ TimeOfDay.from_second_of_day @second_of_day - other
86
+ end
83
87
  end
84
88
 
85
89
  # Returns a Time instance on date using self as the time of day
@@ -1,3 +1,3 @@
1
1
  module Tod
2
- VERSION = "2.0.2"
2
+ VERSION = "2.1.0"
3
3
  end
@@ -4,6 +4,7 @@ require 'bundler/setup'
4
4
  require 'active_support/time'
5
5
  require 'tod'
6
6
  require 'tod/core_extensions'
7
+ require 'tod/arel_extensions'
7
8
  require 'minitest/autorun'
8
9
  require 'pry'
9
10
 
@@ -43,12 +43,49 @@ describe "Shift" do
43
43
  shift1 = Tod::Shift.new(Tod::TimeOfDay.new(12), Tod::TimeOfDay.new(18))
44
44
  shift2 = Tod::Shift.new(Tod::TimeOfDay.new(13), Tod::TimeOfDay.new(15))
45
45
  assert shift1.overlaps?(shift2)
46
+
47
+ # Additional Testing for Shifts that span from one day to another
48
+ cases = [
49
+ [5, 8, 7, 2],
50
+ [7, 2, 1, 8],
51
+ [7, 2, 5, 8],
52
+ [4, 8, 1, 5],
53
+ [1, 5, 4, 8],
54
+ [7, 2, 1, 4],
55
+ [1, 4, 7, 2],
56
+ [1, 4, 3, 2],
57
+ [5, 8, 7, 2],
58
+ [7, 2, 8, 3],
59
+ [7, 2, 6, 3],
60
+ [7, 2, 1, 8]
61
+ ]
62
+
63
+ cases.each do |c|
64
+ shift1 = Tod::Shift.new(Tod::TimeOfDay.new(c[0]), Tod::TimeOfDay.new(c[1]))
65
+ shift2 = Tod::Shift.new(Tod::TimeOfDay.new(c[2]), Tod::TimeOfDay.new(c[3]))
66
+ assert shift1.overlaps?(shift2), "Failed with args: #{c}"
67
+ end
46
68
  end
47
69
 
48
70
  it "is false when shifts don't overlap" do
49
71
  shift1 = Tod::Shift.new(Tod::TimeOfDay.new(1), Tod::TimeOfDay.new(5))
50
72
  shift2 = Tod::Shift.new(Tod::TimeOfDay.new(9), Tod::TimeOfDay.new(12))
51
73
  refute shift1.overlaps?(shift2)
74
+
75
+ # Additional Testing for Shifts that span from one day to another
76
+ cases = [
77
+ [7, 8, 1, 5],
78
+ [1, 5, 7, 8],
79
+ [7, 2, 3, 4],
80
+ [3, 4, 5, 2],
81
+ [1, 5, 9, 12]
82
+ ]
83
+
84
+ cases.each do |c|
85
+ shift1 = Tod::Shift.new(Tod::TimeOfDay.new(c[0]), Tod::TimeOfDay.new(c[1]))
86
+ shift2 = Tod::Shift.new(Tod::TimeOfDay.new(c[2]), Tod::TimeOfDay.new(c[3]))
87
+ refute shift1.overlaps?(shift2), "Failed with args: #{c}"
88
+ end
52
89
  end
53
90
 
54
91
  it "is true when shifts touch with inclusive end" do
@@ -166,4 +203,153 @@ describe "Shift" do
166
203
  assert shift.include?(value)
167
204
  end
168
205
  end
206
+
207
+ describe "#==" do
208
+ it "is true when the beginning time, end time, and exclude end are the same" do
209
+ tod1 = Tod::TimeOfDay.new 8,30
210
+ tod2 = Tod::TimeOfDay.new 13,00,30
211
+ shift1 = Tod::Shift.new tod1, tod2
212
+ shift2 = Tod::Shift.new tod1, tod2
213
+ assert shift1 == shift2
214
+ end
215
+
216
+ it "is false when the beginning time is different" do
217
+ tod1 = Tod::TimeOfDay.new 8,30
218
+ tod2 = Tod::TimeOfDay.new 13,00,30
219
+ shift1 = Tod::Shift.new tod1, tod2
220
+ shift2 = Tod::Shift.new tod1, Tod::TimeOfDay.new(14,00)
221
+ assert !(shift1 == shift2)
222
+ end
223
+
224
+ it "is false when the ending time is different" do
225
+ tod1 = Tod::TimeOfDay.new 8,30
226
+ tod2 = Tod::TimeOfDay.new 13,00,30
227
+ shift1 = Tod::Shift.new tod1, tod2
228
+ shift2 = Tod::Shift.new Tod::TimeOfDay.new(9,30), tod2
229
+ assert !(shift1 == shift2)
230
+ end
231
+
232
+ it "is false when exclude end is different" do
233
+ tod1 = Tod::TimeOfDay.new 8,30
234
+ tod2 = Tod::TimeOfDay.new 13,00,30
235
+ shift1 = Tod::Shift.new tod1, tod2
236
+ shift2 = Tod::Shift.new tod1, tod2, true
237
+ assert !(shift1 == shift2)
238
+ end
239
+ end
240
+
241
+ describe "#eql?" do
242
+ it "is true when the beginning time, end time, and exclude end are the same" do
243
+ tod1 = Tod::TimeOfDay.new 8,30
244
+ tod2 = Tod::TimeOfDay.new 13,00,30
245
+ shift1 = Tod::Shift.new tod1, tod2
246
+ shift2 = Tod::Shift.new tod1, tod2
247
+ assert shift1.eql?(shift2)
248
+ end
249
+
250
+ it "is false when the beginning time is different" do
251
+ tod1 = Tod::TimeOfDay.new 8,30
252
+ tod2 = Tod::TimeOfDay.new 13,00,30
253
+ shift1 = Tod::Shift.new tod1, tod2
254
+ shift2 = Tod::Shift.new tod1, Tod::TimeOfDay.new(14,00)
255
+ assert !shift1.eql?(shift2)
256
+ end
257
+
258
+ it "is false when the ending time is different" do
259
+ tod1 = Tod::TimeOfDay.new 8,30
260
+ tod2 = Tod::TimeOfDay.new 13,00,30
261
+ shift1 = Tod::Shift.new tod1, tod2
262
+ shift2 = Tod::Shift.new Tod::TimeOfDay.new(9,30), tod2
263
+ assert !shift1.eql?(shift2)
264
+ end
265
+
266
+ it "is false when exclude end is different" do
267
+ tod1 = Tod::TimeOfDay.new 8,30
268
+ tod2 = Tod::TimeOfDay.new 13,00,30
269
+ shift1 = Tod::Shift.new tod1, tod2
270
+ shift2 = Tod::Shift.new tod1, tod2, true
271
+ assert !shift1.eql?(shift2)
272
+ end
273
+ end
274
+
275
+ describe "#hash" do
276
+ it "is the same when the beginning time, end time, and exclude end are the same" do
277
+ tod1 = Tod::TimeOfDay.new 8,30
278
+ tod2 = Tod::TimeOfDay.new 13,00,30
279
+ shift1 = Tod::Shift.new tod1, tod2
280
+ shift2 = Tod::Shift.new tod1, tod2
281
+ assert_equal shift1.hash, shift2.hash
282
+ end
283
+
284
+ it "is usually different when the beginning time is different" do
285
+ tod1 = Tod::TimeOfDay.new 8,30
286
+ tod2 = Tod::TimeOfDay.new 13,00,30
287
+ shift1 = Tod::Shift.new tod1, tod2
288
+ shift2 = Tod::Shift.new tod1, Tod::TimeOfDay.new(14,00)
289
+ assert shift1.hash != shift2.hash
290
+ end
291
+
292
+ it "is usually different when the ending time is different" do
293
+ tod1 = Tod::TimeOfDay.new 8,30
294
+ tod2 = Tod::TimeOfDay.new 13,00,30
295
+ shift1 = Tod::Shift.new tod1, tod2
296
+ shift2 = Tod::Shift.new Tod::TimeOfDay.new(9,30), tod2
297
+ assert shift1.hash != shift2.hash
298
+ end
299
+
300
+ it "is usually different when exclude end is different" do
301
+ tod1 = Tod::TimeOfDay.new 8,30
302
+ tod2 = Tod::TimeOfDay.new 13,00,30
303
+ shift1 = Tod::Shift.new tod1, tod2
304
+ shift2 = Tod::Shift.new tod1, tod2, true
305
+ assert shift1.hash != shift2.hash
306
+ end
307
+ end
308
+
309
+ describe "slide" do
310
+ it "handles positive numbers" do
311
+ slide = 30 * 60 # 30 minutes in seconds
312
+
313
+ beginning_expected = Tod::TimeOfDay.new 10, 30
314
+ ending_expected = Tod::TimeOfDay.new 16, 30
315
+
316
+ beginning = Tod::TimeOfDay.new 10
317
+ ending = Tod::TimeOfDay.new 16
318
+ shift = Tod::Shift.new(beginning, ending, false).slide(slide)
319
+
320
+ assert_equal beginning_expected, shift.beginning
321
+ assert_equal ending_expected, shift.ending
322
+ refute shift.exclude_end?
323
+ end
324
+
325
+ it "handles negative numbers" do
326
+ slide = -30 * 60 # -30 minutes in seconds
327
+
328
+ beginning_expected = Tod::TimeOfDay.new 9, 30
329
+ ending_expected = Tod::TimeOfDay.new 15, 30
330
+
331
+ beginning = Tod::TimeOfDay.new 10
332
+ ending = Tod::TimeOfDay.new 16
333
+ shift = Tod::Shift.new(beginning, ending, false).slide(slide)
334
+
335
+ assert_equal beginning_expected, shift.beginning
336
+ assert_equal ending_expected, shift.ending
337
+ refute shift.exclude_end?
338
+ end
339
+
340
+ it "handles ActiveSupport::Duration" do
341
+ slide = 30.minutes
342
+
343
+ beginning_expected = Tod::TimeOfDay.new 10, 30
344
+ ending_expected = Tod::TimeOfDay.new 16, 30
345
+
346
+ beginning = Tod::TimeOfDay.new 10
347
+ ending = Tod::TimeOfDay.new 16
348
+ shift = Tod::Shift.new(beginning, ending, false).slide(slide)
349
+
350
+ assert_equal beginning_expected, shift.beginning
351
+ assert_equal ending_expected, shift.ending
352
+ refute shift.exclude_end?
353
+ end
354
+ end
169
355
  end
@@ -30,4 +30,13 @@ describe "TimeOfDay with ActiveRecord Serializable Attribute" do
30
30
  assert_equal order.time, nil
31
31
  end
32
32
  end
33
+
34
+ describe "Order.where" do
35
+ it "handles TimeOfDay as a parameter" do
36
+ tod = Tod::TimeOfDay.new(11, 11)
37
+ expected = Order.create!(time: tod)
38
+ actual = Order.where(time: tod).first
39
+ assert_equal expected, actual
40
+ end
41
+ end
33
42
  end
@@ -151,6 +151,12 @@ describe "TimeOfDay" do
151
151
  result = original + 15
152
152
  refute_equal original.object_id, result.object_id
153
153
  end
154
+
155
+ it "handles ActiveSupport::Duration" do
156
+ original = Tod::TimeOfDay.new(8,0,0)
157
+ result = original + 10.minutes
158
+ assert_equal Tod::TimeOfDay.new(8,10,0), result
159
+ end
154
160
  end
155
161
 
156
162
  describe "subtraction" do
@@ -171,6 +177,19 @@ describe "TimeOfDay" do
171
177
  result = original - 15
172
178
  refute_equal original.object_id, result.object_id
173
179
  end
180
+
181
+ it "handles ActiveSupport::Duration" do
182
+ original = Tod::TimeOfDay.new(8,0,0)
183
+ result = original - 10.minutes
184
+ assert_equal Tod::TimeOfDay.new(7,50,0), result
185
+ end
186
+
187
+ it "subtracts Tod::TimeOfDay object" do
188
+ right_side = Tod::TimeOfDay.new(10,0,0)
189
+ left_side = Tod::TimeOfDay.new(12,0,30)
190
+ result = right_side - left_side
191
+ assert_equal Tod::TimeOfDay.new(21,59,30), result
192
+ end
174
193
  end
175
194
 
176
195
  describe "from_second_of_day" do
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.add_development_dependency "activerecord", ">= 3.0.0"
20
20
  s.add_development_dependency "sqlite3"
21
21
  s.add_development_dependency "pry"
22
+ s.add_development_dependency "arel"
22
23
 
23
24
  s.files = `git ls-files`.split("\n")
24
25
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tod
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Christensen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-21 00:00:00.000000000 Z
11
+ date: 2016-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: arel
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  description: Supplies TimeOfDay and Shift class that includes parsing, strftime, comparison,
98
112
  and arithmetic.
99
113
  email:
@@ -115,6 +129,7 @@ files:
115
129
  - gemfiles/4.1.gemfile
116
130
  - gemfiles/4.2.gemfile
117
131
  - lib/tod.rb
132
+ - lib/tod/arel_extensions.rb
118
133
  - lib/tod/conversions.rb
119
134
  - lib/tod/core_extensions.rb
120
135
  - lib/tod/date_extensions.rb
@@ -153,8 +168,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
168
  version: '0'
154
169
  requirements: []
155
170
  rubyforge_project:
156
- rubygems_version: 2.4.5
171
+ rubygems_version: 2.5.1
157
172
  signing_key:
158
173
  specification_version: 4
159
174
  summary: Supplies TimeOfDay and Shift class
160
- test_files: []
175
+ test_files:
176
+ - test/support/active_record.rb
177
+ - test/test_helper.rb
178
+ - test/tod/conversion_test.rb
179
+ - test/tod/date_test.rb
180
+ - test/tod/shift_test.rb
181
+ - test/tod/time_of_day_serializable_attribute_test.rb
182
+ - test/tod/time_of_day_test.rb
183
+ - test/tod/time_of_day_time_zone_with_active_support_test.rb
184
+ - test/tod/time_test.rb