class Lumberjack::Formatter::StructuredFormatter

Dereference arrays and hashes and recursively call formatters on each element.

Public Class Methods

new(formatter = nil) click to toggle source
# File lib/lumberjack/formatter/structured_formatter.rb, line 12
def initialize(formatter = nil)
  @formatter = formatter
end

Public Instance Methods

call(obj) click to toggle source
# File lib/lumberjack/formatter/structured_formatter.rb, line 16
def call(obj)
  call_with_references(obj, Set.new)
end

Private Instance Methods

call_with_references(obj, references) click to toggle source
# File lib/lumberjack/formatter/structured_formatter.rb, line 22
def call_with_references(obj, references)
  if obj.is_a?(Hash)
    with_object_reference(obj, references) do
      hash = {}
      obj.each do |name, value|
        value = call_with_references(value, references)
        hash[name.to_s] = value unless value.is_a?(RecusiveReferenceError)
      end
      hash
    end
  elsif obj.is_a?(Enumerable) && obj.respond_to?(:size) && obj.size != Float::INFINITY
    with_object_reference(obj, references) do
      array = []
      obj.each do |value|
        value = call_with_references(value, references)
        array << value unless value.is_a?(RecusiveReferenceError)
      end
      array
    end
  elsif @formatter
    @formatter.format(obj)
  else
    obj
  end
end
with_object_reference(obj, references) { || ... } click to toggle source
# File lib/lumberjack/formatter/structured_formatter.rb, line 48
def with_object_reference(obj, references)
  if obj.is_a?(Enumerable)
    return RecusiveReferenceError.new if references.include?(obj.object_id)
    references << obj.object_id
    begin
      yield
    ensure
      references.delete(obj.object_id)
    end
  else
    yield
  end
end