tod 3.0.0 → 3.1.1

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
  SHA256:
3
- metadata.gz: 8986b140f4dcc736d253b188cd5f9a46a72110f38887cd6e8ff93ecee823c438
4
- data.tar.gz: 95d31c83abc1264946bf2ce6aa6ba6fac45dcaeca1362b76a22d54eab27f402f
3
+ metadata.gz: 9abbaa919b6c5095de1d85a9e4db5da1160d4d80038515366502c2144d007b75
4
+ data.tar.gz: 87106d404f688c2ba9997b18874172440f34f7bcacfe35f41c693309693c7654
5
5
  SHA512:
6
- metadata.gz: 2d1c3b50deb1df502928666b38e113fb7d58b4a25c140119bac8155ad3ff3add0689cffba810b7bdd6ab1461493838873a839ac259895630210e47679ae9ef89
7
- data.tar.gz: 98a76ddcb2b8c866f602efe1def27c236b88ffcc6bbb12812580794113dc964d5b6df1bd95b0f055a48e9e95ca4b69ccf3dea93b20f866ca72be7932596e1aaf
6
+ metadata.gz: a890e3dc2b7aee24401388d8d85214c01e0f730c0547a61f7c46d9a097dce842fccf2a43836d46e52af3eba87f1df2c9aa2bd13fe6f10f5e780b085e43b9741d
7
+ data.tar.gz: f6bdb357ba0c50aaba4b30f84038378a5418112776452832b2bfa8a53313975c88ca7caea2cdc8f4701acc7206b55dab19dfd08a3eb32214e3cf53f0fa771760
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ # 3.1.1 (June 11, 2023)
2
+
3
+ * Allow use in Rails forms with time_select (Bill Pearce)
4
+ * Prevent error when comparing a range to nil (Chris Northwood)
5
+
6
+ # 3.1.0 (December 20, 2021)
7
+
8
+ * Allow / ignore parsing milliseconds (Eli Fatsi)
9
+ * Improve TimeOfDate#inspect and Shift#inspect (zverok)
10
+ * Fix Shift#contains? with exclusive ending (nazamoresco)
11
+
1
12
  # 3.0.0 (March 6, 2021)
2
13
 
3
14
  * Support and require Rails 6
