Class: DeadCodeDetector::Initializer

Inherits:
Object
  • Object
show all
Defined in:
lib/dead_code_detector/initializer.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.fully_enabledObject

Returns the value of attribute fully_enabled.



6
7
8
# File 'lib/dead_code_detector/initializer.rb', line 6

def fully_enabled
  @fully_enabled
end

.last_enabled_classObject

Returns the value of attribute last_enabled_class.



6
7
8
# File 'lib/dead_code_detector/initializer.rb', line 6

def last_enabled_class
  @last_enabled_class
end

Class Method Details

.allowed?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/dead_code_detector/initializer.rb', line 54

def allowed?
  if DeadCodeDetector.config.allowed.respond_to?(:call)
    DeadCodeDetector.config.allowed.call
  else
    DeadCodeDetector.config.allowed
  end
end

.cached_classesObject



62
63
64
# File 'lib/dead_code_detector/initializer.rb', line 62

def cached_classes
  DeadCodeDetector.config.storage.get(tracked_classes_key)
end

.clear_cacheObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/dead_code_detector/initializer.rb', line 23

def clear_cache
  cached_classes.each do |class_name|
    klass = Object.const_get(class_name) rescue nil
    if klass
      DeadCodeDetector::ClassMethodWrapper.new(klass).clear_cache
      DeadCodeDetector::InstanceMethodWrapper.new(klass).clear_cache
    end
  end
  DeadCodeDetector.config.storage.clear(tracked_classes_key)
end

.enable(klass) ⇒ Object



34
35
36
37
# File 'lib/dead_code_detector/initializer.rb', line 34

def enable(klass)
  DeadCodeDetector::ClassMethodWrapper.new(klass).wrap_methods!
  DeadCodeDetector::InstanceMethodWrapper.new(klass).wrap_methods!
end

.enable_for_cached_classes!Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dead_code_detector/initializer.rb', line 39

def enable_for_cached_classes!
  return if fully_enabled
  return unless allowed?
  classes = cached_classes.sort.to_a
  starting_index = (classes.index(last_enabled_class) || -1) + 1
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  classes[starting_index..-1].each do |class_name|
    klass = Object.const_get(class_name) rescue nil
    enable(klass) if klass
    self.last_enabled_class = class_name
    return if Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time > DeadCodeDetector.config.max_seconds_to_enable
  end
  self.fully_enabled = true
end

.refresh_cache_for(klass) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/dead_code_detector/initializer.rb', line 14

def refresh_cache_for(klass)
  self.fully_enabled = false
  self.last_enabled_class = nil
  classes = [klass, *descendants_of(klass)]
  classes.each do |class_to_enable|
    cache_methods_for(class_to_enable)
  end
end

.refresh_cachesObject



8
9
10
11
12
# File 'lib/dead_code_detector/initializer.rb', line 8

def refresh_caches
  DeadCodeDetector.config.classes_to_monitor.each do |klass|
    refresh_cache_for(klass)
  end
end