module Sequel::Plugins::UnusedAssociations::ClassMethods

  1. lib/sequel/plugins/unused_associations.rb

Public Instance methods

associate(type, assoc_name, opts=OPTS)

If modifying associations, and this association is marked as not used, and the association does not include the specific :is_used option, skip defining the association.

[show source]
    # File lib/sequel/plugins/unused_associations.rb
287 def associate(type, assoc_name, opts=OPTS)
288   if !opts[:is_used] && @unused_associations_data && (data = @unused_associations_data[name]) && data[assoc_name.to_s] == 'unused'
289     return
290   end
291   
292   super
293 end
association_reflection(association)

Record access to association reflections to determine which associations are not used.

[show source]
    # File lib/sequel/plugins/unused_associations.rb
278 def association_reflection(association)
279   uar = used_association_reflections
280   Sequel.synchronize{uar[association] ||= true}
281   super
282 end
delete_unused_associations_files()

Delete the unused associations coverage file and unused associations data file, if either exist.

[show source]
    # File lib/sequel/plugins/unused_associations.rb
469 def delete_unused_associations_files
470   _delete_unused_associations_file(@unused_associations_coverage_file)
471   _delete_unused_associations_file(@unused_associations_file)
472 end
freeze()

Setup the used_association_reflections storage before freezing

[show source]
    # File lib/sequel/plugins/unused_associations.rb
296 def freeze
297   used_association_reflections
298   super
299 end
unused_association_options(opts=OPTS)

Return an array of unused association options. These are associations some but not all of the association methods are used, according to the coverage information. Each entry in the array is an array of three elements. The first element is the class name string, the second element is the association name string, and the third element is a hash of association options that can be used in the association so it does not define methods that are not used.

Options:

:unused_associations_data

The data to use for determining which associations are unused, which is returned from update_unused_associations_data. If not given, loads the data from the file specified by the :file plugin option.

[show source]
    # File lib/sequel/plugins/unused_associations.rb
453 def unused_association_options(opts=OPTS)
454   unused_associations_data = opts[:unused_associations_data] || Sequel.parse_json(File.binread(@unused_associations_file))
455 
456   unused_association_methods = []
457   unused_associations_data.each do |sc, associations|
458     associations.each do |assoc, unused|
459       unless unused == 'unused'
460         unused_association_methods << [sc, assoc, set_unused_options_for_association({}, unused)]
461       end
462     end
463   end
464   unused_association_methods
465 end
unused_associations(opts=OPTS)

Return an array of unused associations. These are associations where none of the association methods are used, according to the coverage information. Each entry in the array is an array of two strings, with the first string being the class name and the second string being the association name.

Options:

:unused_associations_data

The data to use for determining which associations are unused, which is returned from update_unused_associations_data. If not given, loads the data from the file specified by the :file plugin option.

[show source]
    # File lib/sequel/plugins/unused_associations.rb
426 def unused_associations(opts=OPTS)
427   unused_associations_data = opts[:unused_associations_data] || Sequel.parse_json(File.binread(@unused_associations_file))
428 
429   unused_associations = []
430   unused_associations_data.each do |sc, associations|
431     associations.each do |assoc, unused|
432       if unused == 'unused'
433         unused_associations << [sc, assoc]
434       end
435     end
436   end
437   unused_associations
438 end
update_associations_coverage(opts=OPTS)

Parse the coverage result, and return the coverage data for the associations for descendants of this class. If the plugin uses the :coverage_file option, the existing coverage file will be loaded if present, and before the method returns, the coverage file will be updated.

Options:

:coverage_result

The coverage result to use. This defaults to Coverage.result.

[show source]
    # File lib/sequel/plugins/unused_associations.rb
