fabrication 2.23.0 → 2.26.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: af00199fb86206bfaecee18e88668f63446301f7bfff776831ad26a18c092187
4
- data.tar.gz: b63b7eb25cf5e98f9051befe7ffdb2c994a07c149a32af93e4f48b183f17fdfe
3
+ metadata.gz: 1fa068a804c3a1374125c849657ee519ce7b08662ea994c3a9b357e1a74ce56c
4
+ data.tar.gz: 94031042eb41382a40aa39877abfadaa3664893e3d5c94946a1022a483b68d93
5
5
  SHA512:
6
- metadata.gz: bfc49c073e8a19855863b5da27ff6c45aa45cd80eea134611b40174fb9c4ac2f9df6ee44e8dd3613001c7662a4e1ece7db0d2df78a8b025d647546d0f0fa137d
7
- data.tar.gz: b8802428e05897ec7a0b6dc0a0a01f4d0c33437d5b3d4a895e116664e00fd7f79904143cfd9a6c8db4fb135d6f0c1c4c54754fb7b35fc47d7962209a1fe7eb6a
6
+ metadata.gz: d5ca378dd07da6affa590ba5a798e91d2261228af1d016066c6fb82e686f9a8d6fb397e5080ba819a26884e2d17479c408de0ae88e83607c8a57f54c997bd78c
7
+ data.tar.gz: 496347a6e1279a2a62c10e6b77e9e4a9a2fc5ab32b6654b62fdb2a330b33cb8fe8712a56059540694d16bb221ef1ef1131e2f982f76880b3e9eb6721dc5efa02
data/lib/fabricate.rb CHANGED
@@ -1,14 +1,14 @@
1
1
  class Fabricate
2
2
  def self.times(count, name, overrides = {}, &block)
3
- count.times.map { Fabricate(name, overrides, &block) }
3
+ Array.new(count).map { Fabricate(name, overrides, &block) }
4
4
  end
5
5
 
6
6
  def self.build_times(count, name, overrides = {}, &block)
7
- count.times.map { Fabricate.build(name, overrides, &block) }
7
+ Array.new(count).map { Fabricate.build(name, overrides, &block) }
8
8
  end
9
9
 
10
10
  def self.attributes_for_times(count, name, overrides = {}, &block)
11
- count.times.map { Fabricate.attributes_for(name, overrides, &block) }
11
+ Array.new(count).map { Fabricate.attributes_for(name, overrides, &block) }
12
12
  end
13
13
 
14
14
  def self.attributes_for(name, overrides = {}, &block)
@@ -24,13 +24,19 @@ class Fabricate
24
24
  def self.build(name, overrides = {}, &block)
25
25
  fail_if_initializing(name)
26
26
  schematic(name).build(overrides, &block).tap do |object|
27
- Fabrication::Cucumber::Fabrications[name] = object if Fabrication::Config.register_with_steps?
27
+ Fabrication::Config.notifiers.each do |notifier|
28
+ notifier.call(name, object)
29
+ end
28
30
  end
29
31
  end
30
32
 
31
33
  def self.create(name, overrides = {}, &block)
32
34
  fail_if_initializing(name)
33
- schematic(name).fabricate(overrides, &block)
35
+ schematic(name).fabricate(overrides, &block).tap do |object|
36
+ Fabrication::Config.notifiers.each do |notifier|
37
+ notifier.call(name, object)
38
+ end
39
+ end
34
40
  end
35
41
 
36
42
  def self.sequence(name = Fabrication::Sequencer::DEFAULT, start = nil, &block)
@@ -35,7 +35,7 @@ module Fabrication
35
35
  self.fabricator_path = folders
36
36
  end
37
37
 
38
- attr_writer :sequence_start, :register_with_steps
38
+ attr_writer :sequence_start
39
39
 
40
40
  def sequence_start
41
41
  @sequence_start ||= 0
@@ -50,10 +50,6 @@ module Fabrication
50
50
  end
51
51
  alias path_prefixes path_prefix
52
52
 