data/Gemfile.lock CHANGED
@@ -1,40 +1,40 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tod (3.0.0)
4
+ tod (3.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- activemodel (6.1.3)
10
- activesupport (= 6.1.3)
11
- activerecord (6.1.3)
12
- activemodel (= 6.1.3)
13
- activesupport (= 6.1.3)
14
- activesupport (6.1.3)
9
+ activemodel (7.0.5)
10
+ activesupport (= 7.0.5)
11
+ activerecord (7.0.5)
12
+ activemodel (= 7.0.5)
13
+ activesupport (= 7.0.5)
14
+ activesupport (7.0.5)
15
15
  concurrent-ruby (~> 1.0, >= 1.0.2)
16
16
  i18n (>= 1.6, < 2)
17
17
  minitest (>= 5.1)
18
18
  tzinfo (~> 2.0)
19
- zeitwerk (~> 2.3)
20
19
  byebug (11.1.3)
21
20
  coderay (1.1.3)
22
- concurrent-ruby (1.1.8)
23
- i18n (1.8.9)
21
+ concurrent-ruby (1.2.2)
22
+ i18n (1.14.1)
24
23
  concurrent-ruby (~> 1.0)
25
24
  method_source (1.0.0)
26
- minitest (5.14.4)
27
- pry (0.13.1)
25
+ mini_portile2 (2.8.2)
26
+ minitest (5.18.0)
27
+ pry (0.14.2)
28
28
  coderay (~> 1.1)
29
29
  method_source (~> 1.0)
30
- pry-byebug (3.9.0)
30
+ pry-byebug (3.10.1)
31
31
  byebug (~> 11.0)
32
- pry (~> 0.13.0)
33
- rake (13.0.3)
34
- sqlite3 (1.4.2)
35
- tzinfo (2.0.4)
32
+ pry (>= 0.13, < 0.15)
33
+ rake (13.0.6)
34
+ sqlite3 (1.6.3)
35
+ mini_portile2 (~> 2.8.0)
36
+ tzinfo (2.0.6)
36
37
  concurrent-ruby (~> 1.0)
37
- zeitwerk (2.4.2)
38
38
 
39
39
  PLATFORMS
40
40
  ruby
@@ -49,4 +49,4 @@ DEPENDENCIES
49
49
  tzinfo
50
50
 
51
51
  BUNDLED WITH
52
- 2.0.2
52
+ 2.2.15
data/README.markdown CHANGED
@@ -182,7 +182,7 @@ Include?
182
182
  Tod::Shift.new(Tod::TimeOfDay.new(5), Tod::TimeOfDay.new(9), true).include?(Tod::TimeOfDay.new(9)) # => false
183
183
 
184
184
 
185
- Overlap?
185
+ Overlaps?
186
186
  --------------------
187
187
 
188
188
  breakfast = Tod::Shift.new(Tod::TimeOfDay.new(8), Tod::TimeOfDay.new(11))
@@ -201,8 +201,8 @@ Overlap?
201
201
 
202
202
  Contains?
203
203
  --------------------
204
- workday = Shift.new(TimeOfDay.new(9), TimeOfDay.new(17))
205
- lunch = Shift.new(TimeOfDay.new(10), TimeOfDay.new(14))
204
+ workday = Tod::Shift.new(Tod::TimeOfDay.new(9), Tod::TimeOfDay.new(17))
205
+ lunch = Tod::Shift.new(Tod::TimeOfDay.new(10), Tod::TimeOfDay.new(14))
206
206
  workday.contains?(lunch) # => true
207
207
  lunch.contains?(workday) # => false
208
208
 
@@ -234,6 +234,9 @@ in a column of the time type.
234
234
  Example:
235
235
 
236
236
  ```ruby
237
+ ActiveModel::Type.register(:time_only, Tod::TimeOfDayType)
238
+ ActiveRecord::Type.register(:time_only, Tod::TimeOfDayType)
239
+
237
240
  class Order < ActiveRecord::Base
238
241
  attribute :time, :time_only
239
242
  end
@@ -241,6 +244,18 @@ order = Order.create(time: Tod::TimeOfDay.new(9,30))
241
244
  order.time # => 09:30:00
242
245
  ```
243
246
 
247
+ In Rails, this can be used with `time_select` in forms like so:
248
+
249
+ ```ruby
250
+ f.time_select :time, ignore_date: true
251
+ ```
252
+
253
+ Or with **simple_form**:
254
+
255
+ ```ruby
256
+ f.input :time, as: :time, ignore_date: true
257
+ ```
258
+
244
259
  MongoDB Support
245
260
  ===============
246
261
 
data/lib/tod/shift.rb CHANGED
@@ -24,6 +24,10 @@ module Tod
24
24
  freeze # Shift instances are value objects
25
25
  end
26
26
 
27
+ def inspect
28
+ "#<#{self.class} #{beginning}#{exclude_end? ? '...' : '..'}#{ending}>"
29
+ end
30
+
27
31
  # Returns true if the time of day is inside the shift, false otherwise.
28
32
  def include?(tod)
29
33
  second = tod.to_i
@@ -70,7 +74,7 @@ module Tod
70
74
  end
71
75
 
72
76
  def contains?(shift)
73
- self.include?(shift.beginning) && self.include?(shift.ending)
77
+ self.include?(shift.beginning) && self.include?(shift.inclusive_ending)
74
78
  end
75
79
 
76
80
  # Return shift duration in seconds.
@@ -84,11 +88,11 @@ module Tod
84
88
  end
85
89
 
86
90
  def ==(other)
87
- @range == other.range
91
+ !other.nil? && @range == other.range
88
92
  end
89
93
 
90
94
  def eql?(other)
91
- @range.eql?(other.range)
95
+ !other.nil? && @range.eql?(other.range)
92
96
  end
93
97
 
94
98
  def hash
@@ -99,5 +103,12 @@ module Tod
99
103
  def slide(seconds)
100
104
  self.class.new(beginning + seconds, ending + seconds, exclude_end?)
101
105
  end
106
+
107
+ protected
108
+
109
+ # If exclusive ending returns equivalent ending but inclusive
110
+ def inclusive_ending
111
+ self.exclude_end? ? ending - 1 : ending
112
+ end
102
113
  end
103
114
  end
@@ -14,6 +14,7 @@ module Tod
14
14
  ([0-5]\d)?
15
15
  :?
16
16
  ([0-5]\d)?
17
+ (?:\.\d{3})?
17
18
  \z
18
19
  /x
19
20
 
@@ -24,6 +25,7 @@ module Tod
24
25
  ([0-5]\d)?
25
26
  :?
26
27
  ([0-5]\d)?
28
+ (?:\.\d{3})?
27
29
  \s*
28
30
  ([ap])
29
31
  \.?
@@ -110,6 +112,10 @@ module Tod
110
112
  to_s
111
113
  end
112
114
 
115
+ def inspect
116
+ "#<#{self.class} #{self}>"
117
+ end
118
+
113
119
  # Return a new TimeOfDay num_seconds greater than self. It will wrap around
114
120
  # at midnight.
115
121
  def +(num_seconds)
@@ -4,7 +4,7 @@ module Tod
4
4
  if value.is_a? Hash
5
5
  # rails multiparam attribute
6
6
  # get hour, minute and second and construct new TimeOfDay object
7
- ::Tod::TimeOfDay.new(value[4], value[5], value[6])
7
+ ::Tod::TimeOfDay.new(value[4], value[5], value[6] || 0)
8
8
  else
9
9
  # return nil, if input is not parsable
10
10
  Tod::TimeOfDay(value){}
data/lib/tod/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tod
2
- VERSION = "3.0.0"
2
+ VERSION = "3.1.1"
3
3
  end
data/test/test_helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  $LOAD_PATH.unshift File.join(File.dirname(__FILE__),'..','lib')
2
2
 
3
3
  require 'bundler/setup'
4
+ require 'active_support'
4
5
  require 'active_support/time'
5
6
  require 'tod'
6
7
  require 'tod/core_extensions'
@@ -88,4 +88,18 @@ describe "TimeOfDay()" do
88
88
 
89
89
  assert_equal(tod, Tod::TimeOfDay.new(24, 00, 00))
90
90
  end
91
+
92
+ it "parses 24:00:00.000" do
93
+ t = "24:00:00.000"
94
+ tod = Tod::TimeOfDay(t)
95
+
96
+ assert_equal(tod, Tod::TimeOfDay.new(24, 00, 00))
97
+ end
98
+
99
+ it "parses 04:00:00.000 pm" do
100
+ t = "04:00:00.000 pm"
101
+ tod = Tod::TimeOfDay(t)
102
+
103
+ assert_equal(tod, Tod::TimeOfDay.new(16, 00, 00))
104
+ end
91
105
  end
@@ -11,6 +11,16 @@ describe "Shift" do
11
11
  end
12
12
  end
13
13
 
14
+ describe "inspect" do
15
+ it "provides friendly description" do
16
+ shift = Tod::Shift.new Tod::TimeOfDay.new(8), Tod::TimeOfDay.new(10), false
17
+ assert_equal "#<Tod::Shift 08:00:00..10:00:00>", shift.inspect
18
+
19
+ shift = Tod::Shift.new Tod::TimeOfDay.new(8), Tod::TimeOfDay.new(10), true
20
+ assert_equal "#<Tod::Shift 08:00:00...10:00:00>", shift.inspect
21
+ end
22
+ end
23
+
14
24
  describe "#duration" do
15
25
  it "returns correct duration when first time is lower than the second one" do
16
26
  duration_expected = 4 * 60 * 60 + 30 * 60 + 30 # 4 hours, 30 min and 30 sec later
@@ -162,6 +172,12 @@ describe "Shift" do
162
172
  shift2 = Tod::Shift.new(Tod::TimeOfDay.new(18), Tod::TimeOfDay.new(19))
163
173
  refute shift1.contains?(shift2)
164
174
  end
175
+
176
+ it "is true when beginning is included and exclusive endings match" do
177
+ inside = Tod::Shift.new(Tod::TimeOfDay.new(13), Tod::TimeOfDay.new(17), true)
178
+ outside = Tod::Shift.new(Tod::TimeOfDay.new(10), Tod::TimeOfDay.new(17), true)
179
+ assert outside.contains?(inside)
180
+ end
165
181
  end
166
182
 
167
183
  describe "#include?" do
@@ -279,6 +295,13 @@ describe "Shift" do
279
295
  shift2 = Tod::Shift.new tod1, tod2, true
280
296
  assert !(shift1 == shift2)
281
297
  end
298
+
299
+ it "is false when one side is nil" do
300
+ tod1 = Tod::TimeOfDay.new 8,30
301
+ tod2 = Tod::TimeOfDay.new 13,00,30
302
+ shift = Tod::Shift.new tod1, tod2
303
+ assert !(shift == nil)
304
+ end
282
305
  end
283
306
 
284
307
  describe "#eql?" do
@@ -313,6 +336,13 @@ describe "Shift" do
313
336
  shift2 = Tod::Shift.new tod1, tod2, true
314
337
  assert !shift1.eql?(shift2)
315
338
  end
339
+
340
+ it "is false when one side is nil" do
341
+ tod1 = Tod::TimeOfDay.new 8,30
342
+ tod2 = Tod::TimeOfDay.new 13,00,30
343
+ shift = Tod::Shift.new tod1, tod2
344
+ assert !shift.eql?(nil )
345
+ end
316
346
  end
317
347
 
318
348
  describe "#hash" do
@@ -24,6 +24,11 @@ describe "TimeOfDay with ActiveRecord Attribute" do
24
24
  assert_equal Tod::TimeOfDay.new(8,6,5), order.time
25
25
  end
26
26
 
27
+ it "works with multiparam time arguments without seconds" do
28
+ order = Order.create!({"time(4i)" => "8", "time(5i)" => "6"})
29
+ assert_equal Tod::TimeOfDay.new(8,6,0), order.time
30
+ end
31
+
27
32
  it "should not raise Exception on access of unparsable values" do
28
33
  order = Order.new(time: 'unparsable')
29
34
  order.time
@@ -190,6 +190,13 @@ describe "TimeOfDay" do
190
190
  end
191
191
  end
192
192
 
193
+ describe "inspect" do
194
+ it "is friendly representation" do
195
+ t = Tod::TimeOfDay.new(12,15,05)
196
+ assert_equal "#<Tod::TimeOfDay 12:15:05>", t.inspect
197
+ end
198
+ end
199
+
193
200
  describe "to_i" do
194
201
  it "formats to integer" do
195
202
  assert_equal 29730, Tod::TimeOfDay.new(8,15,30).to_i
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: 3.0.0
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Christensen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-06 00:00:00.000000000 Z
11
+ date: 2023-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -150,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
150
  - !ruby/object:Gem::Version
151
151
  version: '0'
152
152
  requirements: []
153
- rubygems_version: 3.0.6
153
+ rubygems_version: 3.4.1
154
154
  signing_key:
155
155
  specification_version: 4
156
156
  summary: Supplies TimeOfDay and Shift class