Sha256: 828661227f0ed4cfc9e169c0a4a4e08ab837e3f28ca01879cb1d2609c609d310
Contents?: true
Size: 1.31 KB
Versions: 3
Compression:
Stored size: 1.31 KB
Contents
module Dry module Monads module Do # Do notation as a mixin. # You can use it in any place in your code, see examples. # # @example class-level mixin # # class CreateUser # extend Dry::Monads::Do::Mixin # extend Dry::Monads[:result] # # def self.run(params) # self.call do # values = bind Validator.validate(params) # user = bind UserRepository.create(values) # # Success(user) # end # end # end # # @example using methods defined on Do # # create_user = proc do |params| # Do.() do # values = bind validate(params) # user = bind user_repo.create(values) # # Success(user) # end # end # # @api public module Mixin # @api public def call yield rescue Halt => e e.result end # @api public def bind(monads) monads = Do.coerce_to_monad(Array(monads)) unwrapped = monads.map do |result| monad = result.to_monad monad.or { Do.halt(monad) }.value! end monads.size == 1 ? unwrapped[0] : unwrapped end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
dry-monads-1.3.4 | lib/dry/monads/do/mixin.rb |
dry-monads-1.3.3 | lib/dry/monads/do/mixin.rb |
dry-monads-1.3.2 | lib/dry/monads/do/mixin.rb |