lib/dry/monads/do.rb
# Generates a module that passes a block to methods
# that either unwraps a single-valued monadic value or halts
# the execution.
#
# @example A complete example
#
#   class CreateUser
#     include Dry::Monads::Result::Mixin
#     include Dry::Monads::Try::Mixin
#     include Dry::Monads::Do.for(:call)
#
#     attr_reader :user_repo
#
#     def initialize(:user_repo)
#       @user_repo = user_repo
#     end
#
#     def call(params)
#       json = yield parse_json(params)
#       hash = yield validate(json)
#
#       user_repo.transaction do
#         user = yield create_user(hash[:user])
#         yield create_profile(user, hash[:profile])
#       end
#
#       Success(user)
#     end
#
#     private
#
#     def parse_json(params)
#       Try(JSON::ParserError) {
#         JSON.parse(params)
#       }.to_result
#     end
#
#     def validate(json)
#       UserSchema.(json).to_monad
#     end
#
#     def create_user(user_data)
#       Try(Sequel::Error) {
#         user_repo.create(user_data)
#       }.to_result
#     end
#
#     def create_profile(user, profile_data)
#       Try(Sequel::Error) {
#         user_repo.create_profile(user, profile_data)
#       }.to_result
#     end
#   end
#
# @param [Array<Symbol>] methods
# @return [Module]
Generates a module that passes a block to methods
that either unwraps a single-valued monadic value or halts
the execution.
Generates a module that passes a block to methods that either unwraps a single - valued monadic value or halts the execution .
def for(*methods)
          ::Module.new do
            singleton_class.define_method(:included) do |base|
              mod = ::Module.new
              base.prepend(mod)
              base.extend(MethodTracker.new(methods, base, mod))
            end
          end
        end
def for ( * methods ) :: Module . new do singleton_class . define_method ( :included ) do | base | mod = :: Module . new base . prepend ( mod ) base . extend ( MethodTracker . new ( methods , base , mod ) ) end end end
Generates a module that passes a block to methods
that either unwraps a single-valued monadic value or halts
the execution.
Generates a module that passes a block to methods that either unwraps a single - valued monadic value or halts the execution .
None
Module
methods
<not_specific>
<not_specific>
None
example
A complete example class CreateUser include Dry::Monads::Result::Mixin include Dry::Monads::Try::Mixin include Dry::Monads::Do.for(:call) attr_reader :user_repo def initialize(:user_repo) @user_repo = user_repo end def call(params) json = yield parse_json(params) hash = yield validate(json) user_repo.transaction do user = yield create_user(hash[:user]) yield create_profile(user, hash[:profile]) end Success(user) end private def parse_json(params) Try(JSON::ParserError) { JSON.parse(params) }.to_result end def validate(json) UserSchema.(json).to_monad end def create_user(user_data) Try(Sequel::Error) { user_repo.create(user_data) }.to_result end def create_profile(user, profile_data) Try(Sequel::Error) { user_repo.create_profile(user, profile_data) }.to_result end end
A complete example class CreateUser include Dry :: Monads :: Result :: Mixin include Dry :: Monads :: Try :: Mixin include Dry :: Monads :: Do . for ( : call ) attr_reader : user_repo def initialize ( : user_repo ) @user_repo = user_repo end def call ( params ) json = yield parse_json ( params ) hash = yield validate ( json ) user_repo . transaction do user = yield create_user ( hash [ : user ] ) yield create_profile ( user hash [ : profile ] ) end Success ( user ) end private def parse_json ( params ) Try ( JSON :: ParserError ) { JSON . parse ( params ) } . to_result end def validate ( json ) UserSchema . ( json ) . to_monad end def create_user ( user_data ) Try ( Sequel :: Error ) { user_repo . create ( user_data ) } . to_result end def create_profile ( user profile_data ) Try ( Sequel :: Error ) { user_repo . create_profile ( user profile_data ) } . to_result end end