Skip to content

Commit 6f267a4

Browse files
committed
Add gen-sig script
This is adds the `gen-sig` script to generate signatures as well as a signatures for the project.
1 parent d96f8b9 commit 6f267a4

35 files changed

+2693
-1
lines changed

Gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ group :docs do
1818
gem "yard-junk"
1919
end
2020

21+
group :sig do
22+
gem "rbs"
23+
gem "rbs-inline"
24+
end
25+
2126
group :test do
2227
gem "debug_inspector"
2328
gem "dry-types"

bin/gen-sig

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "fileutils"
5+
require "pathname"
6+
7+
root_path = Pathname.new(File.expand_path("../", __dir__))
8+
9+
manifest_path = root_path.join("sig/manifest.yaml")
10+
manifest = manifest_path.exist? ? manifest_path.read : "dependencies: []"
11+
12+
FileUtils.rm_rf(root_path.join("sig"))
13+
14+
system "rbs-inline lib --opt-out --output=./sig"
15+
16+
EMPTY_COMMENT_BEFORE_CODE = /
17+
^[ \t]*\#[ \t]*\n
18+
(?=^[ \t]*[^#\s])
19+
/mx
20+
EMPTY_STRING = ""
21+
GENERATED_LINE = /^ *# Generated from .+.rb with RBS::Inline *$/
22+
RBS_COMMENT_BLOCK = /
23+
^[ \t]*\#[ \t]*@rbs.*\n
24+
(?:^[ \t]*\#.*\n)*
25+
/x
26+
27+
root_path.glob("sig/**/*.rbs").each do |file|
28+
contents = file.read
29+
30+
contents.gsub!(GENERATED_LINE, EMPTY_STRING)
31+
&.gsub!(RBS_COMMENT_BLOCK, EMPTY_STRING)
32+
&.gsub!(EMPTY_COMMENT_BEFORE_CODE, EMPTY_STRING)
33+
&.strip
34+
35+
if contents.nil? || contents.strip.empty?
36+
File.delete(file)
37+
else
38+
File.write(file, "#{contents}\n")
39+
end
40+
end
41+
42+
File.write(manifest_path, manifest)

lib/dry/monads.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Dry
1414
# @api public
1515
module Monads
1616
# @api private
17+
# @rbs () -> Zietwerk::Loader
1718
def self.loader
1819
@loader ||= ::Zeitwerk::Loader.new.tap do |loader|
1920
root = ::File.expand_path("..", __dir__)
@@ -31,6 +32,7 @@ def self.loader
3132
end
3233

3334
# @private
35+
# @rbs (Class | Module base) -> void
3436
def self.included(base)
3537
if all_loaded?
3638
base.include(*constructors)
@@ -68,6 +70,7 @@ def self.included(base)
6870
# @param [Array<Symbol>] monads
6971
# @return [Module]
7072
# @api public
73+
# @rbs (*Symbol monads) -> Module
7174
def self.[](*monads)
7275
monads.sort!
7376
@mixins.fetch_or_store(monads.hash) do

lib/dry/monads/conversion_stubs.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module Dry
44
module Monads
55
module ConversionStubs
6+
# @rbs (*Symbol method_names) -> void
67
def self.[](*method_names)
78
::Module.new do
89
method_names.each do |name|
@@ -16,14 +17,17 @@ def self.[](*method_names)
1617
module Methods
1718
module_function
1819

20+
# @rbs () -> Maybe
1921
def to_maybe
2022
raise "Load Maybe first with require 'dry/monads/maybe'"
2123
end
2224

25+
# @rbs () -> Result
2326
def to_result
2427
raise "Load Result first with require 'dry/monads/result'"
2528
end
2629

30+
# @rbs () -> Validated
2731
def to_validated
2832
raise "Load Validated first with require 'dry/monads/validated'"
2933
end

lib/dry/monads/do.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class << self
102102
#
103103
# @param [Array<Symbol>] methods
104104
# @return [Module]
105+
# @rbs (*Symbol methods) -> Module
105106
def for(*methods)
106107
::Module.new do
107108
singleton_class.define_method(:included) do |base|
@@ -113,6 +114,7 @@ def for(*methods)
113114
end
114115

115116
# @api private
117+
# @rbs (Class | Module base) -> void
116118
def included(base)
117119
super
118120

lib/dry/monads/do/all.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ module Do
5757
module All
5858
# @private
5959
class MethodTracker < ::Module
60-
attr_reader :wrappers
60+
attr_reader :wrappers #: Hash[Class | Module, Module]
6161

62+
# @rbs (Hash[Class | Module, Module] wrappers) -> void
6263
def initialize(wrappers)
6364
super()
6465

@@ -86,11 +87,13 @@ def included(base)
8687
end
8788
end
8889

90+
# @rbs (Class | Module target) -> void
8991
def extend_object(target)
9092
super
9193
target.prepend(wrappers[target])
9294
end
9395

96+
# @rbs (Class | Module target, Symbol method) -> void
9497
def wrap_method(target, method)
9598
visibility = Do.method_visibility(target, method)
9699
Do.wrap_method(wrappers[target], method, visibility)
@@ -99,6 +102,7 @@ def wrap_method(target, method)
99102

100103
class << self
101104
# @api private
105+
# @rbs (Class | Module base) -> void
102106
def included(base)
103107
super
104108

@@ -110,6 +114,7 @@ def included(base)
110114
end
111115

112116
# @api private
117+
# @rbs (Class klass, __todo__ target) -> void
113118
def wrap_defined_methods(klass, target)
114119
klass.public_instance_methods(false).each do |m|
115120
Do.wrap_method(target, m, :public)
@@ -128,6 +133,7 @@ def wrap_defined_methods(klass, target)
128133
# @api private
129134
module InstanceMixin
130135
# @api private
136+
# @rbs (Class | Module object) -> void
131137
def extended(object)
132138
super
133139

lib/dry/monads/lazy.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,29 @@ def new(promise = nil, &)
2727
# Forces the compution and returns its value.
2828
#
2929
# @return [Object]
30+
# @rbs () -> Object
3031
def value! = @promise.execute.value!
3132
alias_method :force!, :value!
3233

3334
# Forces the computation. Note that if the computation
3435
# thrown an error it won't be re-raised as opposed to value!/force!.
3536
#
3637
# @return [Lazy]
38+
# @rbs () -> Lazy
3739
def force
3840
@promise.execute
3941
self
4042
end
4143

4244
# @return [Boolean]
45+
# @rbs () -> bool
4346
def evaluated? = @promise.complete?
4447
deprecate :complete?, :evaluated?
4548

4649
undef_method :wait
4750

4851
# @return [String]
52+
# @rbs () -> String
4953
def to_s
5054
state =
5155
case promise.state
@@ -76,6 +80,7 @@ module Constructors
7680
#
7781
# @param block [Proc]
7882
# @return [Lazy]
83+
# @rbs () {(?) -> __todo__ } -> Lazy
7984
def Lazy(&) = Lazy.new(&)
8085
end
8186

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: () -> Zietwerk::Loader
10+
11+
# @private
12+
def self.included: (Class | Module base) -> void
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.[]: (*Symbol monads) -> Module
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+

0 commit comments

Comments
 (0)