Class: RailsLens::Analyzers::Enums

Inherits:
Base
  • Object
show all
Defined in:
lib/rails_lens/analyzers/enums.rb

Instance Attribute Summary

Attributes inherited from Base

#model_class

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from ErrorHandling

#safe_analyze

Constructor Details

This class inherits a constructor from RailsLens::Analyzers::Base

Instance Method Details

#analyzeObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rails_lens/analyzers/enums.rb', line 6

def analyze
  return nil unless model_class.respond_to?(:defined_enums) && model_class.defined_enums.any?

  lines = []
  lines << '[enums]'

  model_class.defined_enums.each do |name, values|
    # Format as TOML inline table: name = { key = "value", ... }
    formatted_values = if values.values.all?(Integer)
                         # Integer-based enum
                         values.map { |k, v| "#{k} = #{v}" }.join(', ')
                       else
                         # String-based enum
                         values.map { |k, v| "#{k} = \"#{v}\"" }.join(', ')
                       end
    lines << "#{name} = { #{formatted_values} }"
  end

  lines.join("\n")
end