factory_bot-blueprint-rspec 0.1.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 383c2e021a8f95d413975221ad71a812573085d87c344d9092489cd4701c7bcc
4
- data.tar.gz: 855d528ff18cfa4e02645fd6eb7a24b65e170918ba759454ccd06bc5c7e14574
3
+ metadata.gz: 6d69a5a7bc30ab7ef24bf30d52470782905063ca277b6a43200dea4889913360
4
+ data.tar.gz: c5260558476aeda1899f059587f5ef1f8367433f550535b09db3f440344ea6a0
5
5
  SHA512:
6
- metadata.gz: 791b93263904413b1bbdc11df06f18c5181fe44882a10c1d9b6c114b1eec33ee3222edf6a52fff90af03aead6e9e04dfedcf22e8410687bd8d11edea92203a26
7
- data.tar.gz: 057af3f3d8bba6f971a4f6e18c40ab69a13829973b38b3bf1c73ddb6477104d704d5d24d524cbf6b567c8f2ebf2c4e1a7587161c3e601256efd9bfb843040f85
6
+ metadata.gz: 2c465be8c2c042766086b3c11a9ceee1924a2b0883c89bb2d4d8372792cfc213fa9241e35694a5f826f5025a485858aa75a63fe52362665f59595d90d183b867
7
+ data.tar.gz: e3355786455ec3222b5fdf97edac2e1ca2c6cfb094afc6148c223ca4454ef9939e6e5ef2138fb4b90e8826573d6953c43536c108b4bfb7d708bcab8b37e30fab
@@ -27,6 +27,7 @@ module FactoryBot
27
27
  # end
28
28
  def let_blueprint(name, inherit: false, &)
29
29
  let(name) { ::FactoryBot::Blueprint.plan(inherit ? super() : nil, ext: self, &) }
30
+ name
30
31
  end
31
32
 
32
33
  # Build objects by <code>build</code> strategy in FactoryBot from a blueprint and declare them using RSpec's
@@ -41,7 +42,7 @@ module FactoryBot
41
42
  # end
42
43
  #
43
44
  # # Simplest example:
44
- # # This is equivalent to `let_blueprint_build blog_bp: { representative: :blog }`
45
+ # # This is equivalent to `let_blueprint_build blog_bp: { result: :blog }`
45
46
  # let_blueprint_build blog_bp: :blog
46
47
  #
47
48
  # # Another shorthand example:
@@ -49,15 +50,15 @@ module FactoryBot
49
50
  # let_blueprint_build blog_bp: %i[blog article]
50
51
  #
51
52
  # # Most flexible example:
52
- # # :representative specifies the name of the representative object to be declared. Defaults to nil
53
+ # # :result specifies the name of the result object to be declared. Defaults to nil
53
54
  # # :items specifies the names of the objects to be declared. Defaults to []
54
55
  # # :instance specifies the name of the instance object to be declared. Defaults to :"#{source}_instance"
55
- # let_blueprint_build blog_bp: { representative: :blog, items: %i[article], instance: :blog_instance }
56
+ # let_blueprint_build blog_bp: { result: :blog, items: %i[article], instance: :blog_instance }
56
57
  #
57
58
  # # Above example will be expanded to:
58
59
  # let(:blog_instance) { ::FactoryBot::Blueprint.build(blog_bp) } # the instance object
59
- # let(:blog) { blog_instance[blog_bp.representative_node.name] } # the representative object
60
- # let(:article) { blog_instance[:article] } # the item objects
60
+ # let(:blog) { blog_instance[0] } # the result object
61
+ # let(:article) { blog_instance[1][:article] } # the item objects
61
62
  # end
62
63
  def let_blueprint_build(**map) = let_blueprint_instantiate(:build, **map)
63
64
 
@@ -73,13 +74,13 @@ module FactoryBot
73
74
  def let_blueprint_instantiate(strategy, **map)
74
75
  raise ArgumentError, "Unsupported strategy: #{strategy}" if strategy && !%i[create build].include?(strategy)
75
76
 
76
- map.each do |source, definition|
77
+ map.map do |source, definition|
77
78
  raise TypeError, "source must be a Symbol" unless source.is_a?(Symbol)
78
79
 
79
80
  definition =
80
81
  case definition
81
82
  when Symbol
82
- { representative: definition }
83
+ { result: definition }
83
84
  when Array
84
85
  { items: definition }
85
86
  when Hash
