factory_bot-blueprint 0.2.0 → 0.4.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: a787d266dd765ed467151942c7aa1dfef462c1155a1bbfc5e6c6f7ae664faff4
4
- data.tar.gz: e0dba6052140c4f03213de646ffba6c686b7593135e46674a91800b1583dbef5
3
+ metadata.gz: 8f0c7f0e3747308e1e3eecd28cbd960aa98b65ae2c08b55b7e6346d1b953b064
4
+ data.tar.gz: dcc1a57961d57b1ce4f29f8b2f83f659ccfa9ae19781903516b698d75dedbcdb
5
5
  SHA512:
6
- metadata.gz: e2738e28af183c6d379e9361c2dd3218b2b0f840ad498e0a74131014378db687ac08b3fe8862943fa83f921770af0af7df7ddf87e57cea6106b81f672a89dc4f
7
- data.tar.gz: d436c8b2aebf96496da6f493e94e753a99f62900e4d516186c6a8851ccd67567539927111c29222f6a816e1db93ac1c47a88d7f75ef30dba6b5738834913b573
6
+ metadata.gz: e5fb41444b1b0ca831a651deb8a70c7bfd02409b5536a6f5c7367b34abade51463bb03bcc562fc190399f54a493a55564d589d10b3c31ff7207160f5f5da8257
7
+ data.tar.gz: 784d7af975f12da0a6f576c5dccb3e3ccd121e4dd0826280ec6b9a9cbc2f19df8610c450b34263d0b1252367918584750544ac542ca41e761f4c6ce8ca868d8c
@@ -10,16 +10,38 @@ module FactoryBot
10
10
  # We would like to minimize these dependencies as much as possible.
11
11
 
12
12
  # @!visibility private
13
- def respond_to_missing?(type_name, _)
14
- FactoryBot.factories.registered? type_name
13
+ def respond_to_missing?(name, _)
14
+ _autocompleted_method_names(name).any? do |method_name|
15
+ self.class.method_defined?(method_name) || FactoryBot.factories.registered?(method_name)
16
+ end
15
17
  end
16
18
 
17
- def method_missing(type_name, ...)
18
- raise NoMethodError, "Unknown type #{type_name}" unless FactoryBot.factories.registered? type_name
19
+ def method_missing(name, ...)
20
+ _autocompleted_method_names(name).each do |method_name|
21
+ if self.class.method_defined?(method_name)
22
+ return __send__(method_name, ...)
23
+ elsif FactoryBot.factories.registered?(method_name)
24
+ factory = FactoryBot.factories.find(method_name)
25
+ self.class.add_type(self.class.type_from_factory_bot_factory(method_name, factory))
26
+ return __send__(method_name, ...)
27
+ end
28
+ end
29
+
30
+ raise NoMethodError,
31
+ "Undefined method `#{name}' for #{self} and cannot be resolved from FactoryBot factories"
32
+ end
19
33
 
20
- factory = FactoryBot.factories.find(type_name)
21
- self.class.add_type(self.class.type_from_factory_bot_factory(type_name, factory))
22
- __send__(type_name, ...)
34
+ private
35
+
36
+ def _autocompleted_method_names(name)
37
+ return enum_for(__method__, name) unless block_given?
38
+
39
+ @ancestors.reverse_each do |ancestor|
40
+ ancestor.type.compatible_types.each do |ancestor_type|
41
+ yield :"#{ancestor_type}_#{name}"
42
+ end
43
+ end
44
+ yield name
23
45
  end
24
46
 
25
47
  class << self
@@ -2,6 +2,6 @@
2
2
 
3
3
  module FactoryBot
4
4
  module Blueprint
5
- VERSION = "0.2.0"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
@@ -41,33 +41,33 @@ module FactoryBot
41
41
  # end
42
42
  #
43
43
  # # Create a set of objects in FactoryBot (with `build` strategy) from the blueprint:
44
- # result, objects = FactoryBot::Blueprint.build(bp)
44
+ # objects = FactoryBot::Blueprint.build(bp)
45
45
  #
46
46
  # # This behaves as:
47
47
  # objects = {}
48
48
  # objects[:blog] = blog = FactoryBot.build(:blog)
49
- # objects[gen_random_sym] = FactoryBot.build(:article, title: "Article 1", blog:)
50
- # objects[gen_random_sym] = FactoryBot.build(:article, title: "Article 2", blog:)
51
- # objects[gen_random_sym] = article3 = FactoryBot.build(:article, title: "Article 3", blog:)
52
- # objects[gen_random_sym] = FactoryBot.build(:comment, name: "John", article: article3)
53
- # objects[gen_random_sym] = FactoryBot.build(:comment, name: "Doe", article: article3)
54
- # result = blog
49
+ # objects[random_anon_sym] = FactoryBot.build(:article, title: "Article 1", blog:)
50
+ # objects[random_anon_sym] = FactoryBot.build(:article, title: "Article 2", blog:)
51
+ # objects[random_anon_sym] = article3 = FactoryBot.build(:article, title: "Article 3", blog:)
52
+ # objects[random_anon_sym] = FactoryBot.build(:comment, name: "John", article: article3)
53
+ # objects[random_anon_sym] = FactoryBot.build(:comment, name: "Doe", article: article3)
54
+ # objects[Factrey::Blueprint::Node::RESULT_NAME] = blog
55
55
  def plan(blueprint = nil, ext: nil, &) = Factrey.blueprint(blueprint, ext:, dsl: DSL, &)
56
56
 
57
- # Create a set of objects and compute the result by <code>build</code> strategy in FactoryBot.
57
+ # Create a set of objects by <code>build</code> strategy in FactoryBot.
58
58
  # See {.plan} for more details.
59
59
  # @param blueprint [Factrey::Blueprint, nil]
60
60
  # @param ext [Object] an external object that can be accessed using {DSL#ext} in the DSL
61
61
  # @yield Write Blueprint DSL code here
62
- # @return [(Object, {Symbol => Object})] the result and the created objects
62
+ # @return [Hash{Symbol => Object}] the created objects
63
63
  def build(blueprint = nil, ext: nil, &) = instantiate(:build, blueprint, ext:, &)
64
64
 
65
- # Create a set of objects and compute the result by <code>create</code> strategy in FactoryBot.
65
+ # Create a set of objects by <code>create</code> strategy in FactoryBot.
66
66
  # See {.plan} for more details.
67
67
  # @param blueprint [Factrey::Blueprint, nil]
68
68
  # @param ext [Object] an external object that can be accessed using {DSL#ext} in the DSL
69
69
  # @yield Write Blueprint DSL code here
70
- # @return [(Object, {Symbol => Object})] the result and the created objects
70
+ # @return [Hash{Symbol => Object}] the created objects
71
71
  def create(blueprint = nil, ext: nil, &) = instantiate(:create, blueprint, ext:, &)
72
72
 
73
73
  # @!visibility private
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_bot-blueprint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.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-29 00:00:00.000000000 Z
11
+ date: 2024-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: factory_bot
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.2.0
33
+ version: 0.4.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.2.0
40
+ version: 0.4.0
41
41
  description: |
42
42
  FactoryBot::Blueprint is a FactoryBot extension for building structured objects using a declarative DSL.
43
43
  On the DSL, the factories defined in FactoryBot can be used without any additional configuration.