|
| 1 | + |
| 2 | + |
| 3 | +module Dry |
| 4 | + module Monads |
| 5 | + # An implementation of do-notation. |
| 6 | + # |
| 7 | + # @see Do.for |
| 8 | + module Do |
| 9 | + extend Mixin |
| 10 | + |
| 11 | + VISIBILITY_WORD: untyped |
| 12 | + |
| 13 | + # @api private |
| 14 | + class Halt < StandardError |
| 15 | + # @api private |
| 16 | + attr_reader result: untyped |
| 17 | + |
| 18 | + def initialize: (untyped result) -> untyped |
| 19 | + end |
| 20 | + |
| 21 | + # @api private |
| 22 | + class MethodTracker < ::Module |
| 23 | + # @api private |
| 24 | + def initialize: (untyped tracked_methods, untyped base, untyped wrapper) -> untyped |
| 25 | + end |
| 26 | + |
| 27 | + # Generates a module that passes a block to methods |
| 28 | + # that either unwraps a single-valued monadic value or halts |
| 29 | + # the execution. |
| 30 | + # |
| 31 | + # @example A complete example |
| 32 | + # |
| 33 | + # class CreateUser |
| 34 | + # include Dry::Monads::Result::Mixin |
| 35 | + # include Dry::Monads::Try::Mixin |
| 36 | + # include Dry::Monads::Do.for(:call) |
| 37 | + # |
| 38 | + # attr_reader :user_repo |
| 39 | + # |
| 40 | + # def initialize(:user_repo) |
| 41 | + # @user_repo = user_repo |
| 42 | + # end |
| 43 | + # |
| 44 | + # def call(params) |
| 45 | + # json = yield parse_json(params) |
| 46 | + # hash = yield validate(json) |
| 47 | + # |
| 48 | + # user_repo.transaction do |
| 49 | + # user = yield create_user(hash[:user]) |
| 50 | + # yield create_profile(user, hash[:profile]) |
| 51 | + # end |
| 52 | + # |
| 53 | + # Success(user) |
| 54 | + # end |
| 55 | + # |
| 56 | + # private |
| 57 | + # |
| 58 | + # def parse_json(params) |
| 59 | + # Try(JSON::ParserError) { |
| 60 | + # JSON.parse(params) |
| 61 | + # }.to_result |
| 62 | + # end |
| 63 | + # |
| 64 | + # def validate(json) |
| 65 | + # UserSchema.(json).to_monad |
| 66 | + # end |
| 67 | + # |
| 68 | + # def create_user(user_data) |
| 69 | + # Try(Sequel::Error) { |
| 70 | + # user_repo.create(user_data) |
| 71 | + # }.to_result |
| 72 | + # end |
| 73 | + # |
| 74 | + # def create_profile(user, profile_data) |
| 75 | + # Try(Sequel::Error) { |
| 76 | + # user_repo.create_profile(user, profile_data) |
| 77 | + # }.to_result |
| 78 | + # end |
| 79 | + # end |
| 80 | + # |
| 81 | + # @param [Array<Symbol>] methods |
| 82 | + # @return [Module] |
| 83 | + def self.for: (*untyped methods) -> untyped |
| 84 | + |
| 85 | + # @api private |
| 86 | + def self.included: (untyped base) -> untyped |
| 87 | + |
| 88 | + # @api private |
| 89 | + def self.wrap_method: (untyped target, untyped method, untyped visibility) -> untyped |
| 90 | + |
| 91 | + # @api private |
| 92 | + def self.method_visibility: (untyped mod, untyped method) -> untyped |
| 93 | + |
| 94 | + # @api private |
| 95 | + def self.coerce_to_monad: (untyped monads) -> untyped |
| 96 | + |
| 97 | + # @api private |
| 98 | + def self.halt: (untyped result) -> untyped |
| 99 | + end |
| 100 | + end |
| 101 | +end |
| 102 | + |
0 commit comments