Class: WhyClasses::Rules::DataBucket

Inherits:
WhyClasses::Rule show all
Defined in:
lib/why_classes/rules/data_bucket.rb

Overview

Rule 4 (summary): "If it's just a data bucket, use a Struct, Data object, or a Hash." A class that only declares attr_* fields and an initialize that assigns them, with no behaviour, is a data bucket.

Constant Summary

Constants inherited from WhyClasses::Rule

WhyClasses::Rule::H

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

.descriptionObject



12
13
14
# File 'lib/why_classes/rules/data_bucket.rb', line 12

def self.description
  "Class that only holds data (attr_* + field-assigning initialize) -> use Struct or Data."
end

.tierObject



16
17
18
# File 'lib/why_classes/rules/data_bucket.rb', line 16

def self.tier
  :suggested
end

Instance Method Details

#on_class(node) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/why_classes/rules/data_bucket.rb', line 20

def on_class(node)
  s = shape(node)
  return if s.name.nil?
  return if configuration.framework_base_class?(s.superclass)
  return unless data_bucket?(s)

  analysis = Correctors::DataBucketCorrector.analyze(s, prefer_data: rule_option("PreferData", true))
  corrector = build_corrector(node, s, analysis)

  add_offense(
    node,
    message: "#{s.name} is a data bucket: it only stores fields and has no behaviour.",
    suggestion: suggestion_for(s, analysis),
    corrector: corrector
  )
end