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.



33
34
35
36
37
# File 'lib/bade/runtime/globals_tracker.rb', line 33

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:



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

def caught_constants
  @caught_constants
end

#caught_variablesArray<Symbol>

Returns:



22
23
24
# File 'lib/bade/runtime/globals_tracker.rb', line 22

def caught_variables
  @caught_variables
end

#constants_location_prefixesArray<String>?

Returns:



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

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:



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

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)


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

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



61
62
63
64
# File 'lib/bade/runtime/globals_tracker.rb', line 61

def clear_all
  clear_global_variables
  clear_constants
end

#clear_constantsObject



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

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

#clear_global_variablesObject



73
74
75
76
77
# File 'lib/bade/runtime/globals_tracker.rb', line 73

def clear_global_variables
  @caught_variables.each do |name|
    eval("#{name} = nil", binding, __FILE__, __LINE__) # rubocop:disable Security/Eval
  end
end