Class: Spork::Diagnoser

Inherits:
Object
  • Object
show all
Defined in:
lib/spork/diagnoser.rb

Class Method Summary collapse

Class Method Details

.add_included_file(filename, callstack) ⇒ Object



27
28
29
30
31
# File 'lib/spork/diagnoser.rb', line 27

def add_included_file(filename, callstack)
  filename = expand_filename(filename)
  return unless File.exist?(filename)
  loaded_files[filename] = filter_callstack(caller) if subdirectory?(filename)
end

.install_hook!(entry_file = nil, dir = Dir.pwd) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/spork/diagnoser.rb', line 7

def install_hook!(entry_file = nil, dir = Dir.pwd)
  @dir = File.expand_path(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
    
    def load(string)
      ::Spork::Diagnoser.add_included_file(string, caller)
      load_without_diagnoser(string)
    end
  end
end

.loaded_filesObject



3
4
5
# File 'lib/spork/diagnoser.rb', line 3

def loaded_files
  @loaded_files ||= {}
end

.output_results(stdout) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/spork/diagnoser.rb', line 45

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

.remove_hook!Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/spork/diagnoser.rb', line 33

def remove_hook!
  return unless Kernel.private_instance_methods.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