Class: Timestream::EmployeeActivityReport

Inherits:
Object
  • Object
show all
Defined in:
lib/timestream/employee_activity_report.rb

Defined Under Namespace

Classes: Timestream

Constant Summary collapse

ACTIVITY_COLUMN =
2
DATE_COLUMN =
1
DATE_ROW =
2
DAY_COLUMN_SPAN =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ EmployeeActivityReport

Returns a new instance of EmployeeActivityReport.



34
35
36
# File 'lib/timestream/employee_activity_report.rb', line 34

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



38
39
40
# File 'lib/timestream/employee_activity_report.rb', line 38

def path
  @path
end

Instance Method Details

#date_column(date) ⇒ Object



14
15
16
17
# File 'lib/timestream/employee_activity_report.rb', line 14

def date_column(date)
  slot = date_slot(date)
  slot_column(slot)
end

#date_slot(date) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/timestream/employee_activity_report.rb', line 19

def date_slot(date)
  day_delta = (date - saturday).to_i
  
  case day_delta
    # Saturday and Sunday map to weekend
    when 0..1
      0
    # All others map to their own day
    when 2..6
      day_delta - 1
    else
      raise ArgumentError, "#{date} is not in the same week as #{saturday}"
  end
end

#row(index) ⇒ Object



40
41
42
# File 'lib/timestream/employee_activity_report.rb', line 40

def row(index)
  worksheet.row(index)
end

#saturdayObject



44
45
46
# File 'lib/timestream/employee_activity_report.rb', line 44

def saturday
  @saturday ||= worksheet.row(DATE_ROW)[DATE_COLUMN]
end

#slot_column(slot) ⇒ Object



48
49
50
# File 'lib/timestream/employee_activity_report.rb', line 48

def slot_column(slot)
  9 + (slot * DAY_COLUMN_SPAN)
end

#spreadsheetObject



52
53
54
# File 'lib/timestream/employee_activity_report.rb', line 52

def spreadsheet
  @spreadsheet ||= Spreadsheet.open path
end

#timestream_by_project_numberObject



90
91
92
93
94
95
96
# File 'lib/timestream/employee_activity_report.rb', line 90

def timestream_by_project_number
  if @timestream_by_project_number.nil?
    parse
  end
  
  @timestream_by_project_number
end

#to_text_table(date_format = :day_of_week) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/timestream/employee_activity_report.rb', line 98

def to_text_table(date_format=:day_of_week)
  employee_activity_report = self
  
  table {
    self.headings = ['Project Number']
    
    case date_format
      when :day_of_week
        date_format = "%A"
      when :erp
        date_format = "%a %m/%d"
    end
     
    Timestream::SLOT_NAMES.each_index do |sunday_offset|
      # XXX assign weekend activity to sunday to simplify math
      sunday = employee_activity_report.saturday + 1
      slot_date = sunday + sunday_offset
       
      self.headings << slot_date.strftime(date_format)
    end
    
    employee_activity_report.timestream_by_project_number.each do |project_number, timestream|
      row = [project_number]
      timestream.each do |daily_total|
        row << "%02.02f" % daily_total
      end
      
      add_row row
    end 
  }
end

#updateFromHamster(hamster) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/timestream/employee_activity_report.rb', line 130

def updateFromHamster(hamster)
  # Categories in Hamster are mapped to Activities in EAR
  
  hamster.timestream_by_category.each do |category, timestream|
    # XXX should this been creating EAR Timestreams instead of updating rows directly?
    (12 .. 36).each do |row_index|
      row = self.row(row_index)
      activity = row[ACTIVITY_COLUMN]
      
      # XXX Category is assumed to be a substring of Activity since Activity is very wordy.
      if activity and activity.include? category
        timestream.each do |date, hours|
          column = self.date_column(date)
          row[column] ||= 0
          row[column] += hours
        end
      end
    end
  end
end

#updateFromHamster!(hamster) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/timestream/employee_activity_report.rb', line 151

def updateFromHamster!(hamster)
  updateFromHamster(hamster)
  
  # spreadsheet library recommends not writing back to same file, write to
  # temporary file and then rename that file to original.
  temporary_path = "#{path}.timestream"
  spreadsheet.write(temporary_path)
  File.rename(temporary_path, path)
end

#worksheetObject



161
162
163
# File 'lib/timestream/employee_activity_report.rb', line 161

def worksheet
  @worksheet ||= spreadsheet.worksheet "EAR - Current Week"
end