Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arefaslani/4b3911d9540d642ef47ce8fcd008024f to your computer and use it in GitHub Desktop.
Save arefaslani/4b3911d9540d642ef47ce8fcd008024f to your computer and use it in GitHub Desktop.
Medium 702687394e3d-railway-oriented-programming
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