class Timesheets::Commands::Summary

Public Instance Methods

run() click to toggle source
# File lib/timesheets/commands/summary.rb, line 4
def run
  puts summary_table
end

Private Instance Methods

comma_numbers(number, delimiter = ',') click to toggle source
# File lib/timesheets/commands/summary.rb, line 61
def comma_numbers(number, delimiter = ',')
        sprintf('%0.02f', number).reverse.gsub(%r{([0-9]{3}(?=([0-9])))}, "\\1#{delimiter}").reverse
end
entries() click to toggle source
Calls superclass method Timesheets::Commands::Base#entries
# File lib/timesheets/commands/summary.rb, line 73
def entries
  the_entries = super

                          if options['week-of']
    week_num = DateTime.parse(options['week-of']).strftime("%U")
    the_entries = the_entries.select {|entry| entry.first.strftime("%U") == week_num }
                          end

                          the_entries
end
entries_by_day(entries) click to toggle source
# File lib/timesheets/commands/summary.rb, line 84
def entries_by_day(entries)
  entries.group_by {|entry|
    entry.first.strftime('%B %e, %Y')
  }
end
entries_by_week() click to toggle source
# File lib/timesheets/commands/summary.rb, line 69
def entries_by_week
  @entries_by_week ||= entries.group_by {|entry| entry.first.strftime('%U') }.values
end
format_entries(entries) click to toggle source
# File lib/timesheets/commands/summary.rb, line 98
def format_entries(entries)
                          entries_by_day(entries).map {|day, entries|
    entry = [
      entries.first.first.strftime('%A'),
      day,
                                          times_for_entries(entries),
      sprintf('%0.02f', hours_in_entries(entries))
                                  ].flatten

                                  if options[:rate]
                                          entry << sprintf('$%0.02f', options[:rate])
                                          entry << sprintf('$%0.02f', hours_in_entries(entries).to_f * options[:rate].to_f)
                                  end

                                  entry
  }
end
heading() click to toggle source
# File lib/timesheets/commands/summary.rb, line 41
def heading
  the_heading = ['Weekday', 'Date', 'Time', 'Hour(s)']

                          the_heading += ['Hourly Rate', 'Total'] if options[:rate]

                          the_heading
end
hours_in_entries(the_entries) click to toggle source
# File lib/timesheets/commands/summary.rb, line 65
def hours_in_entries(the_entries)
  the_entries.map {|entry| hours_in_entry(entry) }.reduce(:+)
end
rows_for_entries(entries) click to toggle source
# File lib/timesheets/commands/summary.rb, line 34
def rows_for_entries(entries)
  format_entries(entries) + [
    :separator,
    total_row(entries)
  ]
end
summary_table() click to toggle source
# File lib/timesheets/commands/summary.rb, line 22
def summary_table
  table_renderer {|t|
    entries_by_week.each_with_index {|entries, index|
      rows_for_entries(entries).each {|row| t << row }
                                          t << :separator unless index == entries_by_week.length - 1
                                  }
    t << total_row(entries) unless options['week-of']

    (heading.length - 1).times {|i| t.align_column(i, :right) }
  }
end
tableClass() click to toggle source
# File lib/timesheets/commands/summary.rb, line 10
def tableClass
                          {
                                  'html'  => Timesheets::HTMLTable,
                                  'csv'   => Timesheets::CSVTable,
                                  'ascii' => Terminal::Table
                          }[options[:format]] || Terminal::Table
end
table_renderer(&block) click to toggle source
# File lib/timesheets/commands/summary.rb, line 18
def table_renderer(&block)
  tableClass.new({ headings: heading, template: options[:template] }, &block)
end
times_for_entries(entries) click to toggle source
# File lib/timesheets/commands/summary.rb, line 94
def times_for_entries(entries)
                          entries.map {|entry| times_in_entry(entry) }.join(', ')
end
times_in_entry(entry) click to toggle source
# File lib/timesheets/commands/summary.rb, line 90
def times_in_entry(entry)
  entry.map {|time| time.strftime('%l:%M%p') }.join(' - ')
end
total_row(entries) click to toggle source
# File lib/timesheets/commands/summary.rb, line 49
def total_row(entries)
                          padN = heading.length - 2
                          padN -= 2 if options[:rate]
  the_row = padN.times.map { '' } + ['Total:', sprintf('%0.02f', hours_in_entries(entries))]

                          if options[:rate]
                                  the_row += ['', "$#{comma_numbers(hours_in_entries(entries).to_f * options[:rate].to_f)}"]
                          end

                          the_row
end