Class: WhyClasses::Rules::StatelessSingletonMethods
- Inherits:
-
WhyClasses::Rule
- Object
- WhyClasses::Rule
- WhyClasses::Rules::StatelessSingletonMethods
- Defined in:
- lib/why_classes/rules/stateless_singleton_methods.rb
Overview
Rule 3 (summary): "If it has no state, it should not be a class." A class
whose body is only self. methods is a namespace for functions, not an
object factory -- it should be a module.
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/stateless_singleton_methods.rb', line 12 def self.description "Class with only class methods and no state -> use a module with `extend self`." end |
.tier ⇒ Object
16 17 18 |
# File 'lib/why_classes/rules/stateless_singleton_methods.rb', line 16 def self.tier :unsafe end |
Instance Method Details
#on_class(node) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/why_classes/rules/stateless_singleton_methods.rb', line 20 def on_class(node) s = shape(node) return if s.name.nil? return if s.superclass? # inheritance is a separate concern (see InheritanceForMixins) return if configuration.framework_base_class?(s.superclass) return unless only_singleton_methods?(s) corrector = if Correctors::StatelessModuleCorrector.correctable?(s) Correctors::StatelessModuleCorrector.corrector(node) end add_offense( node, message: "#{s.name} has only class methods and no state; it's a bucket of functions, " \ "not an object factory.", suggestion: "Make it a module and expose the methods with `extend self`:\n" \ " module #{s.name}\n def #{example_method(s)}(...)\n ...\n end\n\n" \ " private\n # helper methods here are now genuinely private\n\n" \ " extend self\n end", corrector: corrector ) end |