Class: Spork::Diagnoser
- Inherits:
-
Object
- Object
- Spork::Diagnoser
- Defined in:
- lib/spork/diagnoser.rb
Overview
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)
Class Method Summary (collapse)
- + (Object) add_included_file(filename, callstack)
-
+ (Object) install_hook!(entry_file = nil, dir = Dir.pwd)
Installs the diagnoser hook into Kernel#require and Kernel#load.
- + (Object) loaded_files
-
+ (Object) output_results(stdout)
output the results of a diagnostic run.
-
+ (Object) remove_hook!
Uninstall the hook.
Class Method Details
+ (Object) add_included_file(filename, callstack)
43 44 45 46 47 |
# File 'lib/spork/diagnoser.rb', line 43 def add_included_file(filename, callstack) filename = (filename) return unless File.exist?(filename) loaded_files[filename] = filter_callstack(caller) if subdirectory?(filename) end |
+ (Object) install_hook!(entry_file = nil, dir = Dir.pwd)
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.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/spork/diagnoser.rb', line 21 def install_hook!(entry_file = nil, dir = Dir.pwd) @dir = File.(Dir.pwd, dir) @entry_file = entry_file Kernel.class_eval do alias :require_without_diagnoser :require alias :load_without_diagnoser :load def require(string) ::Spork::Diagnoser.add_included_file(string, caller) require_without_diagnoser(string) end private :require def load(string) ::Spork::Diagnoser.add_included_file(string, caller) load_without_diagnoser(string) end private :load end end |
+ (Object) loaded_files
11 12 13 |
# File 'lib/spork/diagnoser.rb', line 11 def loaded_files @loaded_files ||= {} end |
+ (Object) output_results(stdout)
output the results of a diagnostic run.
Parameters
-
stdout - An IO stream to output the results to.
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/spork/diagnoser.rb', line 67 def output_results(stdout) project_prefix = Dir.pwd + "/" minimify = lambda { |f| f.gsub(project_prefix, '')} stdout.puts "- Spork Diagnosis -\n" stdout.puts "-- Summary --" stdout.puts loaded_files.keys.sort.map(&minimify) stdout.puts "\n\n\n" stdout.puts "-- Detail --" loaded_files.keys.sort.each do |file| stdout.puts "\n\n\n--- #{minimify.call(file)} ---\n" stdout.puts loaded_files[file].map(&minimify) end end |
+ (Object) remove_hook!
Uninstall the hook. Generally useful only for testing the Diagnoser.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/spork/diagnoser.rb', line 50 def remove_hook! return unless Kernel.private_instance_methods.map(&:to_sym).include?(:require_without_diagnoser) Kernel.class_eval do alias :require :require_without_diagnoser alias :load :load_without_diagnoser undef_method(:require_without_diagnoser) undef_method(:load_without_diagnoser) end true end |