Sha256: abb4f52cc4686e1fa24511fa2f55df9cac096c8f523e287b5241a9b56020bb1c

Contents?: true

Size: 1.79 KB

Versions: 13

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

require 'json'

module KPM
  class TraceLogger
    def initialize
      @trace = {}
    end

    # Return JSON representation of the logs
    def to_json(*_args)
      @trace.to_json
    end

    # Return String representation of the logs
    def to_s
      @trace.to_s
    end

    # Return HASH representation of the logs
    def to_hash
      @trace
    end

    def add(group, key, message)
      add_to_hash(group, key, message)
    end

    private

    # This procedures will store the logs into a hash to be later returned
    def add_to_hash(group, key, message)
      if group.nil? || key.nil?
        add_with_key(group || key, message)
      else
        container_key = group.to_sym

        @trace[container_key] ||= {}
        child_key = key.to_sym

        if @trace[container_key][child_key].nil?
          @trace[container_key][child_key] = message
        else
          child_is_an_array = @trace[container_key][child_key].is_a?(Array)

          old_message = nil
          old_message = @trace[container_key][child_key] unless child_is_an_array
          @trace[container_key][child_key] = [] unless child_is_an_array
          @trace[container_key][child_key].push(old_message) unless old_message.nil?
          @trace[container_key][child_key].push(message)
        end
      end
    end

    def add_with_key(key, message)
      child_key = key.to_sym

      if @trace[child_key].nil?
        @trace[child_key] = message
      else
        child_is_an_array = @trace[child_key].is_a?(Array)

        old_message = nil
        old_message = @trace[child_key] unless child_is_an_array
        @trace[child_key] = [] unless child_is_an_array
        @trace[child_key].push(old_message) unless old_message.nil?
        @trace[child_key].push(message)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
kpm-0.10.6 lib/kpm/trace_logger.rb
kpm-0.11.1 lib/kpm/trace_logger.rb
kpm-0.10.5 lib/kpm/trace_logger.rb
kpm-0.11.0 lib/kpm/trace_logger.rb
kpm-0.10.4 lib/kpm/trace_logger.rb
kpm-0.10.3 lib/kpm/trace_logger.rb
kpm-0.10.2 lib/kpm/trace_logger.rb
kpm-0.10.1 lib/kpm/trace_logger.rb
kpm-0.10.0 lib/kpm/trace_logger.rb
kpm-0.9.0 lib/kpm/trace_logger.rb
kpm-0.8.2 lib/kpm/trace_logger.rb
kpm-0.8.1 lib/kpm/trace_logger.rb
kpm-0.8.0 lib/kpm/trace_logger.rb