Class: Bade::Runtime::GlobalsTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/bade/runtime/globals_tracker.rb

Overview

Tracks created global variables and constants in block.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constants_location_prefixes: nil) ⇒ GlobalsTracker

Returns a new instance of GlobalsTracker.

Parameters:

  • constants_location_prefixes (Array<String>, nil) (defaults to: nil)

    If given, only constants whose location starts with one of the prefixes will be removed. If nil, all constants will be removed.



35
36
37
38
39
# File 'lib/bade/runtime/globals_tracker.rb', line 35

def initialize(constants_location_prefixes: nil)
  @caught_variables = []
  @caught_constants = []
  @constants_location_prefixes = constants_location_prefixes
end

Instance Attribute Details

#caught_constantsArray<[Object, Symbol]>

Returns:



27
28
29
# File 'lib/bade/runtime/globals_tracker.rb', line 27

def caught_constants
  @caught_constants
end

#caught_variablesArray<Symbol>

Returns:



24
25
26
# File 'lib/bade/runtime/globals_tracker.rb', line 24

def caught_variables
  @caught_variables
end

#constants_location_prefixesArray<String>?

Returns:



30
31
32
# File 'lib/bade/runtime/globals_tracker.rb', line 30

def constants_location_prefixes
  @constants_location_prefixes
end

Instance Method Details

#_filtered_constantsArray<[Object, Symbol]>

Filteres caught constants by location prefixes and returns ones that should be removed.

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/bade/runtime/globals_tracker.rb', line 84

def _filtered_constants
  @caught_constants.select do |(obj, name)|
    next unless obj.const_defined?(name)

    begin
      location = obj.const_source_location(name)
    rescue ::ArgumentError
      next
    end

    next true if location.nil?

    path = location.first
    next false if path == false
    next false if $LOADED_FEATURES.include?(path)

    next true if constants_location_prefixes.nil?

    constants_location_prefixes&.any? { |prefix| path.start_with?(prefix) }
  end
end

#catchT

Yield Returns:

  • (T)

Returns:

  • (T)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bade/runtime/globals_tracker.rb', line 43

def catch
  before_variables = global_variables
  before_global_constants = Object.constants
  before_binding_constants = Bade::Runtime::RenderBinding.constants(false)

  res = nil
  begin
    res = yield
  ensure
    @caught_variables += global_variables - before_variables

    @caught_constants += (Object.constants - before_global_constants)
                         .map { |name| [Object, name] }
    @caught_constants += (Bade::Runtime::RenderBinding.constants(false) - before_binding_constants)
                         .map { |name| [Bade::Runtime::RenderBinding, name] }
  end

  res
end

#clear_allObject



63
64
65
66
# File 'lib/bade/runtime/globals_tracker.rb', line 63

def clear_all
  clear_global_variables
  clear_constants
end

#clear_constantsObject



68
69
70
71
72
73
# File 'lib/bade/runtime/globals_tracker.rb', line 68

def clear_constants
  _filtered_constants.each do |(obj, name)|
    obj.send(:remove_const, name)
  end
  @caught_constants = []
end

#clear_global_variablesObject



75
76
77
78
79
# File 'lib/bade/runtime/globals_tracker.rb', line 75

def clear_global_variables
  @caught_variables.each do |name|
    eval("#{name} = nil", binding, __FILE__, __LINE__)
  end
end