-
-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use pattern matching with controller helpers example #54
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,14 +188,11 @@ Here's a simple usage example how you could access an operation powered by dry-m | |
```ruby | ||
class UsersController < ApplicationController | ||
def create | ||
resolve("users.create").(safe_params[:user]) do |m| | ||
m.success do |user| | ||
render json: user | ||
end | ||
|
||
m.failure do |code, errors| | ||
render json: { code: code, errors: errors.to_h }, status: :unprocessable_entity | ||
end | ||
case resolve("users.create").(safe_params[:user]) | ||
in Success[user] | ||
render json: user | ||
in Failure[code, errors] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be neat to demonstrate how much more specific pattern-matching can be compared to dry-matcher |
||
render json: { code: code, errors: errors.to_h }, status: :unprocessable_entity | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A pattern-matcher should always be exhaustive, otherwise you'll raise a very unhelpful exception. |
||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning
user
within an array makes no sense here, since we are creating a single userThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a production app me might assume that
Dry::Monads
has been included already, but that will create confusion in a document example.