Skip to content

Commit

Permalink
Initial release v0.1.0 (#11)
Browse files Browse the repository at this point in the history
This gem works with Sorbet's static type checker and type definitions. It'll return a statically-typed object or throws `T::CoercionError` if the coercion fails.

It provides a simple and generic way of coercing types in a sorbet-typed project. It is particularly useful when we're dealing with external API responses and controller parameters.

```ruby
converted = T::Coerce[<Type>].new.from(<value>)

T.reveal_type(converted) # <Type>
```

The supported types include
- Simple Types (Integer, String, etc.)
- Custom Types: If the values can be coerced by `.new`
- `T.nilable(<supported type>)`
- `T::Array[<supported type>]`
- Subclasses of `T::Struct`

We don't support
- `T::Hash` (currently)
- `T.any(<supported type>, ...)`: A union type other than `T.nilable`
  • Loading branch information
donaldong authored Oct 16, 2019
1 parent 6b00127 commit fb766b7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

A type coercion lib works with [Sorbet](https://sorbet.org)'s static type checker and type definitions; raises an error if the coercion fails.

It provides a simple and generic way of coercing types in a sorbet-typed project. It is particularly useful when we're dealing with external API responses and controller parameters.

## Installation
1. Follow the steps [here](https://sorbet.org/docs/adopting) to set up the latest version of Sorbet and run `srb tc`.
2. Add `sorbet-coerce` to your Gemfile and install them with `Bundler`.
Expand Down
4 changes: 2 additions & 2 deletions sorbet-coerce.gemspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Gem::Specification.new do |s|
s.name = %q{sorbet-coerce}
s.version = "0.0.1"
s.version = "0.1.0"
s.date = %q{2019-10-04}
s.summary = %q{Type coercion with Sorbet}
s.summary = %q{A type coercion lib works with Sorbet's static type checker and type definitions; raises an error if the coercion fails.}
s.authors = ["Chan Zuckerberg Initiative"]
s.email = "[email protected]"
s.homepage = "https://github.com/chanzuckerberg/sorbet-coerce"
Expand Down

0 comments on commit fb766b7

Please sign in to comment.