Sha256: 38ef37d6dd184ce92f699eec2059e1361fa0d4859bcd226293d8249cc857f15d

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literals: true

require "set"

module Lumberjack
  class Formatter
    # Dereference arrays and hashes and recursively call formatters on each element.
    class StructuredFormatter
      def initialize(formatter = nil)
        @formatter = formatter
      end

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

      private

      def call_with_references(obj, references)
        if obj.is_a?(Hash)
          hash = {}
          references << obj.object_id
          obj.each do |name, value|
            next if references.include?(value.object_id)
            references << value
            hash[name.to_s] = call_with_references(value, references)
          end
          references.delete(obj.object_id)
          hash
        elsif obj.is_a?(Enumerable) && obj.respond_to?(:size) && obj.size != Float::INFINITY
          array = []
          references << obj.object_id
          obj.each do |value|
            next if references.include?(value.object_id)
            references << value
            array << call_with_references(value, references)
          end
          references.delete(obj.object_id)
          array
        elsif @formatter
          @formatter.format(obj)
        else
          obj
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lumberjack-1.2.4 lib/lumberjack/formatter/structured_formatter.rb
lumberjack-1.2.3 lib/lumberjack/formatter/structured_formatter.rb