Class: WhyClasses::Rules::InheritanceForMixins
- Inherits:
-
WhyClasses::Rule
- Object
- WhyClasses::Rule
- WhyClasses::Rules::InheritanceForMixins
- Defined in:
- lib/why_classes/rules/inheritance_for_mixins.rb
Overview
Thomas criticises inheriting from a fat base class purely to inject
behaviour (e.g. < ActiveRecord::Base pulls in hundreds of methods) rather
than to specialise. Advice-only: safe rewriting needs whole-program
knowledge of which inherited methods are actually used.
Constant Summary
Constants inherited from WhyClasses::Rule
Class Method Summary collapse
Instance Method Summary collapse
Methods inherited from WhyClasses::Rule
autocorrectable?, #call, inherited, #initialize, rule_name
Constructor Details
This class inherits a constructor from WhyClasses::Rule
Class Method Details
.description ⇒ Object
12 13 14 |
# File 'lib/why_classes/rules/inheritance_for_mixins.rb', line 12 def self.description "Inheriting a fat base class to inject methods -> include only the mixins you use." end |
Instance Method Details
#on_class(node) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/why_classes/rules/inheritance_for_mixins.rb', line 16 def on_class(node) return unless rails? s = shape(node) return if s.name.nil? return unless s.superclass? mixins = configuration.mixin_suggestions_for(s.superclass) return if mixins.nil? add_offense( node, message: "#{s.name} < #{s.superclass} inherits a large surface just to gain behaviour.", suggestion: "If you only need part of it, include the specific mixins instead:\n" \ " class #{s.name}\n" + mixins.map { |m| " include #{m}" }.join("\n") + "\n end\n Inherit only when you are genuinely specialising #{s.superclass}." ) end |