Skip to content

Commit fad95af

Browse files
authored
bump to 3.0.5 (#75)
* bump to 3.0.5 * add test
1 parent 31709f9 commit fad95af

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

lib/subroutine.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,17 @@
33
require "subroutine/version"
44
require "subroutine/fields"
55
require "subroutine/op"
6+
7+
module Subroutine
8+
9+
def self.preserve_time_precision=(bool)
10+
@preserve_time_precision = !!bool
11+
end
12+
13+
def self.preserve_time_precision?
14+
return !!@preserve_time_precision if defined?(@preserve_time_precision)
15+
16+
false
17+
end
18+
19+
end

lib/subroutine/type_caster.rb

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
require 'time'
55
require 'bigdecimal'
66
require 'securerandom'
7+
require 'active_support'
8+
require 'active_support/json'
79
require 'active_support/core_ext/date_time/acts_like'
810
require 'active_support/core_ext/date_time/calculations'
911
require 'active_support/core_ext/object/acts_like'
@@ -111,7 +113,7 @@ def self.cast(value, options = {})
111113
t ||= value if value.is_a?(::Time)
112114
t ||= value if value.try(:acts_like?, :time)
113115
t ||= ::Time.parse(String(value))
114-
t.utc.iso8601
116+
t.utc.iso8601(::ActiveSupport::JSON::Encoding.time_precision)
115117
end
116118

117119
::Subroutine::TypeCaster.register :date do |value, _options = {}|
@@ -123,21 +125,16 @@ def self.cast(value, options = {})
123125
::Subroutine::TypeCaster.register :time, :timestamp, :datetime do |value, options = {}|
124126
next nil unless value.present?
125127

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)
128+
value = if value.try(:acts_like?, :time)
129+
value.to_time
130+
else
131+
::Time.parse(String(value))
140132
end
133+
134+
# High precision must be opted into. The original implementation is to set usec:0
135+
next value if options[:precision] == :high || ::Subroutine.preserve_time_precision?
136+
137+
value.change(usec: 0)
141138
end
142139

143140
::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 = 4
7+
PATCH = 5
88
PRE = nil
99

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

test/subroutine/type_caster_test.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,20 @@ def test_time_inputs__with_seconds_precision
316316
assert_equal 0, op.time_input.usec
317317
end
318318

319+
def test_time_inputs__with_preserve_time_precision
320+
Subroutine.stubs(:preserve_time_precision?).returns(true)
321+
322+
time = Time.at(1678741605.123456).utc
323+
op.time_input = time
324+
assert_equal 2023, op.time_input.year
325+
assert_equal 3, op.time_input.month
326+
assert_equal 13, op.time_input.day
327+
assert_equal 21, op.time_input.hour
328+
assert_equal 6, op.time_input.min
329+
assert_equal 45, op.time_input.sec
330+
assert_equal 123456, op.time_input.usec
331+
end
332+
319333
def test_time_inputs__with_high_precision
320334
op.precise_time_input = nil
321335
assert_nil op.precise_time_input
@@ -410,11 +424,11 @@ def test_iso_time_inputs
410424

411425
op.iso_time_input = '2022-12-22T10:30:24Z'
412426
assert_equal ::String, op.iso_time_input.class
413-
assert_equal '2022-12-22T10:30:24Z', op.iso_time_input
427+
assert_equal '2022-12-22T10:30:24.000Z', op.iso_time_input
414428

415-
op.iso_time_input = Time.parse('2022-12-22T10:30:24Z')
429+
op.iso_time_input = Time.parse('2022-12-22T10:30:24.123456Z')
416430
assert_equal ::String, op.iso_time_input.class
417-
assert_equal '2022-12-22T10:30:24Z', op.iso_time_input
431+
assert_equal '2022-12-22T10:30:24.123Z', op.iso_time_input
418432
end
419433

420434
def test_file_inputs

0 commit comments

Comments
 (0)