You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guide/type-system.md
+36-1Lines changed: 36 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,42 @@
2
2
3
3
## Overview
4
4
5
-
Mortymer provides a robust type system through the `Sigil` module that leverages `dry-types` to enable runtime type checking for method inputs and outputs. This guide will walk you through how to use type checking in your Ruby code.
5
+
Mortymer provides a way to enforce the types contracts for inputs and
6
+
outputs of your methods through the `Sigil` module that leverages `dry-types`
7
+
to enable runtime type checking. Not only that, but, it also will try to coerce
8
+
the given parameters to the declared type, and will check validation contracts
9
+
and rules for your inputs and outputs. This is not intended to be a replacement
10
+
for Type Checkers like Sorbet or Steep, but rather a nice utility to avoid guard
11
+
code like:
12
+
13
+
```ruby
14
+
deffunc(params)
15
+
result =MyContract.new.call(params)
16
+
if result.errors.empty?
17
+
result[:age].to_i +10
18
+
else
19
+
raiseStandardError, result.errors
20
+
end
21
+
end
22
+
```
23
+
24
+
The above snippet is really common when dealing with validation (you may use `#instance_of?` method as well,
25
+
or strong parameters or whatever, but the essence is that you need to declare the shape of the data
26
+
that comes in and out of your methods).
27
+
28
+
Using Mortymer's Sigil, the above will translate to pretty much the following code:
29
+
30
+
```ruby
31
+
sign MyContract
32
+
deffunc(params)
33
+
params.age +10
34
+
end
35
+
36
+
func(MyContract.structify({age:10})) # Will work
37
+
func(age:10) # Will work, the params would be { age: 10 } which will be coerced and validated with MyCotnract
38
+
func(age:"10") # Will also work, if MyContract is Coercing the age to Integer
0 commit comments