Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions docsite/source/index.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Copy link

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 user

Copy link

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.

render json: user
in Failure[code, errors]
Copy link

Choose a reason for hiding this comment

The 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
Copy link

Choose a reason for hiding this comment

The 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
Expand Down