Class: Bulldogger::Formatter

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

Overview

Turns an arbitrary Ruby value into a bounded, JSON-safe String. Bounded because a snapshot is written on every failing test, so an unbounded value (a huge String, a deep object graph) would make capture itself expensive. Safe because inspect is arbitrary user code: it can raise, and the object it runs on may descend from BasicObject, where even #class is undefined.

Constant Summary collapse

MAX_ELEMENTS =
10

Instance Method Summary collapse

Constructor Details

#initialize(config:, redactor:) ⇒ Formatter

Returns a new instance of Formatter.



13
14
15
16
# File 'lib/bulldogger/formatter.rb', line 13

def initialize(config:, redactor:)
  @max_value_length = config.max_value_length
  @redactor = redactor
end

Instance Method Details

#format(value) ⇒ Object

Returns the per-local entry shape: => str normally, plus "truncated"/"original_length" when the final string was too long. Those extra keys are the record that something was cut -- they must never appear on a value that was not, so a reader can trust their absence.



23
24
25
# File 'lib/bulldogger/formatter.rb', line 23

def format(value)
  truncate_entry(render(value))
end

#format_self(value) ⇒ Object

Returns just the (possibly truncated) String, for JSON slots that hold a plain string rather than a "value"=>... entry -- a frame's "self", for example.



30
31
32
# File 'lib/bulldogger/formatter.rb', line 30

def format_self(value)
  format(value)["value"]
end