-
-
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.
- Loading branch information
1 parent
6005e14
commit ef8c1db
Showing
5 changed files
with
64 additions
and
3 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 DateCoercer < Coercer | ||
extend T::Generic | ||
|
||
Target = type_member { {fixed: Date} } | ||
|
||
sig { override.params(type: T::Types::Base).returns(T::Boolean) } | ||
def used_for_type?(type) | ||
T::Utils.coerce(type) == T::Utils.coerce(Date) | ||
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 Date.")) unless used_for_type?(type) | ||
|
||
return Success.new(value) if value.is_a?(Date) | ||
|
||
Success.new(Date.parse(value)) | ||
rescue Date::Error, TypeError | ||
Failure.new(CoercionError.new("'#{value}' cannot be coerced into Date.")) | ||
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,31 @@ | ||
# typed: true | ||
|
||
class DateCoercerTest < Minitest::Test | ||
def setup | ||
@coercer = Typed::Coercion::DateCoercer.new | ||
@type = T::Utils.coerce(Date) | ||
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: Date.new(2024, 1, 1)) | ||
|
||
assert_failure(result) | ||
assert_error(Typed::Coercion::CoercionError.new("Type must be a Date."), result) | ||
end | ||
|
||
def test_when_coercable_returns_success | ||
assert_payload(Date.new(2024, 1, 1), @coercer.coerce(type: @type, value: "20240101")) | ||
assert_payload(Date.new(2024, 5, 1), @coercer.coerce(type: @type, value: "1/5/2024")) | ||
assert_payload(Date.new(2024, 5, 1), @coercer.coerce(type: @type, value: Date.new(2024, 5, 1))) | ||
end | ||
|
||
def test_when_not_coercable_returns_failure | ||
assert_error(Typed::Coercion::CoercionError.new("'a' cannot be coerced into Date."), @coercer.coerce(type: @type, value: "a")) | ||
assert_error(Typed::Coercion::CoercionError.new("'1' cannot be coerced into Date."), @coercer.coerce(type: @type, value: 1)) | ||
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