Class: Ramaze::AppGraph

Inherits:
Object show all
Defined in:
lib/ramaze/app_graph.rb

Overview

The AppGraph class can be used to generate a graph of all the URLs mapped in a Ramaze application and saves this graph as an image.

Usage

In order to generate a graph of your application all you need to do is the following:

 require 'ramaze/app_graph'

 graph = Ramaze::AppGraph.new graph.generate graph.show

Once this code is executed you can find the .dot and PNG files in the root directory of your application.

Author:

Instance Method Summary (collapse)

Constructor Details

- (AppGraph) initialize

Creates a new instance of the class.

Author:

  • Michael Fellinger



28
29
30
# File 'lib/ramaze/app_graph.rb', line 28

def initialize
  @out = Set.new
end

Instance Method Details

- (Object) connect(hash)

Connects various elements in the graph to each other.

Author:

  • Michael Fellinger



71
72
73
74
75
# File 'lib/ramaze/app_graph.rb', line 71

def connect(hash)
  hash.each do |from, to|
    @out << ("  %p -> %p;" % [from.to_s, to.to_s])
  end
end

- (Object) generate

Generates the graph based on all the current routes. The graph is saved in the application directory.

Author:

  • Michael Fellinger



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
63
64
# File 'lib/ramaze/app_graph.rb', line 38

def generate
  Ramaze::AppMap.to_hash.each do |location, app|
    connect(location => app.name)

    app.url_map.to_hash.each do |c_location, c_node|
      connect(app.name => c_node)
      connect(c_node.mapping => c_node)

      c_node.update_template_mappings
      c_node.view_templates.each do |wish, mapping|
        mapping.each do |action_name, template|
          action_path = File.join(c_node.mapping, action_name)
          connect(c_node => action_path, action_path => template)
        end
      end

      c_node.update_method_arities
      c_node.method_arities.each do |method, arity|
        action_path = File.join(c_node.mapping, method.to_s)
        connect(
          action_path => "#{c_node}##{method}[#{arity}]",
          c_node      => action_path
        )
      end
    end
  end
end

- (Object) show

Generates a PNG file based on the .dot file.

Author:

  • Michael Fellinger



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ramaze/app_graph.rb', line 95

def show
  write_dot
  options = {
    'rankdir' => 'LR',
    'splines' => 'true',
    'overlap' => 'false',
  }
  args = options.map{|k,v| "-G#{k}=#{v}" }
  system("dot -O -Tpng #{args.join(' ')} graph.dot")
  system('feh graph.dot.png')
end

- (Object) write_dot

Writes the dot file containing the graph data.

Author:

  • Michael Fellinger



82
83
84
85
86
87
88
# File 'lib/ramaze/app_graph.rb', line 82

def write_dot
  File.open('graph.dot', 'w+') do |dot|
    dot.puts 'digraph appmap {'
    dot.puts(*@out)
    dot.puts '}'
  end
end