Class: Guardrails::Lookbook::ComponentReport

Inherits:
Object
  • Object
show all
Defined in:
lib/guardrails/lookbook/component_report.rb

Overview

Generates a per-component findings report meant to be rendered inside a Lookbook panel. Returns a Hash so the consumer can choose how to render it (HTML partial, JSON, etc).

Instance Method Summary collapse

Constructor Details

#initialize(root:) ⇒ ComponentReport

Returns a new instance of ComponentReport.



12
13
14
# File 'lib/guardrails/lookbook/component_report.rb', line 12

def initialize(root:)
  @root = Pathname(root)
end

Instance Method Details

#for(component_class_name) ⇒ Hash?

Returns nil when the component class can’t be located.

Parameters:

  • component_class_name (String)

    e.g. “ButtonComponent” or “Admin::Users::ProfileComponent”

Returns:

  • (Hash, nil)

    nil when the component class can’t be located



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/guardrails/lookbook/component_report.rb', line 19

def for(component_class_name)
  class_path = component_class_path(component_class_name)
  return nil unless class_path&.exist?

  template_path = class_path.sub_ext(".html.erb")
  class_relative = relative(class_path)
  template_relative = template_path.exist? ? relative(template_path) : nil

  {
    component: component_class_name,
    class_file: class_relative,
    template_file: template_relative,
    violations: violations_for(template_relative),
    orphan_slots: orphan_slots_for(class_relative),
    similar_templates: similar_templates_for(template_relative)
  }
end