class Timetrap::Formatters::Summary include Timetrap::Helpers def initialize(entries) @entries = entries end def output by_sheet_and_tag = Hash.new @entries.each do |entry| sheet = entry[:sheet] tag = 'default' if entry.note =~ /\bt(ag)?:(\w+)\b/ tag = $2 end by_sheet_and_tag[sheet] ||= Hash.new sum = (by_sheet_and_tag[sheet][tag] or 0.0) sum += entry.duration by_sheet_and_tag[sheet][tag] = sum end by_sheet_and_tag.map do |sheet, tags| total = 0.0 tags = tags.map do |tag, sum| total += sum sum = format_duration(sum.to_i) "#{tag}: #{sum}" end total = format_duration(total.to_i) "sheet: #{sheet}\n\t#{tags.join("\n\t")}\n\ttotal: #{total}" end end end