Class: RailsLens::Route::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_lens/route/parser.rb

Overview

Handles parsing controller files and groups lines into categories

Class Method Summary collapse

Class Method Details

.call(path:, actions:) ⇒ Hash

Parse a controller file and return grouped content

Parameters:

  • path (String)

    File path to parse

  • actions (Array<String>)

    List of valid actions for this controller

Returns:

  • (Hash)

    { content => String, groups => Array<Hash> }



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rails_lens/route/parser.rb', line 13

def call(path:, actions:)
  groups = []
  group = {}
  content = File.read(path)

  content.each_line.with_index do |line, index|
    parsed_line = parse_line(line: line, actions: actions)

    if group[:type] == parsed_line[:type]
      # Same group. Push the current line into the current group.
      group[:body] += line
    else
      # Now looking at a new group. Push the current group onto the array
      # and start a new one.
      groups.push(group) unless group.empty?
      group = parsed_line.merge(line_number: index + 1)
    end
  end

  # Push the last group onto the array and return.
  groups.push(group)
  { content: content, groups: groups }
end