lib/update_repo/metrics.rb in update_repo-0.9.10 vs lib/update_repo/metrics.rb in update_repo-0.10.1

- old
+ new

@@ -1,7 +1,10 @@ +# frozen_string_literal: true + require 'update_repo/version' require 'update_repo/helpers' +require 'yaml' module UpdateRepo # Class : Metrics. # This class takes care of storing the metrics for processed, failures, etc. class Metrics @@ -27,8 +30,34 @@ # @param key [symbol] the key to set # @param value [symbol] set 'key' to this value. # @return [value] Return the value set. def []=(key, value) @metrics[key] = value + end + + # This will save any (git) errors encountered to a file, + # so they can be reprinted again at a later date. + # If no errors, then delete any previously existing error file. + # @param config [instance] of Config class + # @return [void] + def save_errors(config) + # get the location of the config file, we'll use the same dir + # and base name + path = config.config_path + '.errors' + if @metrics[:failed_list].empty? + # delete any existing file if we have no errors + File.delete(path) if File.exist?(path) + else + # otherwise save the errors to file + File.open(path, 'w') { |file| file.write @metrics[:failed_list].to_yaml } + end + end + + # loads an error file (if exists) into the @metrics[:failed_list]. + # @param config [instance] of Config class + # @return [void] + def load_errors(config) + path = config.config_path + '.errors' + @metrics[:failed_list] = YAML.load_file(path) if File.exist?(path) end end end