53
- def register_with_steps?
54
- @register_with_steps ||= nil
55
- end
56
-
57
53
  def generators
58
54
  @generators ||= []
59
55
  end
@@ -69,5 +65,24 @@ module Fabrication
69
65
  def recursion_limit=(limit)
70
66
  @recursion_limit = limit
71
67
  end
68
+
69
+ def register_with_steps=(value)
70
+ puts 'DEPRECATION WARNING: Fabrication::Config.register_with_steps has been ' \
71
+ 'deprecated. Please regenerate your cucumber steps with `rails g fabrication:cucumber_steps'
72
+
73
+ return unless value
74
+
75
+ register_notifier do |name, object|
76
+ Fabrication::Cucumber::Fabrications[name] = object
77
+ end
78
+ end
79
+
80
+ def notifiers
81
+ @notifiers ||= []
82
+ end
83
+
84
+ def register_notifier(&block)
85
+ notifiers.push(block)
86
+ end
72
87
  end
73
88
  end
@@ -20,7 +20,7 @@ module Fabrication
20
20
  end
21
21
 
22
22
  def n(count, attrs = {})
23
- count.times.map { make(attrs) }.tap { |o| remember(o) }
23
+ Array.new(count).map { make(attrs) }.tap { |o| remember(o) }
24
24
  end
25
25
 
26
26
  # rubocop:disable Naming/PredicateName
@@ -54,12 +54,12 @@ module Fabrication
54
54
  end
55
55
 
56
56
  def build_instance_with_constructor_override(callback)
57
- self._instance = instance_eval(&callback)
57
+ self._instance = instance_exec(_transient_attributes, &callback)
58
58
  set_attributes
59
59
  end
60
60
 
61
61
  def build_instance_with_init_callback(callback)
62
- self._instance = _klass.new(*callback.call)
62
+ self._instance = _klass.new(*callback.call(_transient_attributes))
63
63
  set_attributes
64
64
  end
65
65
 
@@ -69,12 +69,8 @@ module Fabrication
69
69
  end
70
70
 
71
71
  def set_attributes
72
- if _instance.respond_to?(:attributes=)
73
- _instance.attributes = _attributes
74
- else
75
- _attributes.each do |k, v|
76
- _instance.send("#{k}=", v)
77
- end
72
+ _attributes.each do |k, v|
73
+ _instance.send("#{k}=", v)
78
74
  end
79
75
  end
80
76
 
@@ -108,7 +104,7 @@ module Fabrication
108
104
  _attributes[attribute.name] = attribute.processed_value(_attributes)
109
105
  _transient_attributes[attribute.name] = _attributes[attribute.name] if attribute.transient?
110
106
  end
111
- _attributes.reject! { |k| _transient_attributes.keys.include?(k) }
107
+ _attributes.reject! { |k| _transient_attributes.key?(k) }
112
108
  end
113
109
  end
114
110
  end
@@ -0,0 +1,17 @@
1
+ module Fabrication
2
+ module Generator
3
+ class Mongoid < Fabrication::Generator::Base
4
+ def self.supports?(klass)
5
+ defined?(::Mongoid) && klass.ancestors.include?(::Mongoid::Document)
6
+ end
7
+
8
+ def build_instance
9
+ self._instance = if _klass.respond_to?(:protected_attributes)
10
+ _klass.new(_attributes, without_protection: true)
11
+ else
12
+ _klass.new(_attributes)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -5,6 +5,7 @@ module Fabrication
5
5
  Fabrication::Generator::ActiveRecord,
6
6
  Fabrication::Generator::DataMapper,
7
7
  Fabrication::Generator::Sequel,
8
+ Fabrication::Generator::Mongoid,
8
9
  Fabrication::Generator::Base
9
10
  ].freeze
10
11
 
@@ -17,7 +18,7 @@ module Fabrication
17
18
  end
18
19
 
19
20
  def process_block(&block)
20
- Fabrication::Schematic::Evaluator.new.process(self, &block) if block_given?
21
+ Fabrication::Schematic::Evaluator.new.process(self, &block) if block
21
22
  end
22
23
 
