Class: Bade::Runtime::GlobalsTracker
- Defined in:
- lib/bade/runtime/globals_tracker.rb
Overview
Tracks created global variables and constants in block.
Instance Attribute Summary collapse
- #caught_constants ⇒ Array<[Object, Symbol]>
- #caught_variables ⇒ Array<Symbol>
- #constants_location_prefixes ⇒ Array<String>?
Instance Method Summary collapse
-
#_filtered_constants ⇒ Array<[Object, Symbol]>
Filteres caught constants by location prefixes and returns ones that should be removed.
- #catch ⇒ T
- #clear_all ⇒ Object
- #clear_constants ⇒ Object
- #clear_global_variables ⇒ Object
-
#initialize(constants_location_prefixes: nil) ⇒ GlobalsTracker
constructor
A new instance of GlobalsTracker.
Constructor Details
#initialize(constants_location_prefixes: nil) ⇒ GlobalsTracker
Returns a new instance of GlobalsTracker.
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_constants ⇒ Array<[Object, Symbol]>
27 28 29 |
# File 'lib/bade/runtime/globals_tracker.rb', line 27 def caught_constants @caught_constants end |
#caught_variables ⇒ Array<Symbol>
24 25 26 |
# File 'lib/bade/runtime/globals_tracker.rb', line 24 def caught_variables @caught_variables end |
Instance Method Details
#_filtered_constants ⇒ Array<[Object, Symbol]>
Filteres caught constants by location prefixes and returns ones that should be removed.
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 |
#catch ⇒ 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_all ⇒ Object
63 64 65 66 |
# File 'lib/bade/runtime/globals_tracker.rb', line 63 def clear_all clear_global_variables clear_constants end |
#clear_constants ⇒ Object
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_variables ⇒ Object
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 |