lib/kpm/trace_logger.rb in kpm-0.7.2 vs lib/kpm/trace_logger.rb in kpm-0.8.0

- old
+ new

@@ -1,15 +1,17 @@ +# frozen_string_literal: true + require 'json' module KPM class TraceLogger def initialize - @trace = Hash.new + @trace = {} end # Return JSON representation of the logs - def to_json + def to_json(*_args) @trace.to_json end # Return String representation of the logs def to_s @@ -19,52 +21,52 @@ # Return HASH representation of the logs def to_hash @trace end - def add(group=nil, key, message) - add_to_hash(group,key,message); + 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=nil, key, message) + # 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] ||= Hash.new + @trace[container_key] ||= {} child_key = key.to_sym - unless @trace[container_key][child_key].nil? - child_is_an_array = @trace[container_key][child_key].kind_of?(Array) + 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) - else - @trace[container_key][child_key] = message end end end def add_with_key(key, message) child_key = key.to_sym - unless @trace[child_key].nil? - child_is_an_array = @trace[child_key].kind_of?(Array) + 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) - else - @trace[child_key] = message end end end -end \ No newline at end of file +end