Skip to content

Commit 25a768d

Browse files
committed
Add more documentation
1 parent fa6e7ba commit 25a768d

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

docs/guide/type-system.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,42 @@
22

33
## Overview
44

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+
def func(params)
15+
result = MyContract.new.call(params)
16+
if result.errors.empty?
17+
result[:age].to_i + 10
18+
else
19+
raise StandardError, 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+
def func(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
39+
func(age: "asd") # will raise a meaningful error
40+
```
641

742
## Basic Usage
843

lib/mortymer/sigil.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require_relative "types"
4+
35
module Mortymer
46
# Sigil provides symbolic type checking for input and outputs
57
# of method calls using dry-types

0 commit comments

Comments
 (0)