factory_bot-sorbet 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.DS_Store +0 -0
- data/CHANGELOG.md +19 -0
- data/LICENSE +8 -0
- data/README.md +121 -0
- data/Rakefile +16 -0
- data/lib/factory_bot/annotations.rbi +9 -0
- data/lib/factory_bot/sorbet/override.rb +24 -0
- data/lib/factory_bot/sorbet/version.rb +8 -0
- data/lib/factory_bot/sorbet.rb +32 -0
- data/lib/tapioca/dsl/compilers/factory_bot.rb +57 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1898ec9f239b8602216d60173afd23de4c71f844215cd8b76d957bca109f673c
|
4
|
+
data.tar.gz: 68210c2ad005fb35d07ab48db799d2bc2002bc4f46221f74e7ef634f11e8c4dd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c8186b8d3d1801dabb9ebdd938e115aec1820c17ad03f4d784d58b6664dac6352af5fde674550db9cd9ad43e0b22680dc42816f8c4f7188366eafb82c67a441d
|
7
|
+
data.tar.gz: 61d7685698c68a645bb0d706050d6f12bc9216a864df484eed3598976ed7ab863ea2aef4b2ed7b7ee2ef92d6d69b8255ff156443d60aac95736ab3aee9bf8a33
|
data/.DS_Store
ADDED
Binary file
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [Unreleased]
|
9
|
+
|
10
|
+
Add changes in new features here. Do not change the gem's version in pull/merge requests.
|
11
|
+
|
12
|
+
### Changes
|
13
|
+
-
|
14
|
+
|
15
|
+
## [0.1.0] - 2025-08-22
|
16
|
+
|
17
|
+
[Diff](https://github.com/Verseth/ruby-factory_bot-sorbet/compare/v0.0.0...v0.1.0)
|
18
|
+
|
19
|
+
- Initial release
|
data/LICENSE
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
Copyright 2025 Mateusz Drewniak
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
8
|
+
|
data/README.md
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
# FactoryBot::Sorbet
|
2
|
+
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
4
|
+
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/factory_bot/sorbet`. To experiment with that code, run `bin/console` for an interactive prompt.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install the gem and add to the application's Gemfile by executing:
|
10
|
+
|
11
|
+
```bash
|
12
|
+
bundle add factory_bot-sorbet
|
13
|
+
```
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
```bash
|
18
|
+
gem install factory_bot-sorbet
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
This gems adds the module `FactoryBot::Sorbet` which contains
|
24
|
+
methods for using all your factories in a type-safe way.
|
25
|
+
|
26
|
+
### The problem
|
27
|
+
|
28
|
+
Given a factory
|
29
|
+
|
30
|
+
```rb
|
31
|
+
FactoryBot.define do
|
32
|
+
factory :foo, class_name: "Foo" do
|
33
|
+
bar { "BAR!" }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
Using regular FactoryBot you'd use it like so:
|
39
|
+
|
40
|
+
```rb
|
41
|
+
FactoryBot.create(:foo, baz: 1) #=> #<Foo:0x0000000106021fc8 @bar="BAR!" @baz=1>
|
42
|
+
FactoryBot.build(:foo, baz: 2) #=> #<Foo:0x0000000106021fc8 @bar="BAR!" @baz=2>
|
43
|
+
FactoryBot.build_stubbed(:foo, baz: 3) #=> #<Foo:0x0000000106021fc8 @bar="BAR!" @baz=3>
|
44
|
+
```
|
45
|
+
|
46
|
+
This DSL has one problem when used with sorbet. It is impossible to create
|
47
|
+
a static signature for `create`, `build`, `build_stubbed` because they
|
48
|
+
return a different type each time based on the first argument.
|
49
|
+
|
50
|
+
### The solution
|
51
|
+
|
52
|
+
This gem defines unique methods for each factory and defines
|
53
|
+
static sorbet signatures for them using a Tapioca compiler.
|
54
|
+
|
55
|
+
```rb
|
56
|
+
FactoryBot::Sorbet.foo(:create, baz: 1) #=> #<Foo:0x0000000106021fc8 @bar="BAR!" @baz=1>
|
57
|
+
FactoryBot::Sorbet.foo(:build, baz: 2) #=> #<Foo:0x0000000106021fc8 @bar="BAR!" @baz=2>
|
58
|
+
FactoryBot::Sorbet.foo(:build_stubbed, baz: 3) #=> #<Foo:0x0000000106021fc8 @bar="BAR!" @baz=3>
|
59
|
+
```
|
60
|
+
Tapioca will generate the following RBI file to make Sorbet fully understand the types of your factories.
|
61
|
+
|
62
|
+
```rb
|
63
|
+
# sorbet/rbi/dsl/factory_bot/sorbet.rbi
|
64
|
+
|
65
|
+
# typed: true
|
66
|
+
|
67
|
+
# DO NOT EDIT MANUALLY
|
68
|
+
# This is an autogenerated file for dynamic methods in `FactoryBot::Sorbet`.
|
69
|
+
# Please instead update this file by running `bin/tapioca dsl FactoryBot::Sorbet`.
|
70
|
+
|
71
|
+
|
72
|
+
module FactoryBot::Sorbet
|
73
|
+
sig { params(strategy: Symbol, args: T.anything, block: NilClass).returns(Foo) }
|
74
|
+
sig do
|
75
|
+
type_parameters(:R)
|
76
|
+
.params(
|
77
|
+
strategy: Symbol,
|
78
|
+
args: T.anything,
|
79
|
+
block: T.proc.params(arg0: Foo).returns(T.type_parameter(:R))
|
80
|
+
).returns(T.type_parameter(:R))
|
81
|
+
end
|
82
|
+
def foo(strategy, *args, &block); end
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
### Test Helpers
|
87
|
+
|
88
|
+
If you use Rails/Minitest (or any other test framework) we highly recommend adding a helper methods
|
89
|
+
for easier access to your factories.
|
90
|
+
|
91
|
+
```rb
|
92
|
+
# test/test_helper.rb
|
93
|
+
|
94
|
+
require 'factory_bot/sorbet'
|
95
|
+
|
96
|
+
class Minitest::Test
|
97
|
+
include FactoryBot::Sorbet::TestHelpers
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
This will the let you use the shorthand:
|
102
|
+
|
103
|
+
```rb
|
104
|
+
f.foo(:create)
|
105
|
+
```
|
106
|
+
|
107
|
+
Instead of:
|
108
|
+
|
109
|
+
```rb
|
110
|
+
FactoryBot::Sorbet.foo(:create)
|
111
|
+
```
|
112
|
+
|
113
|
+
## Development
|
114
|
+
|
115
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
116
|
+
|
117
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
118
|
+
|
119
|
+
## Contributing
|
120
|
+
|
121
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Verseth/ruby-factory_bot-sorbet.
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
::Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << 'test'
|
8
|
+
t.libs << 'lib'
|
9
|
+
t.test_files = ::FileList['test/**/*_test.rb']
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'rubocop/rake_task'
|
13
|
+
|
14
|
+
RuboCop::RakeTask.new
|
15
|
+
|
16
|
+
task default: %i[test]
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# typed: true
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module FactoryBot
|
5
|
+
module Sorbet
|
6
|
+
# @requires_ancestor: singleton(::FactoryBot::Internal)
|
7
|
+
module Internal
|
8
|
+
def register_factory(factory)
|
9
|
+
factory.names.each do |name|
|
10
|
+
Sorbet.module_eval <<~RUBY, __FILE__, __LINE__ + 1
|
11
|
+
def #{name}(kind, *args, **kwargs, &block)
|
12
|
+
::FactoryBot.public_send(kind, #{name.inspect}, *args, **kwargs, &block)
|
13
|
+
end
|
14
|
+
RUBY
|
15
|
+
end
|
16
|
+
|
17
|
+
super
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
Internal.singleton_class.prepend Sorbet::Internal
|
24
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# typed: true
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'factory_bot'
|
5
|
+
require 'booleans'
|
6
|
+
|
7
|
+
require_relative 'sorbet/version'
|
8
|
+
require_relative 'sorbet/override'
|
9
|
+
|
10
|
+
module FactoryBot
|
11
|
+
# Main namespace of the `factory_bot-sorbet` gem
|
12
|
+
# that provides support for static typing with sorbet and tapioca to `factory_bot`.
|
13
|
+
module Sorbet
|
14
|
+
extend Sorbet
|
15
|
+
|
16
|
+
# Contains helper methods for tests.
|
17
|
+
# Specifically the `f` method so you can do:
|
18
|
+
#
|
19
|
+
# f.foo(:create)
|
20
|
+
#
|
21
|
+
# Instead of:
|
22
|
+
#
|
23
|
+
# FactoryBot::Sorbet.foo(:create)
|
24
|
+
#
|
25
|
+
module TestHelpers
|
26
|
+
#: -> singleton(Sorbet)
|
27
|
+
def f = Sorbet
|
28
|
+
end
|
29
|
+
|
30
|
+
class Error < StandardError; end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# typed: true
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'factory_bot/sorbet'
|
5
|
+
|
6
|
+
module Tapioca
|
7
|
+
module Compilers
|
8
|
+
#: [ConstantType = singleton(::FactoryBot::Sorbet)]
|
9
|
+
class FactoryBot < Tapioca::Dsl::Compiler
|
10
|
+
class << self
|
11
|
+
# @override
|
12
|
+
#: -> T::Enumerable[Module]
|
13
|
+
def gather_constants
|
14
|
+
[::FactoryBot::Sorbet]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# @override
|
19
|
+
#: -> void
|
20
|
+
def decorate
|
21
|
+
root.create_path(constant) do |mod|
|
22
|
+
::FactoryBot.factories.each do |factory|
|
23
|
+
begin
|
24
|
+
klass = factory.build_class
|
25
|
+
rescue NameError
|
26
|
+
next
|
27
|
+
end
|
28
|
+
|
29
|
+
mod.create_method(factory.name.to_s) do |method|
|
30
|
+
method.add_param('strategy')
|
31
|
+
method.add_rest_param('args')
|
32
|
+
method.add_block_param('block')
|
33
|
+
|
34
|
+
method.add_sig do |sig|
|
35
|
+
sig.add_param('strategy', 'Symbol')
|
36
|
+
sig.add_param('args', 'T.anything')
|
37
|
+
sig.add_param('block', 'NilClass')
|
38
|
+
sig.return_type = klass.to_s
|
39
|
+
end
|
40
|
+
|
41
|
+
method.add_sig do |sig|
|
42
|
+
sig.type_params << 'R'
|
43
|
+
sig.add_param('strategy', 'Symbol')
|
44
|
+
sig.add_param('args', 'T.anything')
|
45
|
+
sig.add_param('block', "T.proc.params(arg0: #{klass}).returns(T.type_parameter(:R))")
|
46
|
+
sig.return_type = 'T.type_parameter(:R)'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: factory_bot-sorbet
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mateusz Drewniak
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: booleans
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: 0.1.3
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - "~>"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: 0.1.3
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: factory_bot
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6.0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '6.0'
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: sorbet-runtime
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0.5'
|
47
|
+
type: :runtime
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0.5'
|
54
|
+
description: An addon to factory_bot that adds support for static typing with sorbet
|
55
|
+
and tapioca
|
56
|
+
email:
|
57
|
+
- matmg24@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".DS_Store"
|
63
|
+
- CHANGELOG.md
|
64
|
+
- LICENSE
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/factory_bot/annotations.rbi
|
68
|
+
- lib/factory_bot/sorbet.rb
|
69
|
+
- lib/factory_bot/sorbet/override.rb
|
70
|
+
- lib/factory_bot/sorbet/version.rb
|
71
|
+
- lib/tapioca/dsl/compilers/factory_bot.rb
|
72
|
+
homepage: https://github.com/Verseth/ruby-factory_bot-sorbet
|
73
|
+
licenses: []
|
74
|
+
metadata:
|
75
|
+
homepage_uri: https://github.com/Verseth/ruby-factory_bot-sorbet
|
76
|
+
source_code_uri: https://github.com/Verseth/ruby-factory_bot-sorbet
|
77
|
+
changelog_uri: https://raw.githubusercontent.com/Verseth/ruby-factory_bot-sorbet/refs/heads/main/CHANGELOG.md
|
78
|
+
rubygems_mfa_required: 'true'
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: 3.2.0
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubygems_version: 3.7.1
|
94
|
+
specification_version: 4
|
95
|
+
summary: An addon to factory_bot that adds support for static typing with sorbet and
|
96
|
+
tapioca
|
97
|
+
test_files: []
|