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.
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_constants ⇒ Array<[Object, Symbol]>
25 26 27 |
# File 'lib/bade/runtime/globals_tracker.rb', line 25 def caught_constants @caught_constants end |
#caught_variables ⇒ Array<Symbol>
22 23 24 |
# File 'lib/bade/runtime/globals_tracker.rb', line 22 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.
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 |
#catch ⇒ 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_all ⇒ Object
61 62 63 64 |
# File 'lib/bade/runtime/globals_tracker.rb', line 61 def clear_all clear_global_variables clear_constants end |
#clear_constants ⇒ Object
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_variables ⇒ Object
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 |