sequel 5.41.0 → 5.46.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +46 -0
- data/README.rdoc +1 -2
- data/doc/association_basics.rdoc +22 -3
- data/doc/release_notes/5.42.0.txt +136 -0
- data/doc/release_notes/5.43.0.txt +98 -0
- data/doc/release_notes/5.44.0.txt +32 -0
- data/doc/release_notes/5.45.0.txt +34 -0
- data/doc/release_notes/5.46.0.txt +87 -0
- data/doc/testing.rdoc +3 -0
- data/doc/virtual_rows.rdoc +1 -1
- data/lib/sequel/adapters/ado.rb +16 -16
- data/lib/sequel/adapters/odbc.rb +5 -1
- data/lib/sequel/adapters/shared/postgres.rb +0 -12
- data/lib/sequel/adapters/shared/sqlite.rb +8 -4
- data/lib/sequel/core.rb +11 -0
- data/lib/sequel/database/misc.rb +1 -2
- data/lib/sequel/database/schema_generator.rb +35 -47
- data/lib/sequel/database/schema_methods.rb +4 -0
- data/lib/sequel/dataset/query.rb +1 -3
- data/lib/sequel/dataset/sql.rb +7 -0
- data/lib/sequel/extensions/async_thread_pool.rb +438 -0
- data/lib/sequel/extensions/date_arithmetic.rb +29 -16
- data/lib/sequel/extensions/pg_enum.rb +1 -1
- data/lib/sequel/extensions/pg_loose_count.rb +3 -1
- data/lib/sequel/model/associations.rb +146 -75
- data/lib/sequel/model/base.rb +2 -2
- data/lib/sequel/plugins/async_thread_pool.rb +39 -0
- data/lib/sequel/plugins/auto_validations_constraint_validations_presence_message.rb +68 -0
- data/lib/sequel/plugins/column_encryption.rb +728 -0
- data/lib/sequel/plugins/composition.rb +2 -1
- data/lib/sequel/plugins/concurrent_eager_loading.rb +174 -0
- data/lib/sequel/plugins/json_serializer.rb +37 -22
- data/lib/sequel/plugins/nested_attributes.rb +5 -2
- data/lib/sequel/plugins/pg_array_associations.rb +52 -38
- data/lib/sequel/plugins/rcte_tree.rb +27 -19
- data/lib/sequel/plugins/serialization.rb +8 -3
- data/lib/sequel/plugins/serialization_modification_detection.rb +1 -1
- data/lib/sequel/plugins/unused_associations.rb +500 -0
- data/lib/sequel/version.rb +1 -1
- metadata +19 -3
@@ -127,7 +127,7 @@ module Sequel
|
|
127
127
|
def serialize_attributes(format, *columns)
|
128
128
|
if format.is_a?(Symbol)
|
129
129
|
unless format = Sequel.synchronize{REGISTERED_FORMATS[format]}
|
130
|
-
raise(Error, "Unsupported serialization format: #{format} (valid formats: #{Sequel.synchronize{REGISTERED_FORMATS.keys}.
|
130
|
+
raise(Error, "Unsupported serialization format: #{format} (valid formats: #{Sequel.synchronize{REGISTERED_FORMATS.keys}.inspect})")
|
131
131
|
end
|
132
132
|
end
|
133
133
|
serializer, deserializer = format
|
@@ -154,7 +154,10 @@ module Sequel
|
|
154
154
|
deserialized_values[column] = deserialize_value(column, super())
|
155
155
|
end
|
156
156
|
end
|
157
|
-
|
157
|
+
alias_method(column, column)
|
158
|
+
|
159
|
+
setter = :"#{column}="
|
160
|
+
define_method(setter) do |v|
|
158
161
|
cc = changed_columns
|
159
162
|
if !cc.include?(column) && (new? || get_column_value(column) != v)
|
160
163
|
cc << column
|
@@ -164,6 +167,7 @@ module Sequel
|
|
164
167
|
|
165
168
|
deserialized_values[column] = v
|
166
169
|
end
|
170
|
+
alias_method(setter, setter)
|
167
171
|
end
|
168
172
|
end
|
169
173
|
end
|
@@ -177,8 +181,9 @@ module Sequel
|
|
177
181
|
|
178
182
|
# Freeze the deserialized values
|
179
183
|
def freeze
|
180
|
-
deserialized_values
|
184
|
+
deserialized_values
|
181
185
|
super
|
186
|
+
deserialized_values.freeze
|
182
187
|
end
|
183
188
|
|
184
189
|
# Serialize deserialized values before saving
|
@@ -0,0 +1,500 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
|
3
|
+
# :nocov:
|
4
|
+
|
5
|
+
# This entire file is excluded from coverage testing. This is because it
|
6
|
+
# requires coverage testing to work, and if you've already loaded Sequel
|
7
|
+
# without enabling coverage, then coverage testing won't work correctly
|
8
|
+
# for methods defined by Sequel.
|
9
|
+
#
|
10
|
+
# While automated coverage testing is disabled, manual coverage testing
|
11
|
+
# was used during spec development to make sure this code is 100% covered.
|
12
|
+
|
13
|
+
if RUBY_VERSION < '2.5'
|
14
|
+
raise LoadError, "The Sequel unused_associations plugin depends on Ruby 2.5+ method coverage"
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'coverage'
|
18
|
+
require 'json'
|
19
|
+
|
20
|
+
module Sequel
|
21
|
+
module Plugins
|
22
|
+
# The unused_associations plugin detects which model associations are not
|
23
|
+
# used and can be removed, and which model association methods are not used
|
24
|
+
# and can skip being defined. The advantage of removing unused associations
|
25
|
+
# and unused association methods is decreased memory usage, since each
|
26
|
+
# method defined takes memory and adds more work for the garbage collector.
|
27
|
+
#
|
28
|
+
# In order to detect which associations are used, this relies on the method
|
29
|
+
# coverage support added in Ruby 2.5. To allow flexibility to override
|
30
|
+
# association methods, the association methods that Sequel defines are
|
31
|
+
# defined in a module included in the class instead of directly in the
|
32
|
+
# class. Unfortunately, that makes it difficult to directly use the
|
33
|
+
# coverage data to find unused associations. The advantage of this plugin
|
34
|
+
# is that it is able to figure out from the coverage information whether
|
35
|
+
# the association methods Sequel defines are actually used.
|
36
|
+
#
|
37
|
+
# = Basic Usage
|
38
|
+
#
|
39
|
+
# The expected usage of the unused_associations plugin is to load it
|
40
|
+
# into the base class for models in your application, which will often
|
41
|
+
# be Sequel::Model:
|
42
|
+
#
|
43
|
+
# Sequel::Model.plugin :unused_associations
|
44
|
+
#
|
45
|
+
# Then you run your test suite with method coverage enabled, passing the
|
46
|
+
# coverage result to +update_associations_coverage+.
|
47
|
+
# +update_associations_coverage+ returns a data structure containing
|
48
|
+
# method coverage information for all subclasses of the base class.
|
49
|
+
# You can pass the coverage information to
|
50
|
+
# +update_unused_associations_data+, which will return a data structure
|
51
|
+
# with information on unused associations.
|
52
|
+
#
|
53
|
+
# require 'coverage'
|
54
|
+
# Coverage.start(methods: true)
|
55
|
+
# # load sequel after starting coverage, then run your tests
|
56
|
+
# cov_data = Sequel::Model.update_associations_coverage
|
57
|
+
# unused_associations_data = Sequel::Model.update_unused_associations_data(coverage_data: cov_data)
|
58
|
+
#
|
59
|
+
# You can take that unused association data and pass it to the
|
60
|
+
# +unused_associations+ method to get a array of information on
|
61
|
+
# associations which have not been used. Each entry in the array
|
62
|
+
# will contain a class name and association name for each unused
|
63
|
+
# association, both as a string:
|
64
|
+
#
|
65
|
+
# Sequel::Model.unused_associations(unused_associations_data: unused_associations_data)
|
66
|
+
# # => [["Class1", "assoc1"], ...]
|
67
|
+
#
|
68
|
+
# You can use the output of the +unused_associations+ method to determine
|
69
|
+
# which associations are not used at all in your application, and can
|
70
|
+
# be eliminiated.
|
71
|
+
#
|
72
|
+
# You can also take that unused association data and pass it to the
|
73
|
+
# +unused_association_options+ method, which will return an array of
|
74
|
+
# information on associations which are used, but have related methods
|
75
|
+
# defined that are not used. The first two entries in each array are
|
76
|
+
# the class name and association name as a string, and the third
|
77
|
+
# entry is a hash of association options:
|
78
|
+
#
|
79
|
+
# Sequel::Model.unused_association_options(unused_associations_data: unused_associations_data)
|
80
|
+
# # => [["Class2", "assoc2", {:read_only=>true}], ...]
|
81
|
+
#
|
82
|
+
# You can use the output of the +unused_association_options+ to
|
83
|
+
# find out which association options can be provided when defining
|
84
|
+
# the association so that the association method will not define
|
85
|
+
# methods that are not used.
|
86
|
+
#
|
87
|
+
# = Combining Coverage Results
|
88
|
+
#
|
89
|
+
# It is common to want to combine results from multiple separate
|
90
|
+
# coverage runs. For example, if you have multiple test suites
|
91
|
+
# for your application, one for model or unit tests and one for
|
92
|
+
# web or integration tests, you would want to combine the
|
93
|
+
# coverage information from all test suites before determining
|
94
|
+
# that the associations are not used.
|
95
|
+
#
|
96
|
+
# The unused_associations plugin supports combining multiple
|
97
|
+
# coverage results using the :coverage_file plugin option:
|
98
|
+
#
|
99
|
+
# Sequel::Model.plugin :unused_associations,
|
100
|
+
# coverage_file: 'unused_associations_coverage.json'
|
101
|
+
#
|
102
|
+
# With the coverage file option, +update_associations_coverage+
|
103
|
+
# will look in the given file for existing coverage information,
|
104
|
+
# if it exists. If the file exists, the data from it will be
|
105
|
+
# merged with the coverage result passed to the method.
|
106
|
+
# Before returning, the coverage file will be updated with the
|
107
|
+
# merged result. When using the :coverage_file plugin option,
|
108
|
+
# you can each of your test suites update the coverage
|
109
|
+
# information:
|
110
|
+
#
|
111
|
+
# require 'coverage'
|
112
|
+
# Coverage.start(methods: true)
|
113
|
+
# # run this test suite
|
114
|
+
# Sequel::Model.update_associations_coverage
|
115
|
+
#
|
116
|
+
# After all test suites have been run, you can run
|
117
|
+
# +update_unused_associations_data+, without an argument:
|
118
|
+
#
|
119
|
+
# unused_associations_data = Sequel::Model.update_unused_associations_data
|
120
|
+
#
|
121
|
+
# With no argument, +update_unused_associations_data+ will get
|
122
|
+
# the coverage data from the coverage file, and then use that
|
123
|
+
# to prepare the information. You can then use the returned
|
124
|
+
# value the same as before to get the data on unused associations.
|
125
|
+
# To prevent stale coverage information, calling
|
126
|
+
# +update_unused_associations_data+ when using the :coverage_file
|
127
|
+
# plugin option will remove the coverage file by default (you can
|
128
|
+
# use the :keep_coverage option to prevent the deletion of the
|
129
|
+
# coverage file).
|
130
|
+
#
|
131
|
+
# = Automatic Usage of Unused Association Data
|
132
|
+
#
|
133
|
+
# Since it can be a pain to manually update all of your code
|
134
|
+
# to remove unused assocations or add options to prevent the
|
135
|
+
# definition of unused associations, the unused_associations
|
136
|
+
# plugin comes with support to take previously saved unused
|
137
|
+
# association data, and use it to not create unused associations,
|
138
|
+
# and to automatically use the appropriate options so that unused
|
139
|
+
# association methods are not created.
|
140
|
+
#
|
141
|
+
# To use this option, you first need to save the unused association
|
142
|
+
# data previously prepared. You can do this by passing an
|
143
|
+
# :file option when loading the plugin.
|
144
|
+
#
|
145
|
+
# Sequel::Model.plugin :unused_associations,
|
146
|
+
# file: 'unused_associations.json'
|
147
|
+
#
|
148
|
+
# With the :file option provided, you no longer need to use
|
149
|
+
# the return value of +update_unused_associations_data+, as
|
150
|
+
# the file will be updated with the information:
|
151
|
+
#
|
152
|
+
# Sequel::Model.update_unused_associations_data(coverage_data: cov_data)
|
153
|
+
#
|
154
|
+
# Then, to use the saved unused associations data, add the
|
155
|
+
# :modify_associations plugin option:
|
156
|
+
#
|
157
|
+
# Sequel::Model.plugin :unused_associations,
|
158
|
+
# file: 'unused_associations.json',
|
159
|
+
# modify_associations: true
|
160
|
+
#
|
161
|
+
# With the :modify_associations used, and the unused association
|
162
|
+
# data file is available, when subclasses attempt to create an
|
163
|
+
# unused association, the attempt will be ignored. If the
|
164
|
+
# subclasses attempt to create an association where not
|
165
|
+
# all association methods are used, the plugin will automatically
|
166
|
+
# set the appropriate options so that the unused association
|
167
|
+
# methods are not defined.
|
168
|
+
#
|
169
|
+
# When you are testing which associations are used, make sure
|
170
|
+
# not to set the :modify_associations plugin option, or make sure
|
171
|
+
# that the unused associations data file does not exist.
|
172
|
+
#
|
173
|
+
# == Automatic Usage with Combined Coverage Results
|
174
|
+
#
|
175
|
+
# If you have multiple test suites and want to automatically
|
176
|
+
# use the unused association data, you should provide both
|
177
|
+
# :file and :coverage_file options when loading the plugin:
|
178
|
+
#
|
179
|
+
# Sequel::Model.plugin :unused_associations,
|
180
|
+
# file: 'unused_associations.json',
|
181
|
+
# coverage_file: 'unused_associations_coverage.json'
|
182
|
+
#
|
183
|
+
# Then each test suite just needs to run
|
184
|
+
# +update_associations_coverage+ to update the coverage information:
|
185
|
+
#
|
186
|
+
# Sequel::Model.update_associations_coverage
|
187
|
+
#
|
188
|
+
# After all test suites have been run, you can run
|
189
|
+
# +update_unused_associations_data+ to update the unused
|
190
|
+
# association data file (and remove the coverage file):
|
191
|
+
#
|
192
|
+
# Sequel::Model.update_unused_associations_data
|
193
|
+
#
|
194
|
+
# Then you can add the :modify_associations plugin option to
|
195
|
+
# automatically use the unused association data.
|
196
|
+
#
|
197
|
+
# = Caveats
|
198
|
+
#
|
199
|
+
# Since this plugin is based on coverage information, if you do
|
200
|
+
# not have tests that cover all usage of associations in your
|
201
|
+
# application, you can end up with coverage that shows the
|
202
|
+
# association is not used, when it is used in code that is not
|
203
|
+
# covered. The output of plugin can still be useful in such cases,
|
204
|
+
# as long as you are manually checking it. However, you should
|
205
|
+
# avoid using the :modify_associations unless you have
|
206
|
+
# confidence that your tests cover all usage of associations
|
207
|
+
# in your application. You can specify the :is_used association
|
208
|
+
# option for any association that you know is used. If an
|
209
|
+
# association uses the :is_used association option, this plugin
|
210
|
+
# will not modify it if the :modify_associations option is used.
|
211
|
+
#
|
212
|
+
# This plugin does not handle anonymous classes. Any unused
|
213
|
+
# associations defined in anonymous classes will not be
|
214
|
+
# reported by this plugin.
|
215
|
+
#
|
216
|
+
# This plugin only considers the public instance methods the
|
217
|
+
# association defines to determine if it was used. If an
|
218
|
+
# association is used in a way that does not call an instance
|
219
|
+
# method (such as only using the association with the
|
220
|
+
# dataset_associations plugin), then it would show up as unused
|
221
|
+
# by this plugin.
|
222
|
+
#
|
223
|
+
# As this relies on the method coverage added in Ruby 2.5, it does
|
224
|
+
# not work on older versions of Ruby. It also does not work on
|
225
|
+
# JRuby, as JRuby does not implement method coverage.
|
226
|
+
module UnusedAssociations
|
227
|
+
# Load the subclasses plugin, as the unused associations plugin
|
228
|
+
# is designed to handle all subclasses of the class it is loaded
|
229
|
+
# into.
|
230
|
+
def self.apply(mod, opts=OPTS)
|
231
|
+
mod.plugin :subclasses
|
232
|
+
end
|
233
|
+
|
234
|
+
# Plugin options:
|
235
|
+
# :coverage_file :: The file to store the coverage information,
|
236
|
+
# when combining coverage information from
|
237
|
+
# multiple test suites.
|
238
|
+
# :file :: The file to store and/or load the unused associations data.
|
239
|
+
# :modify_associations :: Whether to use the unused associations data
|
240
|
+
# to skip defining associations or association
|
241
|
+
# methods.
|
242
|
+
# :unused_associations_data :: The unused associations data to use if the
|
243
|
+
# :modify_associations is used (by default, the
|
244
|
+
# :modify_associations option will use the data from
|
245
|
+
# the file specified by the :file option). This is
|
246
|
+
# same data returned by the
|
247
|
+
# +update_unused_associations_data+ method.
|
248
|
+
def self.configure(mod, opts=OPTS)
|
249
|
+
mod.instance_exec do
|
250
|
+
@unused_associations_coverage_file = opts[:coverage_file]
|
251
|
+
@unused_associations_file = opts[:file]
|
252
|
+
@unused_associations_data = if opts[:modify_associations]
|
253
|
+
if opts[:unused_associations_data]
|
254
|
+
opts[:unused_associations_data]
|
255
|
+
elsif File.file?(opts[:file])
|
256
|
+
Sequel.parse_json(File.binread(opts[:file]))
|
257
|
+
end
|
258
|
+
end
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
module ClassMethods
|
263
|
+
# Only the data is copied to subclasses, to allow the :modify_associations
|
264
|
+
# plugin option to affect them. The :file and :coverage_file are not copied
|
265
|
+
# to subclasses, as users are expected ot call methods such as
|
266
|
+
# unused_associations only on the class that is loading the plugin.
|
267
|
+
Plugins.inherited_instance_variables(self, :@unused_associations_data=>nil)
|
268
|
+
|
269
|
+
# If modifying associations, and this association is marked as not used,
|
270
|
+
# and the association does not include the specific :is_used option,
|
271
|
+
# skip defining the association.
|
272
|
+
def associate(type, assoc_name, opts=OPTS)
|
273
|
+
if !opts[:is_used] && @unused_associations_data && (data = @unused_associations_data[name]) && data[assoc_name.to_s] == 'unused'
|
274
|
+
return
|
275
|
+
end
|
276
|
+
|
277
|
+
super
|
278
|
+
end
|
279
|
+
|
280
|
+
# Parse the coverage result, and return the coverage data for the
|
281
|
+
# associations for descendants of this class. If the plugin
|
282
|
+
# uses the :coverage_file option, the existing coverage file will be loaded
|
283
|
+
# if present, and before the method returns, the coverage file will be updated.
|
284
|
+
#
|
285
|
+
# Options:
|
286
|
+
# :coverage_result :: The coverage result to use. This defaults to +Coverage.result+.
|
287
|
+
def update_associations_coverage(opts=OPTS)
|
288
|
+
coverage_result = opts[:coverage_result] || Coverage.result
|
289
|
+
module_mapping = {}
|
290
|
+
file = @unused_associations_coverage_file
|
291
|
+
|
292
|
+
coverage_data = if file && File.file?(file)
|
293
|
+
Sequel.parse_json(File.binread(file))
|
294
|
+
else
|
295
|
+
{}
|
296
|
+
end
|
297
|
+
|
298
|
+
([self] + descendents).each do |sc|
|
299
|
+
next if sc.associations.empty? || !sc.name
|
300
|
+
module_mapping[sc.send(:overridable_methods_module)] = sc
|
301
|
+
coverage_data[sc.name] ||= {}
|
302
|
+
end
|
303
|
+
|
304
|
+
coverage_result.each do |file, coverage|
|
305
|
+
coverage[:methods].each do |(mod, meth), times|
|
306
|
+
next unless sc = module_mapping[mod]
|
307
|
+
coverage_data[sc.name][meth.to_s] ||= 0
|
308
|
+
coverage_data[sc.name][meth.to_s] += times
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
if file
|
313
|
+
File.binwrite(file, Sequel.object_to_json(coverage_data))
|
314
|
+
end
|
315
|
+
|
316
|
+
coverage_data
|
317
|
+
end
|
318
|
+
|
319
|
+
# Parse the coverage data returned by #update_associations_coverage,
|
320
|
+
# and return data on unused associations and unused association methods.
|
321
|
+
#
|
322
|
+
# Options:
|
323
|
+
# :coverage_data :: The coverage data to use. If not given, it is taken
|
324
|
+
# from the file specified by the :coverage_file plugin option.
|
325
|
+
# :keep_coverage :: Do not delete the file specified by the :coverage_file plugin
|
326
|
+
# option, even if it exists.
|
327
|
+
def update_unused_associations_data(options=OPTS)
|
328
|
+
coverage_data = options[:coverage_data] || Sequel.parse_json(File.binread(@unused_associations_coverage_file))
|
329
|
+
|
330
|
+
unused_associations_data = {}
|
331
|
+
|
332
|
+
([self] + descendents).each do |sc|
|
333
|
+
next unless cov_data = coverage_data[sc.name]
|
334
|
+
|
335
|
+
sc.associations.each do |assoc|
|
336
|
+
ref = sc.association_reflection(assoc)
|
337
|
+
|
338
|
+
# Only report associations for the class they are defined in
|
339
|
+
next unless ref[:model] == sc
|
340
|
+
|
341
|
+
# Do not report associations using methods_module option, because this plugin only
|
342
|
+
# looks in the class's overridable_methods_module
|
343
|
+
next if ref[:methods_module]
|
344
|
+
|
345
|
+
info = {}
|
346
|
+
|
347
|
+
_update_association_coverage_info(info, cov_data, ref.dataset_method, :dataset_method)
|
348
|
+
_update_association_coverage_info(info, cov_data, ref.association_method, :association_method)
|
349
|
+
|
350
|
+
unless ref[:orig_opts][:read_only]
|
351
|
+
if ref.returns_array?
|
352
|
+
_update_association_coverage_info(info, cov_data, ref[:add_method], :adder)
|
353
|
+
_update_association_coverage_info(info, cov_data, ref[:remove_method], :remover)
|
354
|
+
_update_association_coverage_info(info, cov_data, ref[:remove_all_method], :clearer)
|
355
|
+
else
|
356
|
+
_update_association_coverage_info(info, cov_data, ref[:setter_method], :setter)
|
357
|
+
end
|
358
|
+
end
|
359
|
+
|
360
|
+
next if info.keys == [:missing]
|
361
|
+
|
362
|
+
if !info[:used]
|
363
|
+
(unused_associations_data[sc.name] ||= {})[assoc.to_s] = 'unused'
|
364
|
+
elsif unused = info[:unused]
|
365
|
+
if unused.include?(:setter) || [:adder, :remover, :clearer].all?{|k| unused.include?(k)}
|
366
|
+
[:setter, :adder, :remover, :clearer].each do |k|
|
367
|
+
unused.delete(k)
|
368
|
+
end
|
369
|
+
unused << :read_only
|
370
|
+
end
|
371
|
+
(unused_associations_data[sc.name] ||= {})[assoc.to_s] = unused.map(&:to_s)
|
372
|
+
end
|
373
|
+
end
|
374
|
+
end
|
375
|
+
|
376
|
+
if @unused_associations_file
|
377
|
+
File.binwrite(@unused_associations_file, Sequel.object_to_json(unused_associations_data))
|
378
|
+
end
|
379
|
+
unless options[:keep_coverage]
|
380
|
+
_delete_unused_associations_file(@unused_associations_coverage_file)
|
381
|
+
end
|
382
|
+
|
383
|
+
unused_associations_data
|
384
|
+
end
|
385
|
+
|
386
|
+
# Return an array of unused associations. These are associations where none of the
|
387
|
+
# association methods are used, according to the coverage information. Each entry
|
388
|
+
# in the array is an array of two strings, with the first string being the class name
|
389
|
+
# and the second string being the association name.
|
390
|
+
#
|
391
|
+
# Options:
|
392
|
+
# :unused_associations_data :: The data to use for determining which associations
|
393
|
+
# are unused, which is returned from
|
394
|
+
# +update_unused_associations_data+. If not given,
|
395
|
+
# loads the data from the file specified by the :file
|
396
|
+
# plugin option.
|
397
|
+
def unused_associations(opts=OPTS)
|
398
|
+
unused_associations_data = opts[:unused_associations_data] || Sequel.parse_json(File.binread(@unused_associations_file))
|
399
|
+
|
400
|
+
unused_associations = []
|
401
|
+
unused_associations_data.each do |sc, associations|
|
402
|
+
associations.each do |assoc, unused|
|
403
|
+
if unused == 'unused'
|
404
|
+
unused_associations << [sc, assoc]
|
405
|
+
end
|
406
|
+
end
|
407
|
+
end
|
408
|
+
unused_associations
|
409
|
+
end
|
410
|
+
|
411
|
+
# Return an array of unused association options. These are associations some but not all
|
412
|
+
# of the association methods are used, according to the coverage information. Each entry
|
413
|
+
# in the array is an array of three elements. The first element is the class name string,
|
414
|
+
# the second element is the association name string, and the third element is a hash of
|
415
|
+
# association options that can be used in the association so it does not define methods
|
416
|
+
# that are not used.
|
417
|
+
#
|
418
|
+
# Options:
|
419
|
+
# :unused_associations_data :: The data to use for determining which associations
|
420
|
+
# are unused, which is returned from
|
421
|
+
# +update_unused_associations_data+. If not given,
|
422
|
+
# loads the data from the file specified by the :file
|
423
|
+
# plugin option.
|
424
|
+
def unused_association_options(opts=OPTS)
|
425
|
+
unused_associations_data = opts[:unused_associations_data] || Sequel.parse_json(File.binread(@unused_associations_file))
|
426
|
+
|
427
|
+
unused_association_methods = []
|
428
|
+
unused_associations_data.each do |sc, associations|
|
429
|
+
associations.each do |assoc, unused|
|
430
|
+
unless unused == 'unused'
|
431
|
+
unused_association_methods << [sc, assoc, set_unused_options_for_association({}, unused)]
|
432
|
+
end
|
433
|
+
end
|
434
|
+
end
|
435
|
+
unused_association_methods
|
436
|
+
end
|
437
|
+
|
438
|
+
# Delete the unused associations coverage file and unused associations data file,
|
439
|
+
# if either exist.
|
440
|
+
def delete_unused_associations_files
|
441
|
+
_delete_unused_associations_file(@unused_associations_coverage_file)
|
442
|
+
_delete_unused_associations_file(@unused_associations_file)
|
443
|
+
end
|
444
|
+
|
445
|
+
private
|
446
|
+
|
447
|
+
# Delete the given file if it exists.
|
448
|
+
def _delete_unused_associations_file(file)
|
449
|
+
if file && File.file?(file)
|
450
|
+
File.unlink(file)
|
451
|
+
end
|
452
|
+
end
|
453
|
+
|
454
|
+
# Update the info hash with information on whether the given method was
|
455
|
+
# called, according to the coverage information.
|
456
|
+
def _update_association_coverage_info(info, coverage_data, meth, key)
|
457
|
+
type = case coverage_data[meth.to_s]
|
458
|
+
when 0
|
459
|
+
:unused
|
460
|
+
when Integer
|
461
|
+
:used
|
462
|
+
else
|
463
|
+
# Missing here means there is no coverage information for the
|
464
|
+
# the method, which indicates the expected method was never
|
465
|
+
# defined. In that case, it can be ignored.
|
466
|
+
:missing
|
467
|
+
end
|
468
|
+
|
469
|
+
(info[type] ||= []) << key
|
470
|
+
end
|
471
|
+
|
472
|
+
# Based on the value of the unused, update the opts hash with association
|
473
|
+
# options that will prevent unused association methods from being
|
474
|
+
# defined.
|
475
|
+
def set_unused_options_for_association(opts, unused)
|
476
|
+
opts[:read_only] = true if unused.include?('read_only')
|
477
|
+
opts[:no_dataset_method] = true if unused.include?('dataset_method')
|
478
|
+
opts[:no_association_method] = true if unused.include?('association_method')
|
479
|
+
opts[:adder] = nil if unused.include?('adder')
|
480
|
+
opts[:remover] = nil if unused.include?('remover')
|
481
|
+
opts[:clearer] = nil if unused.include?('clearer')
|
482
|
+
opts
|
483
|
+
end
|
484
|
+
|
485
|
+
# If modifying associations, and this association has unused association
|
486
|
+
# methods, automatically set the appropriate options so the unused association
|
487
|
+
# methods are not defined, unless the association explicitly uses the :is_used
|
488
|
+
# options.
|
489
|
+
def def_association(opts)
|
490
|
+
if !opts[:is_used] && @unused_associations_data && (data = @unused_associations_data[name]) && (unused = data[opts[:name].to_s])
|
491
|
+
set_unused_options_for_association(opts, unused)
|
492
|
+
end
|
493
|
+
|
494
|
+
super
|
495
|
+
end
|
496
|
+
end
|
497
|
+
end
|
498
|
+
end
|
499
|
+
end
|
500
|
+
# :nocov:
|