factory_bot-with 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/lib/factory_bot/with/assoc_info.rb +4 -2
- data/lib/factory_bot/with/methods.rb +0 -17
- data/lib/factory_bot/with/version.rb +1 -1
- data/lib/factory_bot/with.rb +55 -40
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 330ba97b29d7109bb0cb89cf0493cced5300e17c3a1465066405e46c66cbcd76
|
4
|
+
data.tar.gz: 504cbcee6a16323d2a9494f10bcb68754d84130b343c316eab7a9f4c01432a0c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 11ed1a26a0ebd2590398eef94ebbf8aaeb23419cdd2b0d5e3979f3a64bbe4179e1f96d731804ccbd042535806a4999de62f5a6d5b72cf7ff5c2fbf379cd30372
|
7
|
+
data.tar.gz: 5da17d95d18e8c746ddd5ce0a1213a8cf24dec8766753655ea912bc91a6b7c38dde9c7d85d73bca4165b7d17f126af31c2a4fd71be3cdbb5807c40a0434ef259
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.3.0] - 2024-12-09
|
4
|
+
|
5
|
+
- Added: Added `FactoryBot::With.register_strategy` to support custom strategies
|
6
|
+
|
7
|
+
## [0.2.1] - 2024-11-29
|
8
|
+
|
9
|
+
- Fixed: Adjusted priority of factory names autocompletion
|
10
|
+
|
3
11
|
## [0.2.0] - 2024-11-29
|
4
12
|
|
5
13
|
- Fixed: Fixed an incorrect factory resolution problem after factory names autocompletion
|
@@ -46,8 +46,6 @@ module FactoryBot
|
|
46
46
|
# @param partial_factory_name [Symbol]
|
47
47
|
# @return [Symbol]
|
48
48
|
def autocomplete_fully_qualified_factory_name(ancestors, partial_factory_name)
|
49
|
-
return partial_factory_name if exists?(partial_factory_name)
|
50
|
-
|
51
49
|
ancestors.each do |(ancestor_assoc_info, _)|
|
52
50
|
ancestor_assoc_info.factory_names.each do |ancestor_factory_name|
|
53
51
|
factory_name = :"#{ancestor_factory_name}_#{partial_factory_name}"
|
@@ -55,6 +53,10 @@ module FactoryBot
|
|
55
53
|
end
|
56
54
|
end
|
57
55
|
|
56
|
+
# Attempt to resolve with the autocompleted names, then attempt to resolve with the original name.
|
57
|
+
# If we want to avoid autocompletion, we should be able to simply use a factory such as build or create.
|
58
|
+
return partial_factory_name if exists?(partial_factory_name)
|
59
|
+
|
58
60
|
raise ArgumentError, "FactoryBot factory #{partial_factory_name} is not defined"
|
59
61
|
end
|
60
62
|
|
@@ -5,23 +5,6 @@ module FactoryBot
|
|
5
5
|
# A <code>FactoryBot::Syntax::Methods</code> replacement to enable <code>factory_bot-with</code> features.
|
6
6
|
module Methods
|
7
7
|
include FactoryBot::Syntax::Methods
|
8
|
-
|
9
|
-
BUILD_STRATEGIES = %i[build build_stubbed create attributes_for with].freeze
|
10
|
-
VARIATIONS = {
|
11
|
-
unit: BUILD_STRATEGIES.to_h { [_1, _1] }.freeze,
|
12
|
-
pair: BUILD_STRATEGIES.to_h { [_1, :"#{_1}_pair"] }.freeze,
|
13
|
-
list: BUILD_STRATEGIES.to_h { [_1, :"#{_1}_list"] }.freeze,
|
14
|
-
}.freeze
|
15
|
-
|
16
|
-
VARIATIONS.each do |variation, build_strategies|
|
17
|
-
build_strategies.each do |build_strategy, method_name|
|
18
|
-
define_method(method_name) do |factory = nil, *args, **kwargs, &block|
|
19
|
-
return Proxy.new(self, __method__) unless factory
|
20
|
-
|
21
|
-
With.build(variation, factory, *args, **kwargs, &block).instantiate(build_strategy)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
8
|
end
|
26
9
|
end
|
27
10
|
end
|
data/lib/factory_bot/with.rb
CHANGED
@@ -9,7 +9,7 @@ require_relative "with/methods"
|
|
9
9
|
module FactoryBot
|
10
10
|
# An intermediate state for <code>with</code> operator.
|
11
11
|
class With
|
12
|
-
# @return [:
|
12
|
+
# @return [:singular, :pair, :list]
|
13
13
|
attr_reader :variation
|
14
14
|
# @return [Symbol]
|
15
15
|
attr_reader :factory_name
|
@@ -24,7 +24,7 @@ module FactoryBot
|
|
24
24
|
|
25
25
|
# @!visibility private
|
26
26
|
def initialize(variation, factory_name, withes: [], traits: [], attrs: {}, &block)
|
27
|
-
raise ArgumentError unless %i[
|
27
|
+
raise ArgumentError unless %i[singular pair list].include? variation
|
28
28
|
raise TypeError unless factory_name.is_a? Symbol
|
29
29
|
raise TypeError unless withes.is_a?(Array) && withes.all? { _1.is_a? self.class }
|
30
30
|
raise TypeError unless traits.is_a?(Array) && traits.all? { _1.is_a?(Symbol) || _1.is_a?(Numeric) }
|
@@ -59,18 +59,48 @@ module FactoryBot
|
|
59
59
|
withes: [*withes, *other.withes],
|
60
60
|
traits: [*traits, *other.traits],
|
61
61
|
attrs: { **attrs, **other.attrs },
|
62
|
-
&
|
62
|
+
&lambda do |first, second|
|
63
|
+
return first unless second
|
64
|
+
return second unless first
|
65
|
+
|
66
|
+
lambda do |*args|
|
67
|
+
first.call(*args)
|
68
|
+
second.call(*args)
|
69
|
+
end
|
70
|
+
end.call(block, other.block)
|
63
71
|
)
|
64
72
|
end
|
65
73
|
|
66
74
|
# @!visibility private
|
67
|
-
|
75
|
+
def extend!(*args)
|
76
|
+
args.each do |arg|
|
77
|
+
case arg
|
78
|
+
when self.class
|
79
|
+
withes << arg
|
80
|
+
when Symbol, Numeric
|
81
|
+
traits << arg
|
82
|
+
when Array
|
83
|
+
extend!(*arg)
|
84
|
+
when Hash
|
85
|
+
attrs.merge!(arg)
|
86
|
+
when false, nil
|
87
|
+
# Ignored. This behavior is useful for conditional arguments like `is_premium && :premium`
|
88
|
+
else
|
89
|
+
raise ArgumentError, "Unsupported type for factory argument: #{arg}"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
self
|
93
|
+
end
|
94
|
+
|
95
|
+
# @!visibility private
|
96
|
+
# @param build_strategy [Symbol]
|
68
97
|
# @param ancestors [Array<Array(AssocInfo, Object)>, nil]
|
69
98
|
# @return [Object]
|
70
99
|
def instantiate(build_strategy, ancestors = nil)
|
71
100
|
return self if build_strategy == :with
|
72
101
|
|
73
|
-
factory_bot_method =
|
102
|
+
factory_bot_method =
|
103
|
+
variation == :singular ? build_strategy : :"#{build_strategy}_#{variation}"
|
74
104
|
factory_name, attrs =
|
75
105
|
if ancestors
|
76
106
|
attrs = @attrs.dup
|
@@ -83,7 +113,7 @@ module FactoryBot
|
|
83
113
|
result = FactoryBot.__send__(factory_bot_method, factory_name, *traits, **attrs, &block)
|
84
114
|
|
85
115
|
unless withes.empty?
|
86
|
-
parents = variation == :
|
116
|
+
parents = variation == :singular ? [result] : result
|
87
117
|
assoc_info = AssocInfo.get(factory_name)
|
88
118
|
parents.each do |parent|
|
89
119
|
ancestors_for_children = [[assoc_info, parent], *ancestors || []]
|
@@ -96,51 +126,36 @@ module FactoryBot
|
|
96
126
|
|
97
127
|
class << self
|
98
128
|
# @!visibility private
|
99
|
-
# @param variation [:
|
129
|
+
# @param variation [:singular, :pair, :list]
|
100
130
|
# @param factory [Symbol, With]
|
101
131
|
# @param args [Array<Object>]
|
102
132
|
# @param kwargs [{Symbol => Object}]
|
103
133
|
def build(variation, factory, *, **, &)
|
104
134
|
return factory.merge(build(variation, factory.factory_name, *, **, &)) if factory.is_a? self
|
105
135
|
|
106
|
-
|
107
|
-
insert_args!(with, *)
|
108
|
-
with.attrs.merge!(**)
|
109
|
-
with
|
136
|
+
new(variation, factory, &).extend!(*, { ** })
|
110
137
|
end
|
111
138
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
139
|
+
# If you want to use a custom strategy, call this along with <code>FactoryBot.register_strategy</code>.
|
140
|
+
# @param build_strategy [Symbol]
|
141
|
+
# @example
|
142
|
+
# FactoryBot.register_strategy(:json, JsonStrategy)
|
143
|
+
# FactoryBot::With.register_strategy(:json)
|
144
|
+
def register_strategy(build_strategy)
|
145
|
+
{
|
146
|
+
singular: build_strategy,
|
147
|
+
pair: :"#{build_strategy}_pair",
|
148
|
+
list: :"#{build_strategy}_list",
|
149
|
+
}.each do |variation, method_name|
|
150
|
+
Methods.define_method(method_name) do |factory = nil, *args, **kwargs, &block|
|
151
|
+
return Proxy.new(self, __method__) unless factory
|
152
|
+
|
153
|
+
With.build(variation, factory, *args, **kwargs, &block).instantiate(build_strategy)
|
127
154
|
end
|
128
155
|
end
|
129
156
|
end
|
130
|
-
|
131
|
-
# @!visibility private
|
132
|
-
# @param first [Proc, nil]
|
133
|
-
# @param second [Proc, nil]
|
134
|
-
# @return [Proc, nil]
|
135
|
-
def merge_block(first, second)
|
136
|
-
return first unless second
|
137
|
-
return second unless first
|
138
|
-
|
139
|
-
lambda do |*args|
|
140
|
-
first.call(*args)
|
141
|
-
second.call(*args)
|
142
|
-
end
|
143
|
-
end
|
144
157
|
end
|
158
|
+
|
159
|
+
%i[build build_stubbed create attributes_for with].each { register_strategy _1 }
|
145
160
|
end
|
146
161
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: factory_bot-with
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yubrot
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-12-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: factory_bot
|