fabrication 2.29.0 → 2.31.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c81aec7d3bdf1fd43d6b18d57edeb83b408f099eb403eb31b2caa17c715fa32e
4
- data.tar.gz: 5945e9f77ced15f5de56749ae48c3b6f121ae4e3d941784ada4640d774957a08
3
+ metadata.gz: 3efaba7356d0a3fcade7fe08a68719f8d0f452bc8cd72a88e087c10434931745
4
+ data.tar.gz: 1beb71f4903b74255a06c46ee6ca15e464d3ed6b0400a02ba01cd65cefd205a7
5
5
  SHA512:
6
- metadata.gz: 85ae12f9d4e95529411053591e8b80792afd639a32c9de705859f259e3c97725bb3b146c736381ec00a9b1daa16dd678466babc883e2eba797f50322deeac20b
7
- data.tar.gz: cb07e8bf44438241f9500654755f2b350b5646b8e4fbf24b1d840147ffb074103e11bf230ca7e03d92f77c1d2c26d30ab292b70f96186e11a8098f7c230595f8
6
+ metadata.gz: 04bd08bc59c91ff463cac12869d0a4211b969f50635748e830842e62198c9f9ec7dad7bebbc1db32d6e65fd46d76559041da27c689d067d65835978ade6fb86d
7
+ data.tar.gz: 6a4259285d0c8f8787df5a8a6d9dcfe823de65282a50873e06d0660b94bd3f0051a0bfd5e64f6fe6b809637782404646e55c0889a793fd9f70dd7b19ef43a70c
@@ -11,6 +11,9 @@ module Fabrication
11
11
  if callbacks[:initialize_with]
12
12
  build_instance_with_constructor_override(callbacks[:initialize_with])
13
13
  elsif callbacks[:on_init]
14
+ Fabrication::Support.log_deprecation(
15
+ 'The on_init callback has been replaced by initialize_with. Please see the documentation for usage'
16
+ )
14
17
  build_instance_with_init_callback(callbacks[:on_init])
15
18
  else
16
19
  build_instance
@@ -19,18 +22,29 @@ module Fabrication
19
22
  _instance
20
23
  end
21
24
 
22
- def create(attributes = [], callbacks = [])
25
+ def create(attributes = [], callbacks = {})
23
26
  build(attributes, callbacks)
24
- execute_callbacks(callbacks[:before_validation])
25
- execute_callbacks(callbacks[:after_validation])
26
- execute_callbacks(callbacks[:before_save])
27
+ execute_deprecated_callbacks(callbacks, :before_validation, :before_create)
28
+ execute_deprecated_callbacks(callbacks, :after_validation, :before_create)
29
+ execute_deprecated_callbacks(callbacks, :before_save, :before_create)
27
30
  execute_callbacks(callbacks[:before_create])
28
31
  persist
29
32
  execute_callbacks(callbacks[:after_create])
30
- execute_callbacks(callbacks[:after_save])
33
+ execute_deprecated_callbacks(callbacks, :after_save, :after_create)
31
34
  _instance
32
35
  end
33
36
 
37
+ def execute_deprecated_callbacks(callbacks, callback_type, replacement_callback)
38
+ if callbacks[callback_type]
39
+ Fabrication::Support.log_deprecation(
40
+ "Using #{callback_type} is deprecated but you can replace it " \
41
+ "with #{replacement_callback} with the same result."
42
+ )
43
+ end
44
+
45
+ execute_callbacks(callbacks[callback_type])
46
+ end
47
+
34
48
  def execute_callbacks(callbacks)
35
49
  callbacks&.each { |callback| _instance.instance_exec(_instance, _transient_attributes, &callback) }
36
50
  end
@@ -96,18 +110,21 @@ module Fabrication
96
110
 
97
111
  protected
98
112
 
99
- attr_accessor :resolved_class, :_instance, :_transient_attributes
113
+ attr_accessor :resolved_class, :_instance
100
114
 
101
115
  def _attributes
102
116
  @_attributes ||= {}
103
117
  end
104
118
 
119
+ def _transient_attributes
120
+ @_transient_attributes ||= {}
121
+ end
122
+
105
123
  def persist
106
124
  _instance.save! if _instance.respond_to?(:save!)
107
125
  end
108
126
 
109
127
  def process_attributes(attributes)
110
- self._transient_attributes = ({})
111
128
  attributes.each do |attribute|
112
129
  _attributes[attribute.name] = attribute.processed_value(_attributes)