308 def update_associations_coverage(opts=OPTS)
309   coverage_result = opts[:coverage_result] || Coverage.result
310   module_mapping = {}
311   file = @unused_associations_coverage_file
312 
313   coverage_data = if file && File.file?(file)
314     Sequel.parse_json(File.binread(file))
315   else
316     {}
317   end
318 
319   ([self] + descendants).each do |sc|
320     next if sc.associations.empty? || !sc.name
321     module_mapping[sc.send(:overridable_methods_module)] = sc
322     cov_data = coverage_data[sc.name] ||= {''=>[]}
323     cov_data[''].concat(sc.used_association_reflections.keys.map(&:to_s).sort).uniq!
324   end
325 
326   coverage_result.each do |file, coverage|
327     coverage[:methods].each do |(mod, meth), times|
328       next unless sc = module_mapping[mod]
329       coverage_data[sc.name][meth.to_s] ||= 0
330       coverage_data[sc.name][meth.to_s] += times
331     end
332   end
333 
334   if file
335     File.binwrite(file, Sequel.object_to_json(coverage_data))
336   end
337 
338   coverage_data
339 end
update_unused_associations_data(options=OPTS)

Parse the coverage data returned by update_associations_coverage, and return data on unused associations and unused association methods.

Options:

:coverage_data

The coverage data to use. If not given, it is taken from the file specified by the :coverage_file plugin option.

:keep_coverage

Do not delete the file specified by the :coverage_file plugin option, even if it exists.

[show source]
    # File lib/sequel/plugins/unused_associations.rb
349 def update_unused_associations_data(options=OPTS)
350   coverage_data = options[:coverage_data] || Sequel.parse_json(File.binread(@unused_associations_coverage_file))
351 
352   unused_associations_data = {}
353   to_many_modification_methods = [:adder, :remover, :clearer]
354   modification_methods = [:setter, :adder, :remover, :clearer]
355 
356   ([self] + descendants).each do |sc|
357     next unless cov_data = coverage_data[sc.name]
358     reflection_data = cov_data[''] || []
359 
360     sc.association_reflections.each do |assoc, ref|
361       # Only report associations for the class they are defined in
362       next unless ref[:model] == sc
363 
364       # Do not report associations using methods_module option, because this plugin only
365       # looks in the class's overridable_methods_module
366       next if ref[:methods_module]
367 
368       # Do not report associations if they are explicitly marked as used.
369       next if ref[:is_used]
370 
371       info = {}
372       if reflection_data.include?(assoc.to_s)
373         info[:used] = [:reflection]
374       end
375 
376       _update_association_coverage_info(info, cov_data, ref.dataset_method, :dataset_method)
377       _update_association_coverage_info(info, cov_data, ref.association_method, :association_method)
378 
379       unless ref[:orig_opts][:read_only]
380         if ref.returns_array?
381           _update_association_coverage_info(info, cov_data, ref[:add_method], :adder)
382           _update_association_coverage_info(info, cov_data, ref[:remove_method], :remover)
383           _update_association_coverage_info(info, cov_data, ref[:remove_all_method], :clearer)
384         else
385           _update_association_coverage_info(info, cov_data, ref[:setter_method], :setter)
386         end
387       end
388 
389       next if info.keys == [:missing]
390 
391       if !info[:used]
392         (unused_associations_data[sc.name] ||= {})[assoc.to_s] = 'unused'
393       elsif unused = info[:unused]
394         if unused.include?(:setter) || to_many_modification_methods.all?{|k| unused.include?(k)}
395           modification_methods.each do |k|
396             unused.delete(k)
397           end
398           unused << :read_only
399         end
400         (unused_associations_data[sc.name] ||= {})[assoc.to_s] = unused.map(&:to_s)
401       end
402     end
403   end
404 
405   if @unused_associations_file
406     File.binwrite(@unused_associations_file, Sequel.object_to_json(unused_associations_data))
407   end
408   unless options[:keep_coverage]
409     _delete_unused_associations_file(@unused_associations_coverage_file)
410   end
411 
412   unused_associations_data
413 end
used_association_reflections()

Synchronize access to the used association reflections.

[show source]
    # File lib/sequel/plugins/unused_associations.rb
273 def used_association_reflections
274   Sequel.synchronize{@used_association_reflections ||= {}}
275 end