Class: WhyClasses::Rules::FunctionBucket
- Inherits:
-
WhyClasses::Rule
- Object
- WhyClasses::Rule
- WhyClasses::Rules::FunctionBucket
- Defined in:
- lib/why_classes/rules/function_bucket.rb
Overview
Rule 2 (summary): "If it's named after a design pattern, it's probably a
workaround for a less flexible language." The classic service object -- a
class with a single call/perform/run instance method and no state --
is a bucket for a function.
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/function_bucket.rb', line 12 def self.description "Class with a single call/perform/run method and no state -> use a module function." 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 |
# File 'lib/why_classes/rules/function_bucket.rb', line 16 def on_class(node) s = shape(node) return if s.name.nil? return if configuration.framework_base_class?(s.superclass) return if s.superclass? # framework/base subclasses handled above; other inheritance is separate return unless function_bucket?(s) method = s.behavior_methods.first.children[0] add_offense( node, message: "#{s.name} is a bucket for a single function (`##{method}`) with no state.", suggestion: "Prefer a module function or a plain method:\n" \ " module #{s.name}\n module_function\n\n" \ " def #{method}(...)\n ...\n end\n end\n" \ " Callers become `#{s.name}.#{method}(...)` with no throwaway instance." ) end |