Module: VectorMCP::Util::TokenSweeper
- Defined in:
- lib/vector_mcp/util/token_sweeper.rb
Overview
Stateless recursive traversal utility for parsed JSON-like structures.
TokenSweeper.sweep walks Hashes, Arrays, and String leaves, yielding each String value together with its parent Hash key (or the enclosing Hash key of the nearest containing Array). The block’s return value replaces the String in the output. All other scalar types (Integer, Float, nil, Boolean, etc.) are returned unchanged and are not yielded.
The method is purely functional: it never mutates the input structure and always returns a fresh Hash/Array spine when containers are encountered. Circular references are detected via an identity-compared visited set and the originally-referenced node is returned unchanged on cycles.
Class Method Summary collapse
-
.sweep(obj) {|value, parent_key| ... } ⇒ Object
Traverse
objand return a new structure with String leaves transformed byblock.
Class Method Details
.sweep(obj) {|value, parent_key| ... } ⇒ Object
Traverse obj and return a new structure with String leaves transformed by block.
30 31 32 33 34 |
# File 'lib/vector_mcp/util/token_sweeper.rb', line 30 def self.sweep(obj, &block) raise ArgumentError, "TokenSweeper.sweep requires a block" unless block walk(obj, nil, {}.compare_by_identity, &block) end |