subroutine 3.0.3 → 3.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/subroutine/type_caster.rb +18 -5
- data/lib/subroutine/version.rb +1 -1
- data/test/subroutine/type_caster_test.rb +122 -5
- data/test/support/ops.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb0e95ecb87d3b848cbc99714e68405ead97c7c82b2ea7e80b49925502a15790
|
4
|
+
data.tar.gz: dfdae7d9a7a7b227eb35cecc53334412c0d27259f7b8f3bc5a83cf8e402faebd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b0e0f0d68c428bbed21dd09adc7b98890281fc61b69a6a04cebdb34848a8da9db9ff2cdabf9a5257741a5e1d13592cadfe056f3d4fd274fe74aa9bcac830b439
|
7
|
+
data.tar.gz: 046725136d9300e595dcfd0f83fd6fd9b1271b28bdb213bee2f7a9504c8291841c2c35c48abe13d2e0b0eb137698c0c42e553836099dfd5b61c24f51d41c77ff
|
@@ -4,11 +4,14 @@ require 'date'
|
|
4
4
|
require 'time'
|
5
5
|
require 'bigdecimal'
|
6
6
|
require 'securerandom'
|
7
|
+
require 'active_support/core_ext/date_time/acts_like'
|
8
|
+
require 'active_support/core_ext/date_time/calculations'
|
7
9
|
require 'active_support/core_ext/object/acts_like'
|
8
10
|
require 'active_support/core_ext/object/blank'
|
9
11
|
require 'active_support/core_ext/object/try'
|
10
12
|
require 'active_support/core_ext/array/wrap'
|
11
13
|
require 'active_support/core_ext/time/acts_like'
|
14
|
+
require 'active_support/core_ext/time/calculations'
|
12
15
|
|
13
16
|
module Subroutine
|
14
17
|
module TypeCaster
|
@@ -117,13 +120,23 @@ end
|
|
117
120
|
::Date.parse(String(value))
|
118
121
|
end
|
119
122
|
|
120
|
-
::Subroutine::TypeCaster.register :time, :timestamp, :datetime do |value,
|
123
|
+
::Subroutine::TypeCaster.register :time, :timestamp, :datetime do |value, options = {}|
|
121
124
|
next nil unless value.present?
|
122
125
|
|
123
|
-
if
|
124
|
-
value
|
125
|
-
|
126
|
-
|
126
|
+
if options[:precision] == :high
|
127
|
+
if value.try(:acts_like?, :time)
|
128
|
+
value.to_time
|
129
|
+
else
|
130
|
+
::Time.parse(String(value))
|
131
|
+
end
|
132
|
+
else # precision == :seconds
|
133
|
+
time = if value.try(:acts_like?, :time)
|
134
|
+
value.to_time
|
135
|
+
else
|
136
|
+
::Time.parse(String(value))
|
137
|
+
end
|
138
|
+
|
139
|
+
time.change(usec: 0)
|
127
140
|
end
|
128
141
|
end
|
129
142
|
|
data/lib/subroutine/version.rb
CHANGED
@@ -241,7 +241,7 @@ module Subroutine
|
|
241
241
|
assert_nil op.date_input
|
242
242
|
end
|
243
243
|
|
244
|
-
def
|
244
|
+
def test_time_inputs__with_seconds_precision
|
245
245
|
op.time_input = nil
|
246
246
|
assert_nil op.time_input
|
247
247
|
|
@@ -256,22 +256,139 @@ module Subroutine
|
|
256
256
|
assert_equal 0, op.time_input.min
|
257
257
|
assert_equal 0, op.time_input.sec
|
258
258
|
|
259
|
+
op.time_input = ::DateTime.new(2022, 12, 22)
|
260
|
+
assert_equal ::Time, op.time_input.class
|
261
|
+
refute_equal ::DateTime, op.time_input.class
|
262
|
+
|
263
|
+
assert_equal 0, op.time_input.utc_offset
|
264
|
+
assert_equal 2022, op.time_input.year
|
265
|
+
assert_equal 12, op.time_input.month
|
266
|
+
assert_equal 22, op.time_input.day
|
267
|
+
assert_equal 0, op.time_input.hour
|
268
|
+
assert_equal 0, op.time_input.min
|
269
|
+
assert_equal 0, op.time_input.sec
|
270
|
+
|
259
271
|
op.time_input = '2023-05-05T10:00:30.123456Z'
|
260
272
|
assert_equal ::Time, op.time_input.class
|
261
273
|
refute_equal ::DateTime, op.time_input.class
|
262
274
|
|
275
|
+
assert_equal 0, op.time_input.utc_offset
|
276
|
+
assert_equal 2023, op.time_input.year
|
277
|
+
assert_equal 5, op.time_input.month
|
278
|
+
assert_equal 5, op.time_input.day
|
279
|
+
assert_equal 10, op.time_input.hour
|
280
|
+
assert_equal 0, op.time_input.min
|
281
|
+
assert_equal 30, op.time_input.sec
|
282
|
+
assert_equal 0, op.time_input.usec
|
283
|
+
|
284
|
+
op.time_input = '2023-05-05T10:00:30Z'
|
285
|
+
assert_equal ::Time, op.time_input.class
|
286
|
+
assert_equal 0, op.time_input.utc_offset
|
263
287
|
assert_equal 2023, op.time_input.year
|
264
288
|
assert_equal 5, op.time_input.month
|
265
289
|
assert_equal 5, op.time_input.day
|
266
290
|
assert_equal 10, op.time_input.hour
|
267
291
|
assert_equal 0, op.time_input.min
|
268
292
|
assert_equal 30, op.time_input.sec
|
269
|
-
assert_equal
|
293
|
+
assert_equal 0, op.time_input.usec
|
270
294
|
|
271
|
-
|
295
|
+
op.time_input = '2024-11-11T16:42:23.246+0100'
|
296
|
+
assert_equal ::Time, op.time_input.class
|
297
|
+
assert_equal 3600, op.time_input.utc_offset
|
298
|
+
assert_equal 2024, op.time_input.year
|
299
|
+
assert_equal 11, op.time_input.month
|
300
|
+
assert_equal 11, op.time_input.day
|
301
|
+
assert_equal 16, op.time_input.hour
|
302
|
+
assert_equal 42, op.time_input.min
|
303
|
+
assert_equal 23, op.time_input.sec
|
304
|
+
assert_equal 0, op.time_input.usec
|
305
|
+
|
306
|
+
time = Time.at(1678741605.123456).utc
|
272
307
|
op.time_input = time
|
273
|
-
|
274
|
-
|
308
|
+
refute_equal time, op.time_input
|
309
|
+
refute_equal time.object_id, op.time_input.object_id
|
310
|
+
assert_equal 2023, op.time_input.year
|
311
|
+
assert_equal 3, op.time_input.month
|
312
|
+
assert_equal 13, op.time_input.day
|
313
|
+
assert_equal 21, op.time_input.hour
|
314
|
+
assert_equal 6, op.time_input.min
|
315
|
+
assert_equal 45, op.time_input.sec
|
316
|
+
assert_equal 0, op.time_input.usec
|
317
|
+
end
|
318
|
+
|
319
|
+
def test_time_inputs__with_high_precision
|
320
|
+
op.precise_time_input = nil
|
321
|
+
assert_nil op.precise_time_input
|
322
|
+
|
323
|
+
op.precise_time_input = '2022-12-22'
|
324
|
+
assert_equal ::Time, op.precise_time_input.class
|
325
|
+
refute_equal ::DateTime, op.precise_time_input.class
|
326
|
+
|
327
|
+
assert_equal 2022, op.precise_time_input.year
|
328
|
+
assert_equal 12, op.precise_time_input.month
|
329
|
+
assert_equal 22, op.precise_time_input.day
|
330
|
+
assert_equal 0, op.precise_time_input.hour
|
331
|
+
assert_equal 0, op.precise_time_input.min
|
332
|
+
assert_equal 0, op.precise_time_input.sec
|
333
|
+
|
334
|
+
op.precise_time_input = ::DateTime.new(2022, 12, 22)
|
335
|
+
assert_equal ::Time, op.precise_time_input.class
|
336
|
+
refute_equal ::DateTime, op.precise_time_input.class
|
337
|
+
|
338
|
+
assert_equal 0, op.precise_time_input.utc_offset
|
339
|
+
assert_equal 2022, op.precise_time_input.year
|
340
|
+
assert_equal 12, op.precise_time_input.month
|
341
|
+
assert_equal 22, op.precise_time_input.day
|
342
|
+
assert_equal 0, op.precise_time_input.hour
|
343
|
+
assert_equal 0, op.precise_time_input.min
|
344
|
+
assert_equal 0, op.precise_time_input.sec
|
345
|
+
|
346
|
+
op.precise_time_input = '2023-05-05T10:00:30.123456Z'
|
347
|
+
assert_equal ::Time, op.precise_time_input.class
|
348
|
+
refute_equal ::DateTime, op.precise_time_input.class
|
349
|
+
|
350
|
+
assert_equal 0, op.precise_time_input.utc_offset
|
351
|
+
assert_equal 2023, op.precise_time_input.year
|
352
|
+
assert_equal 5, op.precise_time_input.month
|
353
|
+
assert_equal 5, op.precise_time_input.day
|
354
|
+
assert_equal 10, op.precise_time_input.hour
|
355
|
+
assert_equal 0, op.precise_time_input.min
|
356
|
+
assert_equal 30, op.precise_time_input.sec
|
357
|
+
assert_equal 123456, op.precise_time_input.usec
|
358
|
+
|
359
|
+
op.precise_time_input = '2023-05-05T10:00:30Z'
|
360
|
+
assert_equal ::Time, op.precise_time_input.class
|
361
|
+
assert_equal 0, op.precise_time_input.utc_offset
|
362
|
+
assert_equal 2023, op.precise_time_input.year
|
363
|
+
assert_equal 5, op.precise_time_input.month
|
364
|
+
assert_equal 5, op.precise_time_input.day
|
365
|
+
assert_equal 10, op.precise_time_input.hour
|
366
|
+
assert_equal 0, op.precise_time_input.min
|
367
|
+
assert_equal 30, op.precise_time_input.sec
|
368
|
+
assert_equal 0, op.precise_time_input.usec
|
369
|
+
|
370
|
+
op.precise_time_input = '2024-11-11T16:42:23.246+0100'
|
371
|
+
assert_equal ::Time, op.precise_time_input.class
|
372
|
+
assert_equal 3600, op.precise_time_input.utc_offset
|
373
|
+
assert_equal 2024, op.precise_time_input.year
|
374
|
+
assert_equal 11, op.precise_time_input.month
|
375
|
+
assert_equal 11, op.precise_time_input.day
|
376
|
+
assert_equal 16, op.precise_time_input.hour
|
377
|
+
assert_equal 42, op.precise_time_input.min
|
378
|
+
assert_equal 23, op.precise_time_input.sec
|
379
|
+
assert_equal 246000, op.precise_time_input.usec
|
380
|
+
|
381
|
+
time = Time.at(1678741605.123456).utc
|
382
|
+
op.precise_time_input = time
|
383
|
+
assert_equal time, op.precise_time_input
|
384
|
+
assert_equal time.object_id, op.precise_time_input.object_id
|
385
|
+
assert_equal 2023, op.precise_time_input.year
|
386
|
+
assert_equal 3, op.precise_time_input.month
|
387
|
+
assert_equal 13, op.precise_time_input.day
|
388
|
+
assert_equal 21, op.precise_time_input.hour
|
389
|
+
assert_equal 6, op.precise_time_input.min
|
390
|
+
assert_equal 45, op.precise_time_input.sec
|
391
|
+
assert_equal 123456, op.precise_time_input.usec
|
275
392
|
end
|
276
393
|
|
277
394
|
def test_iso_date_inputs
|
data/test/support/ops.rb
CHANGED
@@ -177,6 +177,7 @@ class TypeCastOp < ::Subroutine::Op
|
|
177
177
|
boolean :boolean_input
|
178
178
|
date :date_input
|
179
179
|
time :time_input, default: -> { Time.now }
|
180
|
+
time :precise_time_input, precision: :high
|
180
181
|
iso_date :iso_date_input
|
181
182
|
iso_time :iso_time_input
|
182
183
|
object :object_input
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: subroutine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.
|
4
|
+
version: 3.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Nelson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-11-
|
11
|
+
date: 2024-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|