Skip to content

Commit b18f5b7

Browse files
committed
Time caster now returns input value if it acts like a time object (#70)
1 parent 3ecbe49 commit b18f5b7

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

lib/subroutine/type_caster.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
require 'time'
55
require 'bigdecimal'
66
require 'securerandom'
7+
require 'active_support/core_ext/object/acts_like'
78
require 'active_support/core_ext/object/blank'
89
require 'active_support/core_ext/object/try'
910
require 'active_support/core_ext/array/wrap'
11+
require 'active_support/core_ext/time/acts_like'
1012

1113
module Subroutine
1214
module TypeCaster
@@ -118,7 +120,11 @@ def self.cast(value, options = {})
118120
::Subroutine::TypeCaster.register :time, :timestamp, :datetime do |value, _options = {}|
119121
next nil unless value.present?
120122

121-
::Time.parse(String(value))
123+
if value.try(:acts_like?, :time)
124+
value
125+
else
126+
::Time.parse(String(value))
127+
end
122128
end
123129

124130
::Subroutine::TypeCaster.register :hash, :object, :hashmap, :dict do |value, _options = {}|

lib/subroutine/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Subroutine
44

55
MAJOR = 3
66
MINOR = 0
7-
PATCH = 2
7+
PATCH = 3
88
PRE = nil
99

1010
VERSION = [MAJOR, MINOR, PATCH, PRE].compact.join(".")

test/subroutine/type_caster_test.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def test_time_inputs
256256
assert_equal 0, op.time_input.min
257257
assert_equal 0, op.time_input.sec
258258

259-
op.time_input = '2023-05-05T10:00:30Z'
259+
op.time_input = '2023-05-05T10:00:30.123456Z'
260260
assert_equal ::Time, op.time_input.class
261261
refute_equal ::DateTime, op.time_input.class
262262

@@ -266,6 +266,12 @@ def test_time_inputs
266266
assert_equal 10, op.time_input.hour
267267
assert_equal 0, op.time_input.min
268268
assert_equal 30, op.time_input.sec
269+
assert_equal 123456, op.time_input.usec
270+
271+
time = Time.at(1678741605.123456)
272+
op.time_input = time
273+
assert_equal time, op.time_input
274+
assert_equal time.object_id, op.time_input.object_id
269275
end
270276

271277
def test_iso_date_inputs

0 commit comments

Comments
 (0)