Parent

Spork::Diagnoser

The Diagnoser hooks into load and require and keeps track of when files are required / loaded, and who loaded them. It’s used when you run spork —diagnose

Example

 
 Spork::Diagnoser.install_hook!('/path/env.rb', '/path')
 require '/path/to/env.rb'
 Spork::Diagnoser.output_results(STDOUT)

Public Class Methods

add_included_file(filename, callstack) click to toggle source
    # File lib/spork/diagnoser.rb, line 43
43:     def add_included_file(filename, callstack)
44:       filename = expand_filename(filename)
45:       return unless File.exist?(filename)
46:       loaded_files[filename] = filter_callstack(caller) if subdirectory?(filename)
47:     end
install_hook!(entry_file = nil, dir = Dir.pwd) click to toggle source

Installs the diagnoser hook into Kernel#require and Kernel#load

Parameters

  • entry_file - The file that is used to load the project. Used to filter the backtrace so anything that happens after it is hidden.

  • dir - The project directory. Any file loaded outside of this directory will not be logged.

    # File lib/spork/diagnoser.rb, line 21
21:     def install_hook!(entry_file = nil, dir = Dir.pwd)
22:       @dir = File.expand_path(Dir.pwd, dir)
23:       @entry_file = entry_file
24:       
25:       Kernel.class_eval do
26:         alias :require_without_diagnoser :require
27:         alias :load_without_diagnoser :load
28:         
29:         def require(string)
30:           ::Spork::Diagnoser.add_included_file(string, caller)
31:           require_without_diagnoser(string)
32:         end
33:         private :require
34:         
35:         def load(string)
36:           ::Spork::Diagnoser.add_included_file(string, caller)
37:           load_without_diagnoser(string)
38:         end
39:         private :load
40:       end
41:     end
load(string) click to toggle source
    # File lib/spork/diagnoser.rb, line 35
35:         def load(string)
36:           ::Spork::Diagnoser.add_included_file(string, caller)
37:           load_without_diagnoser(string)
38:         end
loaded_files() click to toggle source
    # File lib/spork/diagnoser.rb, line 11
11:     def loaded_files
12:       @loaded_files ||= {}
13:     end
output_results(stdout) click to toggle source

output the results of a diagnostic run.

Parameters

  • stdout - An IO stream to output the results to.

    # File lib/spork/diagnoser.rb, line 67
67:     def output_results(stdout)
68:       project_prefix = Dir.pwd + "/"
69:       minimify = lambda { |f| f.gsub(project_prefix, '')}
70:       stdout.puts "- Spork Diagnosis -\n"
71:       stdout.puts "-- Summary --"
72:       stdout.puts loaded_files.keys.sort.map(&minimify)
73:       stdout.puts "\n\n\n"
74:       stdout.puts "-- Detail --"
75:       loaded_files.keys.sort.each do |file|
76:         stdout.puts "\n\n\n--- #{minimify.call(file)} ---\n"
77:         stdout.puts loaded_files[file].map(&minimify)
78:       end
79:     end
remove_hook!() click to toggle source

Uninstall the hook. Generally useful only for testing the Diagnoser.

    # File lib/spork/diagnoser.rb, line 50
50:     def remove_hook!
51:       return unless Kernel.private_instance_methods.include?('require_without_diagnoser')
52:       Kernel.class_eval do
53:         alias :require :require_without_diagnoser
54:         alias :load :load_without_diagnoser
55:         
56:         undef_method(:require_without_diagnoser)
57:         undef_method(:load_without_diagnoser)
58:       end
59:       true
60:     end
require(string) click to toggle source
    # File lib/spork/diagnoser.rb, line 29
29:         def require(string)
30:           ::Spork::Diagnoser.add_included_file(string, caller)
31:           require_without_diagnoser(string)
32:         end

Private Class Methods

expand_filename(filename) click to toggle source
    # File lib/spork/diagnoser.rb, line 91
91:       def expand_filename(filename)
92:         ([Dir.pwd] + $:).each do |attempted_path|
93:           attempted_filename = File.expand_path(filename, attempted_path)
94:           return attempted_filename if File.file?(attempted_filename)
95:           attempted_filename = attempted_filename + ".rb"
96:           return attempted_filename if File.file?(attempted_filename)
97:         end
98:         filename
99:       end
filter_callstack(callstack, entry_file = @entry_file) click to toggle source
    # File lib/spork/diagnoser.rb, line 82
82:       def filter_callstack(callstack, entry_file = @entry_file)
83:         callstack.pop until callstack.empty? || callstack.last.include?(@entry_file) if @entry_file
84:         callstack.map do |line|
85:           next if line.include?('lib/spork/diagnoser.rb')
86:           line.gsub!('require_without_diagnoser', 'require')
87:           line
88:         end.compact
89:       end
subdirectory?(directory) click to toggle source
     # File lib/spork/diagnoser.rb, line 101
101:       def subdirectory?(directory)
102:         File.expand_path(directory, Dir.pwd).include?(@dir)
103:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.