@@ -88,13 +89,11 @@ module FactoryBot
88
89
  raise TypeError, "definition must be one of Symbol, Array, Hash"
89
90
  end
90
91
 
91
- representative_name = definition[:representative]
92
+ result_name = definition[:result]
92
93
  item_names = definition[:items] || []
93
94
  instance = definition[:instance] || :"#{source}_instance"
94
95
 
95
- if representative_name && !representative_name.is_a?(Symbol)
96
- raise TypeError, "representative must be a Symbol"
97
- end
96
+ raise TypeError, "result must be a Symbol" if result_name && !result_name.is_a?(Symbol)
98
97
  if !item_names.is_a?(Array) || !item_names.all? { _1.is_a?(Symbol) }
99
98
  raise TypeError, "items must be an Array of Symbols"
100
99
  end
@@ -104,13 +103,11 @@ module FactoryBot
104
103
  let(instance) { ::FactoryBot::Blueprint.instantiate(strategy, __send__(source)) }
105
104
  end
106
105
 
107
- if representative_name
108
- let(representative_name) { __send__(instance)[__send__(source).representative_node.name] }
109
- end
106
+ let(result_name) { __send__(instance)[0] } if result_name
110
107
 
111
- item_names.each do |name|
112
- let(name) { __send__(instance)[name] }
113
- end
108
+ item_names.each { |name| let(name) { __send__(instance)[1][name] } }
109
+
110
+ instance
114
111
  end
115
112
  end
116
113
 
@@ -119,7 +116,7 @@ module FactoryBot
119
116
  #
120
117
  # This is a shorthand for {#let_blueprint} with {#let_blueprint_build} or {#let_blueprint_create}.
121
118
  # @param name [Symbol]
122
- # name of the representative object to be declared using RSpec's <code>let</code>.
119
+ # name of the result object to be declared using RSpec's <code>let</code>.
123
120
  # It is also used as a name prefix of the blueprint
124
121
  # @param items [Array<Symbol>] names of the objects to be declared using RSpec's <code>let</code>
125
122
  # @param inherit [Boolean] whether to extend the blueprint by <code>super()</code>
@@ -145,7 +142,7 @@ module FactoryBot
145
142
  # article(title: "Article 3")
146
143
  # end
147
144
  # end
148
- # let_blueprint_create blog_blueprint: { representative: :blog, items: %i[article] }
145
+ # let_blueprint_create blog_blueprint: { result: :blog, items: %i[article] }
149
146
  # end
150
147
  def letbp(name, items = [], inherit: false, strategy: :create, &)
151
148
  raise TypeError, "name must be a Symbol" unless name.is_a?(Symbol)
@@ -154,7 +151,31 @@ module FactoryBot
154
151
  strategy = nil if inherit
155
152
 
156
153
  let_blueprint(source, inherit:, &)
157
- let_blueprint_instantiate strategy, source => { representative: name, items: }
154
+ let_blueprint_instantiate strategy, source => { result: name, items: }
155
+ end
156
+
157
+ # <code>let!</code> version of {#let_blueprint}.
158
+ def let_blueprint!(...)
159
+ name = let_blueprint(...)
160
+ before { __send__(name) }
161
+ end
162
+
163
+ # <code>let!</code> version of {#let_blueprint_build}.
164
+ def let_blueprint_build!(...)
165
+ names = let_blueprint_build(...)
166
+ before { names.each { __send__(_1) } }
167
+ end
168
+
169
+ # <code>let!</code> version of {#let_blueprint_create}.
170
+ def let_blueprint_create!(...)
171
+ names = let_blueprint_create(...)
172
+ before { names.each { __send__(_1) } }
173
+ end
174
+
175
+ # <code>let!</code> version of {#letbp}.
176
+ def letbp!(...)
177
+ names = letbp(...)
178
+ before { names.each { __send__(_1) } }
158
179
  end
159
180
  end
160
181
  end
@@ -3,7 +3,7 @@
3
3
  module FactoryBot
4
4
  module Blueprint
5
5
  module RSpec
6
- VERSION = "0.1.0"
6
+ VERSION = "0.3.0"
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_bot-blueprint-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.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-07-22 00:00:00.000000000 Z
11
+ date: 2024-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: factory_bot-blueprint
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.0
19
+ version: 0.3.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.1.0
26
+ version: 0.3.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec-core
29
29
  requirement: !ruby/object:Gem::Requirement