Created
September 12, 2020 11:48
-
-
Save arefaslani/4b3911d9540d642ef47ce8fcd008024f to your computer and use it in GitHub Desktop.
Medium 702687394e3d-railway-oriented-programming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Posts | |
class Create | |
include Dry::Monads[:result, :do] | |
ValidationSchema = Dry::Schema.Params do | |
required(:title).filled(:str?) | |
optional(:body).maybe(:str?) | |
end | |
def self.call(params) | |
new.execute(params) | |
end | |
private | |
def execute(params) | |
yield validate_params(params) | |
create_post(params) | |
end | |
def validate_params(params) | |
validation_outcome = ValidationSchema.call(params) | |
return Failure(validation_outcome.errors.to_h) if validation_outcome.failure? | |
Success() | |
end | |
def create_post(params) | |
post = Post.create(params) | |
Success(post) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment