Preview
Open Original
Safely and gracefully model complex transformations with monads, and use pattern matching to make robust error handling a first-class feature of your application.
Dry::Monads::Maybe(user).fmap(&:address).fmap(&:street)
# If user with address exists
# => Some("Street Address")
# If user or address is nil
# => None()
Safely model complex, chained transformations
class CreateArticle
include Dry::Monads[:result]
include Dry::Matcher.for(:call, with: Dry::Matcher::ResultMatcher)
def call(input)
if success?(input)
output = some_action(input)
Success(output)
else
Failure(input)
end
end
end
create = CreateArticle.new
create.(input) do |m|
m.success do |output|
# Handle success
end
m.failur...
Safely and gracefully model complex transformations with monads, and use pattern matching to make robust error handling a first-class feature of your application.
Dry::Monads::Maybe(user).fmap(&:address).fmap(&:street)
# If user with address exists
# => Some("Street Address")
# If user or address is nil
# => None()
Safely model complex, chained transformations
class CreateArticle
include Dry::Monads[:result]
include Dry::Matcher.for(:call, with: Dry::Matcher::ResultMatcher)
def call(input)
if success?(input)
output = some_action(input)
Success(output)
else
Failure(input)
end
end
end
create = CreateArticle.new
create.(input) do |m|
m.success do |output|
# Handle success
end
m.failure do |err|
# Handle failure
end
end
Use the Result monad to model success and failure and match on the result