113
130
  _transient_attributes[attribute.name] = _attributes[attribute.name] if attribute.transient?
@@ -11,14 +11,11 @@ module Fabrication
11
11
  end
12
12
 
13
13
  def set_attributes
14
- _attributes.each do |key, value|
15
- if (reflection = resolved_class.association_reflections[key]) && value.is_a?(Array)
16
- _instance.associations[key] = value
17
- _instance.after_save_hook do
18
- value.each { |o| _instance.send(reflection.add_method, o) }
19
- end
14
+ _attributes.each do |field_name, value|
15
+ if value.is_a?(Array) && (association = association_for(field_name))
16
+ set_association(association, field_name, value)
20
17
  else
21
- _instance.send("#{key}=", value)
18
+ set_attribute(field_name, value)
22
19
  end
23
20
  end
24
21
  end
@@ -29,6 +26,21 @@ module Fabrication
29
26
 
30
27
  private
31
28
 
29
+ def association_for(field_name)
30
+ resolved_class.association_reflections[field_name]
31
+ end
32
+
33
+ def set_attribute(field_name, value)
34
+ _instance.send("#{field_name}=", value)
35
+ end
36
+
37
+ def set_association(association, field_name, value)
38
+ _instance.associations[field_name] = value
39
+ _instance.after_save_hook do
40
+ value.each { |o| _instance.send(association.add_method, o) }
41
+ end
42
+ end
43
+
32
44
  def load_instance_hooks
33
45
  klass = resolved_class.respond_to?(:cti_base_model) ? resolved_class.cti_models.first : resolved_class
34
46
  klass.plugin :instance_hooks unless klass.new.respond_to? :after_save_hook
@@ -59,7 +59,7 @@ module Fabrication
59
59
  if File.file?(path)
60
60
  load path
61
61
  else
62
- Dir.glob(File.join(prefix.to_s, path, '**', '*.rb')).sort.each do |file|
62
+ Dir.glob(File.join(prefix.to_s, path, '**', '*.rb')).each do |file|
63
63
  load file
64
64
  end
65
65
  end
@@ -22,7 +22,7 @@ module Fabrication
22
22
  else
23
23
  sequence_blocks[name] ||= ->(i) { i }
24
24
  end.call(idx).tap do
25
- sequences[name] += 1
25
+ sequences[name] = idx.succ
26
26
  end
27
27
  end
28
28
 
@@ -34,9 +34,9 @@ module Fabrication
34
34
  if name_string.respond_to?(:camelize)
35
35
  name_string.camelize
36
36
  else
37
- name_string.gsub(%r{/(.?)}) do
38
- "::#{Regexp.last_match(1).upcase}"
39
- end.gsub(/(?:^|_)(.)/) { Regexp.last_match(1).upcase }
37
+ name_string
38
+ .gsub(%r{/(.?)}) { "::#{Regexp.last_match(1).upcase}" }
39
+ .gsub(/(?:^|_)(.)/) { Regexp.last_match(1).upcase }
40
40
  end
41
41
  end
42
42
 
@@ -1,3 +1,3 @@
1
1
  module Fabrication
2
- VERSION = '2.29.0'.freeze
2
+ VERSION = '2.31.0'.freeze
3
3
  end
data/lib/fabrication.rb CHANGED
@@ -51,7 +51,7 @@ module Fabrication
51
51
  end
52
52
 
53
53
  def self.schematics
54
- Support.log_deprecation('Fabrication.schematics has been replaced by '\
54
+ Support.log_deprecation('Fabrication.schematics has been replaced by ' \
55
55
  'Fabrication.manager and will be removed in 3.0.0.')
56
56
  manager
57
57
  end
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.29.0
4
+ version: 2.31.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: 2022-06-22 00:00:00.000000000 Z
11
+ date: 2023-11-27 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.
@@ -70,14 +70,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
70
70
  requirements:
71
71
  - - ">="
72
72
  - !ruby/object:Gem::Version
73
- version: 2.7.0
73
+ version: 3.0.0
74
74
  required_rubygems_version: !ruby/object:Gem::Requirement
75
75
  requirements:
76
76
  - - ">="
77
77
  - !ruby/object:Gem::Version
78
78
  version: '0'
79
79
  requirements: []
80
- rubygems_version: 3.2.32
80
+ rubygems_version: 3.4.10
81
81
  signing_key:
82
82
  specification_version: 4
83
83
  summary: Generates object instances for test suites, seed files, etc.