Module: Tins::DeepTransform

Included in:
HashSymbolizeKeysRecursive
Defined in:
lib/tins/deep_transform.rb

Overview

DeepTransform provides a robust engine for traversing an object tree and applying a transformation to each node iteratively, preventing stack overflows.

Instance Method Summary collapse

Instance Method Details

#deep_transform(key: nil, value: nil, circular: nil) ⇒ Object

Transforms an object and its children using the provided lambdas. This implementation uses an iterative bottom-up approach to avoid SystemStackError.

Parameters:

  • key (Proc, nil) (defaults to: nil)

    An optional lambda/proc used to transform hash keys during reconstruction. Defaults to an identity transformation.

  • value (Proc, nil) (defaults to: nil)

    An optional lambda/proc used to transform nodes (values) during reconstruction. Its behavior depends on its arity:

    • Arity 1: called with (node)
    • Arity 2: called with (index_or_key, node)
    • Arity 3: called with (index_or_key, node, parent_container) Defaults to an identity transformation.
  • circular (Object) (defaults to: nil)

    value returned when a circular reference is detected

Returns:

  • (Object)

    the fully transformed object tree

Raises:

  • (ArgumentError)

    if a block is provided (use key: and value: instead)



21
22
23
24
# File 'lib/tins/deep_transform.rb', line 21

def deep_transform(key: nil, value: nil, circular: nil)
  block_given? and raise ArgumentError, '&block not supported'
  _transform_iterative(self, key: key, value: value, circular: circular)
end