Sha256: cb06c32b10c0713ac6eb524dc8c30e11af890960c1ad6bda5632ffdd7f573baa
Contents?: true
Size: 1.34 KB
Versions: 4
Compression:
Stored size: 1.34 KB
Contents
# frozen_string_literal: true 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
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
dry-monads-1.6.0 | lib/dry/monads/do/mixin.rb |
dry-monads-1.5.0 | lib/dry/monads/do/mixin.rb |
dry-monads-1.4.0 | lib/dry/monads/do/mixin.rb |
dry-monads-1.3.5 | lib/dry/monads/do/mixin.rb |