# require "eitil_integrate/application_exporter/auto_sum/sum_data" module EitilIntegrate::RubyXL module AutoSum class << self # Reduce values and alter data structure. def sum_data sum_floats sum_time_strings unpack_arrays end def sum_floats @hash.transform_values! { |array| float_array?(array) ? [array.sum] : array } end def sum_time_strings @hash.transform_values! { |array| time_string_array?(array) ? chronic_sum_array(array) : array } end def chronic_sum_array(array) sum = array.map { |item| ChronicDuration.parse(item) }.compact.sum hours = format_time(sum / (60 * 60)) sum = sum % (60 * 60) minutes = format_time(sum / 60) seconds = format_time(sum % 60) # currently doesn't return seconds, since those will never # be set (?) and screw the consistency of data formatting ["#{hours}:#{minutes}"] end def format_time(time) time.to_s.length == 1 ? "0#{time}" : time.to_s end def float_array?(array) return false if array.empty? array.all? { |item| item.is_a?(Float) } end def time_string_array?(array) return false if array.empty? array.all? { |item| item.is_a?(String) && item.scan(/\d{2}:\d{2}:\d{2}/) } end def unpack_arrays @hash.transform_values! { |array| array.empty? ? nil : array.first } end end end end