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

Class Method Details

.sweep(obj) {|value, parent_key| ... } ⇒ Object

Traverse obj and return a new structure with String leaves transformed by block.

Parameters:

  • obj (Object)

    the object to sweep (typically Hash/Array/String/scalar).

Yields:

  • (value, parent_key)

    invoked for each String leaf.

Yield Parameters:

  • value (String)

    the String value.

  • parent_key (Object, nil)

    the Hash key under which value lives, or nil when the String is a top-level scalar; propagated from the nearest containing Hash when inside Arrays.

Yield Returns:

  • (Object)

    the replacement value.

Returns:

  • (Object)

    the transformed structure.

Raises:

  • (ArgumentError)


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