Class: Lumberjack::Formatter::StructuredFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/lumberjack/formatter/structured_formatter.rb

Overview

Dereference arrays and hashes and recursively call formatters on each element. This formatter provides deep traversal of nested data structures, applying formatting to all contained elements while handling circular references safely.

The StructuredFormatter is essential for formatting complex nested objects like configuration hashes, API responses, or any hierarchical data structures that need consistent formatting throughout their entire structure.

Defined Under Namespace

Classes: RecusiveReferenceError

Instance Method Summary collapse

Constructor Details

#initialize(formatter = nil) ⇒ StructuredFormatter

Returns a new instance of StructuredFormatter.

Parameters:

  • formatter (Formatter, nil) (defaults to: nil)

    The formatter to call on each element in the structure. If nil, elements are returned unchanged.



24
25
26
# File 'lib/lumberjack/formatter/structured_formatter.rb', line 24

def initialize(formatter = nil)
  @formatter = formatter
end

Instance Method Details

#call(obj) ⇒ Object

Format a structured object by recursively processing all nested elements.

Parameters:

  • obj (Object)

    The object to format. Arrays and hashes are traversed recursively, while other objects are passed to the configured formatter.

Returns:

  • (Object)

    The formatted structure with all nested elements processed.



33
34
35
# File 'lib/lumberjack/formatter/structured_formatter.rb', line 33

def call(obj)
  call_with_references(obj, Set.new)
end