Skip to content

Commit 5a2e60c

Browse files
committed
Generate initial signatures
1 parent 06a9c66 commit 5a2e60c

28 files changed

+2625
-0
lines changed

sig/dry/monads.rbs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
3+
module Dry
4+
# Common, idiomatic monads for Ruby
5+
#
6+
# @api public
7+
module Monads
8+
# @api private
9+
def self.loader: () -> untyped
10+
11+
# @private
12+
def self.included: (untyped base) -> untyped
13+
14+
# Build a module with cherry-picked monads.
15+
# It saves a bit of typing when you add multiple
16+
# monads to one class. Not loaded monads get loaded automatically.
17+
#
18+
# @example
19+
# require 'dry/monads'
20+
#
21+
# class CreateUser
22+
# include Dry::Monads[:result, :do]
23+
#
24+
# def initialize(repo, send_email)
25+
# @repo = repo
26+
# @send_email = send_email
27+
# end
28+
#
29+
# def call(name)
30+
# if @repo.user_exist?(name)
31+
# Failure(:user_exists)
32+
# else
33+
# user = yield @repo.add_user(name)
34+
# yield @send_email.(user)
35+
# Success(user)
36+
# end
37+
# end
38+
# end
39+
#
40+
# @param [Array<Symbol>] monads
41+
# @return [Module]
42+
# @api public
43+
def self.[]: (*untyped monads) -> untyped
44+
end
45+
end
46+

sig/dry/monads/all.rbs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
module Dry
4+
module Monads
5+
end
6+
end
7+

sig/dry/monads/constants.rbs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
3+
module Dry
4+
module Monads
5+
include Core::Constants
6+
end
7+
end
8+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
module Dry
4+
module Monads
5+
module ConversionStubs
6+
def self.[]: (*untyped method_names) -> untyped
7+
8+
module Methods
9+
def self?.to_maybe: () -> untyped
10+
11+
def self?.to_result: () -> untyped
12+
13+
def self?.to_validated: () -> untyped
14+
end
15+
end
16+
end
17+
end
18+

sig/dry/monads/curry.rbs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
3+
module Dry
4+
module Monads
5+
# @private
6+
module Curry
7+
# @private
8+
def self.call: (untyped value) -> untyped
9+
end
10+
end
11+
end
12+

sig/dry/monads/do.rbs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+

sig/dry/monads/do/all.rbs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
3+
module Dry
4+
module Monads
5+
module Do
6+
# Do::All automatically wraps methods defined in a class with an unwrapping block.
7+
# Similar to what `Do.for(...)` does except wraps every method so you don't have
8+
# to list them explicitly.
9+
#
10+
# @example annotated example
11+
#
12+
# require 'dry/monads/do/all'
13+
# require 'dry/monads/result'
14+
#
15+
# class CreateUser
16+
# include Dry::Monads::Do::All
17+
# include Dry::Monads::Result::Mixin
18+
#
19+
# def call(params)
20+
# # Unwrap a monadic value using an implicitly passed block
21+
# # if `validates` returns Failure, the execution will be halted
22+
# values = yield validate(params)
23+
# user = create_user(values)
24+
# # If another block is passed to a method then takes
25+
# # precedence over the unwrapping block
26+
# safely_subscribe(values[:email]) { Logger.info("Already subscribed") }
27+
#
28+
# Success(user)
29+
# end
30+
#
31+
# def validate(params)
32+
# if params.key?(:email)
33+
# Success(email: params[:email])
34+
# else
35+
# Failure(:no_email)
36+
# end
37+
# end
38+
#
39+
# def create_user(user)
40+
# # Here a block is passed to the method but we don't use it
41+
# UserRepo.new.add(user)
42+
# end
43+
#
44+
# def safely_subscribe(email)
45+
# repo = SubscriptionRepo.new
46+
#
47+
# if repo.subscribed?(email)
48+
# # This calls the logger because a block
49+
# # explicitly passed from `call`
50+
# yield
51+
# else
52+
# repo.subscribe(email)
53+
# end
54+
# end
55+
# end
56+
module All
57+
# @private
58+
class MethodTracker < ::Module
59+
attr_reader wrappers: untyped
60+
61+
def initialize: (untyped wrappers) -> untyped
62+
63+
def extend_object: (untyped target) -> untyped
64+
65+
def wrap_method: (untyped target, untyped method) -> untyped
66+
end
67+
68+
# @api private
69+
def self.included: (untyped base) -> untyped
70+
71+
# @api private
72+
def self.wrap_defined_methods: (untyped klass, untyped target) -> untyped
73+
74+
# @api private
75+
module InstanceMixin
76+
# @api private
77+
def extended: (untyped object) -> untyped
78+
end
79+
80+
extend InstanceMixin
81+
end
82+
end
83+
84+
def warn: (untyped message, ?category: untyped, **untyped) -> untyped
85+
end
86+
end
87+

sig/dry/monads/do/mixin.rbs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
3+
module Dry
4+
module Monads
5+
module Do
6+
# Do notation as a mixin.
7+
# You can use it in any place in your code, see examples.
8+
#
9+
# @example class-level mixin
10+
#
11+
# class CreateUser
12+
# extend Dry::Monads::Do::Mixin
13+
# extend Dry::Monads[:result]
14+
#
15+
# def self.run(params)
16+
# self.call do
17+
# values = bind Validator.validate(params)
18+
# user = bind UserRepository.create(values)
19+
#
20+
# Success(user)
21+
# end
22+
# end
23+
# end
24+
#
25+
# @example using methods defined on Do
26+
#
27+
# create_user = proc do |params|
28+
# Do.() do
29+
# values = bind validate(params)
30+
# user = bind user_repo.create(values)
31+
#
32+
# Success(user)
33+
# end
34+
# end
35+
#
36+
# @api public
37+
module Mixin
38+
# @api public
39+
def call: () -> untyped
40+
41+
# @api public
42+
def bind: (untyped monads) -> untyped
43+
end
44+
end
45+
end
46+
end
47+

sig/dry/monads/errors.rbs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
3+
module Dry
4+
module Monads
5+
# An unsuccessful result of extracting a value from a monad.
6+
class UnwrapError < ::StandardError
7+
attr_reader receiver: untyped
8+
9+
def initialize: (untyped receiver) -> untyped
10+
end
11+
12+
# An error thrown on returning a Failure of unknown type.
13+
class InvalidFailureTypeError < ::StandardError
14+
def initialize: (untyped failure) -> untyped
15+
end
16+
17+
# Improper use of None
18+
class ConstructorNotAppliedError < ::NoMethodError
19+
def initialize: (untyped method_name, untyped constructor_name) -> untyped
20+
end
21+
end
22+
end
23+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
module Dry
4+
module Monads
5+
module Extensions
6+
module PrettyPrint
7+
class PrintValue < ::Module
8+
def initialize: (untyped constructor, ?accessor: untyped) -> untyped
9+
end
10+
11+
class LazyPrintValue < ::Module
12+
def initialize: (untyped constructor, ?success_prefix: untyped, ?error_prefix: untyped) -> untyped
13+
end
14+
end
15+
16+
def pretty_print: (untyped pp) -> untyped
17+
end
18+
end
19+
end
20+

0 commit comments

Comments
 (0)