Class: Caboose::CommentRoutes

Inherits:
Object
  • Object
show all
Defined in:
app/models/caboose/comment_routes.rb

Class Method Summary collapse

Class Method Details

.parse_controllersObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/caboose/comment_routes.rb', line 4

def CommentRoutes.parse_controllers    

  classes = []        
  files = Dir.glob(Rails.root.join('app', 'controllers','*.rb'))
  for file in files    
    f = Rails.root.join('app', 'controllers', file)
    f2 = File.open(f, "r")
    
    class_name = nil
    class_priority = 20
    route_priority = 20
    uris = []
    actions = []
    f2.each_line do |line|      
      line = line.strip        
      if line =~ /^(.*?)class (.*?)Controller(.*?)$/
        class_name = line.gsub(/^(.*?)class (.*?)Controller(.*?)$/, '\2').gsub(/([A-Z])/, '_\1').downcase
        class_name[0] = '' if class_name[0] == '_'
      elsif line =~ /# @class_route_priority \d/
        class_priority = line.gsub(/# @class_route_priority (\d*?)$/, '\1').to_i
      elsif line =~ /# @route_priority \d/
        route_priority = line.gsub(/# @route_priority (\d*?)$/, '\1').to_i
      elsif line.starts_with?('def ')
        actions << [line.gsub('def ', ''), uris, route_priority]              
        uris = []
        route_priority = 20
      elsif line =~ /# @route GET (.*?)/       then uris << "get    \"#{line.gsub(/# @route GET (.*?)/       , '\1')}\""          
      elsif line =~ /# @route POST (.*?)/      then uris << "post   \"#{line.gsub(/# @route POST (.*?)/      , '\1')}\""          
      elsif line =~ /# @route PUT (.*?)/       then uris << "put    \"#{line.gsub(/# @route PUT (.*?)/       , '\1')}\""          
      elsif line =~ /# @route DELETE (.*?)/    then uris << "delete \"#{line.gsub(/# @route DELETE (.*?)/    , '\1')}\""
      end
    end      
    classes << [class_name, actions, class_priority]
  end

  routes = []
  classes.sort_by{ |arr| arr[2] }.each do |carr|
    
    class_name = carr[0]
    actions = carr[1]
    
    # Get the longest URI so we can make routes that line up vertically
    longest = ''
    actions.each{ |action, uris| uris.each{ |uri| longest = uri if uri.length > longest.length }}
    length = longest.length + 1
    
    # Make the route line
    actions.sort_by{ |arr| arr[2] }.each do |arr|
      action = arr[0]
      uris = arr[1]
      uris.each do |uri|
        #puts "#{uri.ljust(length, ' ')} => \"#{class_name}\##{action}\""
        routes << "#{uri.ljust(length, ' ')} => \"#{class_name}\##{action}\"" 
      end
    end
    puts ""
  end      
  return routes      
end