Class: Timetrap::Formatters::Text

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/timetrap/formatters/text.rb

Constant Summary collapse

LONGEST_NOTE_LENGTH =
50

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#format_date, #format_date_if_new, #format_seconds, #format_time, #format_total, #same_day?, #selected_entries, #sheet_name_from_string

Constructor Details

#initialize(entries) ⇒ Text

Returns a new instance of Text.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/timetrap/formatters/text.rb', line 8

def initialize entries
  @entries = entries
  self.output = ''
  sheets = entries.inject({}) do |h, e|
    h[e.sheet] ||= []
    h[e.sheet] << e
    h
  end

  (sheet_names = sheets.keys.sort).each do |sheet|

    self.output <<  "Timesheet: #{sheet}\n"
    id_heading = Timetrap::CLI.args['-v'] ? "Id#{' ' * (max_id_length - 3)}" : "  "
    self.output <<  "#{id_heading}  Day                Start      End        Duration   Notes\n"
    last_start = nil
    from_current_day = []

    sheets[sheet].each_with_index do |e, i|
      from_current_day << e
      self.output <<  "%-#{max_id_length + 1}s%16s%11s -%9s%10s    %s\n" % [
        (Timetrap::CLI.args['-v'] ? e.id : ''),
        format_date_if_new(e.start, last_start),
        format_time(e.start),
        format_time(e.end),
        format_duration(e.duration),
        format_note(e.note)
      ]

      nxt = sheets[sheet].to_a[i+1]
      if nxt == nil or !same_day?(e.start, nxt.start)
        self.output <<  "%#{49 + max_id_length}s\n" % format_total(from_current_day)
        from_current_day = []
      else
      end
      last_start = e.start
    end
    self.output <<  "#{' ' * (max_id_length + 1)}%s\n" % ('-'*(52+longest_note))
    self.output <<  "#{' ' * (max_id_length + 1)}Total%43s\n" % format_total(sheets[sheet])
    self.output <<  "\n" unless sheet == sheet_names.last
  end
  if sheets.size > 1
    self.output <<  "%s\n" % ('-'*(4+52+longest_note))
    self.output <<  "Grand Total%41s\n" % format_total(sheets.values.flatten)
  end


end

Instance Attribute Details

#outputObject

Returns the value of attribute output.



5
6
7
# File 'lib/timetrap/formatters/text.rb', line 5

def output
  @output
end