class Droutes::Parser

Attributes

routes[R]

Public Class Methods

new(routes) click to toggle source
# File lib/droutes/parser.rb, line 40
def initialize(routes)
  @routes = ::Hash.new {|h,k| h[k] = {}}
  routes.each do |route|
    wrap = ActionDispatch::Routing::RouteWrapper.new(route)
    next if wrap.internal?
    @routes[wrap.controller][wrap.action] = wrap
  end
end

Public Instance Methods

parse() click to toggle source
# File lib/droutes/parser.rb, line 49
def parse
  files = Dir["#{Rails.application.root}/app/controllers/**/*.rb"]
  root = ClassStruct.new(nil)
  files.each do |file|
    parser = YARD::Parser::SourceParser.new.parse(file)
    handle(parser.enumerator, root)
  end
  root
end

Protected Instance Methods

handle(ast, klass) click to toggle source
# File lib/droutes/parser.rb, line 61
def handle(ast, klass)
  ast.each {|node| handle_class(node, klass) if node.is_a?(YARD::Parser::Ruby::ClassNode)}
end
handle_class(ast, klass) click to toggle source
# File lib/droutes/parser.rb, line 77
def handle_class(ast, klass)
  class_name = ast.class_name.path.join("::")
  newKlass = ClassStruct.new(class_name, ast.docstring)
  handle_node(ast, newKlass)
  klass.children.append(newKlass) if klass
end
handle_def(ast, klass) click to toggle source
# File lib/droutes/parser.rb, line 84
def handle_def(ast, klass)
  action = ast.method_name(true).to_s
  route = @routes[klass.controller][action]
  return unless route
  klass.add_to_path(Struct.new(klass,
                               action,
                               route,
                               YARD::Docstring.new(ast.docstring)))
end
handle_node(ast, klass) click to toggle source
# File lib/droutes/parser.rb, line 65
def handle_node(ast, klass)
  ast.each do |node|
    if node.is_a?(YARD::Parser::Ruby::ClassNode)
      handle_class(node, klass)
    elsif node.is_a?(YARD::Parser::Ruby::MethodDefinitionNode)
      handle_def(node, klass)
    elsif node.is_a?(YARD::Parser::Ruby::AstNode)
      handle_node(node, klass)
    end
  end
end