Class: Zeus::LoadTracking

Inherits:
Object
  • Object
show all
Defined in:
lib/zeus/load_tracking.rb

Class Method Summary collapse

Class Method Details

.add_feature(file) ⇒ Object

Check the load path first to see if the file getting loaded is already loaded. Otherwise, add the file to the $untracked_features array which then gets added to $LOADED_FEATURES array.



36
37
38
39
40
41
42
# File 'lib/zeus/load_tracking.rb', line 36

def add_feature(file)
  full_path = File.expand_path(file)

  if find_in_load_path(full_path) || File.exist?(full_path)
    add_extra_feature(full_path)
  end
end

.all_featuresObject

$LOADED_FEATURES global variable is used internally by Rubygems



45
46
47
48
# File 'lib/zeus/load_tracking.rb', line 45

def all_features
  untracked = defined?($untracked_features) ? $untracked_features : []
  $LOADED_FEATURES + untracked
end

.features_loaded_by(&block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/zeus/load_tracking.rb', line 4

def features_loaded_by(&block)
  old_features = all_features

  # Catch exceptions so we can determine the features
  # that were being loaded at the time of the exception.
  err_features = []
  begin
    yield
  rescue SyntaxError => err
    # SyntaxErrors are a bit weird in that the file containing
    # the error is not in the backtrace, only the error message.
    match = /\A([^:]+):\d+: syntax error/.match(err.message)
    err_features << match[1] if match
  rescue Exception => err
    # Just capture this to add to the err_features list
  end

  if err && err.backtrace
    err_features += err.backtrace.map { |b| b.split(':').first }
                       .select { |f| f.start_with?('/') }
                       .take_while { |f| f != __FILE__ }
  end

  new_features = all_features + err_features - old_features
  new_features.uniq!

  [new_features, err]
end