23
24
  def attribute(name)
@@ -55,14 +55,16 @@ module Fabrication
55
55
  def load_definitions
56
56
  preinitialize
57
57
  Fabrication::Config.path_prefixes.each do |prefix|
58
- Fabrication::Config.fabricator_paths.each do |folder|
59
- Dir.glob(File.join(prefix.to_s, folder, '**', '*.rb')).sort.each do |file|
60
- load file
58
+ Fabrication::Config.fabricator_paths.each do |path|
59
+ if File.file?(path)
60
+ load path
61
+ else
62
+ Dir.glob(File.join(prefix.to_s, path, '**', '*.rb')).sort.each do |file|
63
+ load file
64
+ end
61
65
  end
62
66
  end
63
67
  end
64
- rescue StandardError => e
65
- raise e
66
68
  ensure
67
69
  freeze
68
70
  end
@@ -4,7 +4,7 @@ module Fabrication
4
4
 
5
5
  def self.sequence(name = DEFAULT, start = nil, &block)
6
6
  idx = sequences[name] ||= start || Fabrication::Config.sequence_start
7
- if block_given?
7
+ if block
8
8
  sequence_blocks[name] = block.to_proc
9
9
  else
10
10
  sequence_blocks[name] ||= ->(i) { i }
@@ -1,3 +1,3 @@
1
1
  module Fabrication
2
- VERSION = '2.23.0'.freeze
2
+ VERSION = '2.26.0'.freeze
3
3
  end
data/lib/fabrication.rb CHANGED
@@ -33,6 +33,7 @@ module Fabrication
33
33
  autoload :ActiveRecord, 'fabrication/generator/active_record'
34
34
  autoload :ActiveRecord4, 'fabrication/generator/active_record_4'
35
35
  autoload :DataMapper, 'fabrication/generator/data_mapper'
36
+ autoload :Mongoid, 'fabrication/generator/mongoid'
36
37
  autoload :Sequel, 'fabrication/generator/sequel'
37
38
  autoload :Base, 'fabrication/generator/base'
38
39
  end
@@ -63,9 +64,7 @@ def Fabricator(name, options = {}, &block)
63
64
  end
64
65
 
65
66
  def Fabricate(name, overrides = {}, &block)
66
- Fabricate.create(name, overrides, &block).tap do |object|
67
- Fabrication::Cucumber::Fabrications[name] = object if Fabrication::Config.register_with_steps?
68
- end
67
+ Fabricate.create(name, overrides, &block)
69
68
  end
70
69
  # rubocop:enable Naming/MethodName
71
70
 
@@ -1,6 +1,8 @@
1
1
  World(FabricationMethods)
2
2
 
3
- Fabrication::Config.register_with_steps = true
3
+ Fabrication::Config.register_notifier do |name, object|
4
+ Fabrication::Cucumber::Fabrications[name] = object
5
+ end
4
6
 
5
7
  def with_ivars(fabricator)
6
8
  @they = yield fabricator
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fabrication
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.23.0
4
+ version: 2.26.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Elliott
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-06 00:00:00.000000000 Z
11
+ date: 2022-02-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Fabrication is an object generation framework for ActiveRecord, Mongoid,
14
14
  DataMapper, Sequel, or any other Ruby object.
@@ -33,6 +33,7 @@ files:
33
33
  - lib/fabrication/generator/active_record.rb
34
34
  - lib/fabrication/generator/base.rb
35
35
  - lib/fabrication/generator/data_mapper.rb
36
+ - lib/fabrication/generator/mongoid.rb
36
37
  - lib/fabrication/generator/sequel.rb
37
38
  - lib/fabrication/railtie.rb
38
39
  - lib/fabrication/schematic/attribute.rb
@@ -69,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
70
  - !ruby/object:Gem::Version
70
71
  version: '0'
71
72
  requirements: []
72
- rubygems_version: 3.2.22
73
+ rubygems_version: 3.2.32
73
74
  signing_key:
74
75
  specification_version: 4
75
76
  summary: Implementing the factory pattern in Ruby so you don't have to.