Sha256: 320670d29cd445d675d0523a349df650b0356a2a03c187248ba51ed24229a00c

Contents?: true

Size: 940 Bytes

Versions: 2

Compression:

Stored size: 940 Bytes

Contents

# frozen_string_literal: true

module Lograge
  module Formatters
    class KeyValueDeep < KeyValue
      def call(data)
        super flatten_keys(data)
      end

      protected

      def flatten_keys(data, prefix = '')
        return flatten_object(data, prefix) if [Hash, Array].include? data.class

        data
      end

      def flatten_object(data, prefix)
        result = {}
        loop_on_object(data) do |key, value|
          key = "#{prefix}_#{key}" unless prefix.empty?
          if [Hash, Array].include? value.class
            result.merge!(flatten_keys(value, key))
          else
            result[key] = value
          end
        end
        result
      end

      def loop_on_object(data, &block)
        if data.instance_of? Array
          data.each_with_index do |value, index|
            yield index, value
          end
          return
        end
        data.each(&block)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lograge-0.14.0 lib/lograge/formatters/key_value_deep.rb
lograge-0.13.0 lib/lograge/formatters/key_value_deep.rb