class Routler::Parser

Attributes

controllers[RW]
error_lines[RW]
lines[RW]

Public Class Methods

new() click to toggle source
# File lib/routler/parser.rb, line 8
def initialize
  @controllers = Hash.new
  @error_lines = []
  @lines = 0
end

Public Instance Methods

add_controller(controller) click to toggle source
# File lib/routler/parser.rb, line 36
def add_controller(controller)
  if controller.fetch :error, nil
    @error_lines << controller[:error]
  else
    name = controller[:name]
    action = controller[:action]
    @controllers[name] = Controller.new(name) if !@controllers.fetch name, nil
    @controllers[name].add_action action
  end
end
all_controllers() click to toggle source
# File lib/routler/parser.rb, line 47
def all_controllers
  cont = []
  @controllers.keys.sort.each do |k|
    cont << @controllers[k]
  end
  cont
end
parse(content) click to toggle source
# File lib/routler/parser.rb, line 14
def parse(content)
  content.each_line do |line|
    @lines += 1
    add_controller(parse_line line)
  end
  all_controllers
end
parse_line(line) click to toggle source
# File lib/routler/parser.rb, line 22
def parse_line(line)
  match = line.match(/^(\s{1,})?(\S*\s\S*\s{1,}\/\S*)(?<cont>.*)\#(?<action>\S*)/)
  controller = Hash.new
  begin
    if match[:cont]
      controller[:name] = match[:cont].strip
      controller[:action] = match[:action].strip.chomp
    end
  rescue
    controller[:error] = line
  end
  controller
end