-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add coercion support for DateTime (#116)
- Loading branch information
1 parent
00998e7
commit 6f6c3e1
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# typed: strict | ||
|
||
require "date" | ||
|
||
module Typed | ||
module Coercion | ||
class DateTimeCoercer < Coercer | ||
extend T::Generic | ||
|
||
Target = type_member { {fixed: DateTime} } | ||
|
||
sig { override.params(type: T::Types::Base).returns(T::Boolean) } | ||
def used_for_type?(type) | ||
T::Utils.coerce(type) == T::Utils.coerce(DateTime) | ||
end | ||
|
||
sig { override.params(type: T::Types::Base, value: Value).returns(Result[Target, CoercionError]) } | ||
def coerce(type:, value:) | ||
return Failure.new(CoercionError.new("Type must be a DateTime.")) unless used_for_type?(type) | ||
|
||
return Success.new(value) if value.is_a?(DateTime) | ||
|
||
Success.new(DateTime.parse(value)) | ||
rescue Date::Error, TypeError | ||
Failure.new(CoercionError.new("'#{value}' cannot be coerced into DateTime.")) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# typed: true | ||
|
||
require "test_helper" | ||
|
||
class DateTimeCoercerTest < Minitest::Test | ||
def setup | ||
@coercer = Typed::Coercion::DateTimeCoercer.new | ||
@type = T::Utils.coerce(DateTime) | ||
end | ||
|
||
def test_used_for_type_works | ||
assert(@coercer.used_for_type?(@type)) | ||
refute(@coercer.used_for_type?(T::Utils.coerce(Integer))) | ||
end | ||
|
||
def test_when_non_date_type_given_returns_failure | ||
result = @coercer.coerce(type: T::Utils.coerce(Integer), value: DateTime.now) | ||
|
||
assert_failure(result) | ||
assert_error(Typed::Coercion::CoercionError.new("Type must be a DateTime."), result) | ||
end | ||
|
||
def test_when_coercable_returns_success | ||
assert_payload(DateTime.new(2024, 7, 11, 19, 31, 30), @coercer.coerce(type: @type, value: "2024-07-11T19:31:30Z")) | ||
assert_payload(DateTime.new(2024, 7, 11, 19, 31, 30), @coercer.coerce(type: @type, value: "2024-07-11T19:31:30")) | ||
assert_payload(DateTime.new(2024, 7, 11), @coercer.coerce(type: @type, value: "2024-07-11")) | ||
end | ||
|
||
def test_when_not_coercable_returns_failure | ||
assert_error(Typed::Coercion::CoercionError.new("'a' cannot be coerced into DateTime."), @coercer.coerce(type: @type, value: "a")) | ||
assert_error(Typed::Coercion::CoercionError.new("'1' cannot be coerced into DateTime."), @coercer.coerce(type: @type, value: 1)) | ||
end | ||
end |