Skip to content

Instantly share code, notes, and snippets.

@ryrych
Created March 3, 2016 07:05
Show Gist options
  • Save ryrych/e8c68c82db1faf1a5f94 to your computer and use it in GitHub Desktop.
Save ryrych/e8c68c82db1faf1a5f94 to your computer and use it in GitHub Desktop.
Generate graph of jsonapi resources
# railroady -v --hide-private -i -o test.dot -s 'app/resources/**/*_resource.rb' -C && neato -Tpng -O test.dot
# RailRoady - RoR diagrams generator
# http://railroad.rubyforge.org
#
# Copyright 2007-2008 - Javier Smaldone (http://www.smaldone.com.ar)
# See COPYING for more details
require 'railroady/app_diagram'
# RailRoady controllers diagram
class ControllersDiagram < AppDiagram
def initialize(options = OptionsStruct.new)
super options
@graph.diagram_type = 'Controllers'
end
# Process controller files
def generate
STDERR.print "Generating controllers diagram\n" if @options.verbose
files = get_files
files.each do |f|
class_name = extract_class_name(f)
begin
process_class class_name.constantize
rescue Exception
STDERR.print "Warning: exception #{$ERROR_INFO} raised while trying to load controller class #{f}"
end
end
end # generate
def get_files(prefix = '')
files = [email protected]? ? Dir.glob(@options.specify) : Dir.glob(prefix << 'app/controllers/**/*_controller.rb')
files -= Dir.glob(@options.exclude)
files
end
def extract_class_name(filename)
filename = filename.match(/.*\/resources\/(.*).rb$/)[1]
filename = "Api::V1::#{filename.split('/')[2].camelize}"
filename
end
private
# Load controller classes
def load_classes
disable_stdout
get_files.each { |c| require "./#{c}" }
enable_stdout
rescue LoadError
enable_stdout
print_error 'controller classes'
raise
end # load_classes
# Proccess a controller class
def process_class(current_class)
STDERR.print "\tProcessing #{current_class}\n" if @options.verbose
if @options.brief
@graph.add_node ['controller-brief', current_class.name]
elsif current_class.is_a? Class
# Collect controller's methods
node_attribs = { public: [],
protected: [],
private: [] }
current_class.public_instance_methods(false).sort.each do |m|
node_attribs[:public] << m
end unless @options.hide_public
current_class.protected_instance_methods(false).sort.each do |m|
node_attribs[:protected] << m
end unless @options.hide_protected
current_class.private_instance_methods(false).sort.each do |m|
node_attribs[:private] << m
end unless @options.hide_private
@graph.add_node ['controller', current_class.name, node_attribs]
elsif @options.modules && current_class.is_a?(Module)
@graph.add_node ['module', current_class.name]
end
if @options.inheritance
@graph.add_edge ['is-a', current_class.superclass.name, current_class.name]
end
end # process_class
end # class ControllersDiagram
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment