Module: Sequel::Plugins::UnusedAssociations::ClassMethods
- Defined in:
- lib/sequel/plugins/unused_associations.rb
Instance Method Summary collapse
-
#associate(type, assoc_name, opts = OPTS) ⇒ Object
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.
-
#association_reflection(association) ⇒ Object
Record access to association reflections to determine which associations are not used.
-
#delete_unused_associations_files ⇒ Object
Delete the unused associations coverage file and unused associations data file, if either exist.
-
#freeze ⇒ Object
Setup the used_association_reflections storage before freezing.
-
#unused_association_options(opts = OPTS) ⇒ Object
Return an array of unused association options.
-
#unused_associations(opts = OPTS) ⇒ Object
Return an array of unused associations.
-
#update_associations_coverage(opts = OPTS) ⇒ Object
Parse the coverage result, and return the coverage data for the associations for descendants of this class.
-
#update_unused_associations_data(options = OPTS) ⇒ Object
Parse the coverage data returned by #update_associations_coverage, and return data on unused associations and unused association methods.
-
#used_association_reflections ⇒ Object
Synchronize access to the used association reflections.
Instance Method Details
#associate(type, assoc_name, opts = OPTS) ⇒ Object
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.
287 288 289 290 291 292 293 |
# File 'lib/sequel/plugins/unused_associations.rb', line 287 def associate(type, assoc_name, opts=OPTS) if !opts[:is_used] && @unused_associations_data && (data = @unused_associations_data[name]) && data[assoc_name.to_s] == 'unused' return end super end |
#association_reflection(association) ⇒ Object
Record access to association reflections to determine which associations are not used.
278 279 280 281 282 |
# File 'lib/sequel/plugins/unused_associations.rb', line 278 def association_reflection(association) uar = used_association_reflections Sequel.synchronize{uar[association] ||= true} super end |
#delete_unused_associations_files ⇒ Object
Delete the unused associations coverage file and unused associations data file, if either exist.
469 470 471 472 |
# File 'lib/sequel/plugins/unused_associations.rb', line 469 def delete_unused_associations_files _delete_unused_associations_file(@unused_associations_coverage_file) _delete_unused_associations_file(@unused_associations_file) end |
#freeze ⇒ Object
Setup the used_association_reflections storage before freezing
296 297 298 299 |
# File 'lib/sequel/plugins/unused_associations.rb', line 296 def freeze used_association_reflections super end |
#unused_association_options(opts = OPTS) ⇒ Object
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.
453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/sequel/plugins/unused_associations.rb', line 453 def (opts=OPTS) unused_associations_data = opts[:unused_associations_data] || Sequel.parse_json(File.binread(@unused_associations_file)) unused_association_methods = [] unused_associations_data.each do |sc, associations| associations.each do |assoc, unused| unless unused == 'unused' unused_association_methods << [sc, assoc, ({}, unused)] end end end unused_association_methods end |
#unused_associations(opts = OPTS) ⇒ Object
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.
426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/sequel/plugins/unused_associations.rb', line 426 def unused_associations(opts=OPTS) unused_associations_data = opts[:unused_associations_data] || Sequel.parse_json(File.binread(@unused_associations_file)) unused_associations = [] unused_associations_data.each do |sc, associations| associations.each do |assoc, unused| if unused == 'unused' unused_associations << [sc, assoc] end end end unused_associations end |
#update_associations_coverage(opts = OPTS) ⇒ Object
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.
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/sequel/plugins/unused_associations.rb', line 308 def update_associations_coverage(opts=OPTS) coverage_result = opts[:coverage_result] || Coverage.result module_mapping = {} file = @unused_associations_coverage_file coverage_data = if file && File.file?(file) Sequel.parse_json(File.binread(file)) else {} end ([self] + descendants).each do |sc| next if sc.associations.empty? || !sc.name module_mapping[sc.send(:overridable_methods_module)] = sc cov_data = coverage_data[sc.name] ||= {''=>[]} cov_data[''].concat(sc.used_association_reflections.keys.map(&:to_s).sort).uniq! end coverage_result.each do |file, coverage| coverage[:methods].each do |(mod, meth), times| next unless sc = module_mapping[mod] coverage_data[sc.name][meth.to_s] ||= 0 coverage_data[sc.name][meth.to_s] += times end end if file File.binwrite(file, Sequel.object_to_json(coverage_data)) end coverage_data end |
#update_unused_associations_data(options = OPTS) ⇒ Object
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.
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/sequel/plugins/unused_associations.rb', line 349 def update_unused_associations_data(=OPTS) coverage_data = [:coverage_data] || Sequel.parse_json(File.binread(@unused_associations_coverage_file)) unused_associations_data = {} to_many_modification_methods = [:adder, :remover, :clearer] modification_methods = [:setter, :adder, :remover, :clearer] ([self] + descendants).each do |sc| next unless cov_data = coverage_data[sc.name] reflection_data = cov_data[''] || [] sc.association_reflections.each do |assoc, ref| # Only report associations for the class they are defined in next unless ref[:model] == sc # Do not report associations using methods_module option, because this plugin only # looks in the class's overridable_methods_module next if ref[:methods_module] # Do not report associations if they are explicitly marked as used. next if ref[:is_used] info = {} if reflection_data.include?(assoc.to_s) info[:used] = [:reflection] end _update_association_coverage_info(info, cov_data, ref.dataset_method, :dataset_method) _update_association_coverage_info(info, cov_data, ref.association_method, :association_method) unless ref[:orig_opts][:read_only] if ref.returns_array? _update_association_coverage_info(info, cov_data, ref[:add_method], :adder) _update_association_coverage_info(info, cov_data, ref[:remove_method], :remover) _update_association_coverage_info(info, cov_data, ref[:remove_all_method], :clearer) else _update_association_coverage_info(info, cov_data, ref[:setter_method], :setter) end end next if info.keys == [:missing] if !info[:used] (unused_associations_data[sc.name] ||= {})[assoc.to_s] = 'unused' elsif unused = info[:unused] if unused.include?(:setter) || to_many_modification_methods.all?{|k| unused.include?(k)} modification_methods.each do |k| unused.delete(k) end unused << :read_only end (unused_associations_data[sc.name] ||= {})[assoc.to_s] = unused.map(&:to_s) end end end if @unused_associations_file File.binwrite(@unused_associations_file, Sequel.object_to_json(unused_associations_data)) end unless [:keep_coverage] _delete_unused_associations_file(@unused_associations_coverage_file) end unused_associations_data end |
#used_association_reflections ⇒ Object
Synchronize access to the used association reflections.
273 274 275 |
# File 'lib/sequel/plugins/unused_associations.rb', line 273 def used_association_reflections Sequel.synchronize{@used_association_reflections ||= {}} end |