-
-
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 schema method to structs (#60)
- Loading branch information
1 parent
e90d62b
commit 4b7ff34
Showing
3 changed files
with
34 additions
and
1 deletion.
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,12 @@ | ||
# typed: strict | ||
|
||
module T | ||
class Struct | ||
extend T::Sig | ||
|
||
sig { overridable.returns(Typed::Schema) } | ||
def self.schema | ||
Typed::Schema.from_struct(self) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# typed: true | ||
|
||
class StructTest < Minitest::Test | ||
def test_schema_can_be_derived_from_struct | ||
expected_schema = Typed::Schema.new( | ||
fields: [ | ||
Typed::Field.new(name: :name, type: String), | ||
Typed::Field.new(name: :capital, type: T::Utils.coerce(T::Boolean)) | ||
], | ||
target: City | ||
) | ||
|
||
assert_equal(expected_schema, City.schema) | ||
end | ||
end |