fabrication 2.29.0 → 2.30.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: 38d9550f8e85412ca21c540ad7f850a61f9f511dce7048487c8cfc49f965922d
4
+ data.tar.gz: a2e732f74342b3342584483af9b2a446f4bf42fea8af50edbf1d792203e4d16b
5
5
  SHA512:
6
- metadata.gz: 85ae12f9d4e95529411053591e8b80792afd639a32c9de705859f259e3c97725bb3b146c736381ec00a9b1daa16dd678466babc883e2eba797f50322deeac20b
7
- data.tar.gz: cb07e8bf44438241f9500654755f2b350b5646b8e4fbf24b1d840147ffb074103e11bf230ca7e03d92f77c1d2c26d30ab292b70f96186e11a8098f7c230595f8
6
+ metadata.gz: bc4ed3c24fcd1d18ffff850a98d35fafe5408e2b166d234944a82d34e272a9a1fa1da1dff5df6677f7d083ac7cade969f10dcdc95e2fe8cb2b45d3f1719096e8
7
+ data.tar.gz: 28ad7426b1426f3b1f284bf1cf172397b83734b3865f91d64e755995da846f5bf4efc4629e90ee7a800c309e9f39898fac5ff3c87ab2607e7c0a6290d57490f8
@@ -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
@@ -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.30.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.30.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: 2022-07-25 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.
@@ -77,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
77
  - !ruby/object:Gem::Version
78
78
  version: '0'
79
79
  requirements: []
80
- rubygems_version: 3.2.32
80
+ rubygems_version: 3.3.7
81
81
  signing_key:
82
82
  specification_version: 4
83
83
  summary: Generates object instances for test suites, seed files, etc.