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
As we know Virtus isn't maintained anymore, so it makes sense to use something
different for coercing. There is a dry-types lib which was created as
a replacement for Virtus (at least a part of it). Although, it is only
about coercion.
This change gets rid of Virtus and adds dry-types to the stack.
The structure inside didn't change match. Dry-types was integrated
without huge refactoring.
If people want to use Virtus for custom types, it is possible after
implementing a parse method which initializes a model.
class User
include Virtus.model
attribute :id, Integer
attribute :name, String
def self.parse(*args)
new(*args)
end
end
Copy file name to clipboardExpand all lines: UPGRADING.md
+43Lines changed: 43 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,49 @@
1
1
Upgrading Grape
2
2
===============
3
3
4
+
### Upgrading to >= 1.3.0
5
+
6
+
#### Ruby
7
+
8
+
After adding dry-types, Ruby 2.4 or newer is required.
9
+
10
+
#### Coercion
11
+
12
+
[Virtus](https://github.com/solnic/virtus) has been replaced by [dry-types](https://dry-rb.org/gems/dry-types/1.2/) for parameter coercion. If your project depends on Virtus, explicitly add it to your `Gemfile`. Also, if Virtus is used for defining custom types
13
+
14
+
```ruby
15
+
classUser
16
+
includeVirtus.model
17
+
18
+
attribute :id, Integer
19
+
attribute :name, String
20
+
end
21
+
22
+
# somewhere in your API
23
+
params do
24
+
requires :user, type:User
25
+
end
26
+
```
27
+
28
+
Add a class-level `parse` method to the model:
29
+
30
+
```ruby
31
+
classUser
32
+
includeVirtus.model
33
+
34
+
attribute :id, Integer
35
+
attribute :name, String
36
+
37
+
defself.parse(attrs)
38
+
new(attrs)
39
+
end
40
+
end
41
+
```
42
+
43
+
Custom types which don't depend on Virtus don't require any changes.
44
+
45
+
For more information see [#1920](https://github.com/ruby-grape/grape/pull/1920).
0 commit comments