Class: Rufo::ErbFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rufo/erb_formatter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, **options) ⇒ ErbFormatter

Returns a new instance of ErbFormatter.



26
27
28
29
30
31
32
# File 'lib/rufo/erb_formatter.rb', line 26

def initialize(code, **options)
  @options = options
  @scanner = CustomScanner.new(code)
  @code_mode = false
  @current_lineno = 0
  @current_column = 0
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



24
25
26
# File 'lib/rufo/erb_formatter.rb', line 24

def result
  @result
end

Class Method Details

.format(code, **options) ⇒ Object



20
21
22
# File 'lib/rufo/erb_formatter.rb', line 20

def self.format(code, **options)
  new(code, **options).format
end

Instance Method Details

#formatObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rufo/erb_formatter.rb', line 34

def format
  out = []
  process_erb do |(type, content)|
    if type == :code
      formatted_code = process_code(content)
      indented_code = formatted_code.lines.join(" " * current_column)
      out << " #{indented_code} "
    else
      out << content
    end

    update_lineno(out.last)
    update_column(out.last)
  end
  @result = out.join("")
end