Class: Wlog::TimelogHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/wlog/domain/timelog_helper.rb

Overview

Author:

  • Simon Symeonidis

Constant Summary collapse

Times =
{
  'h' => 60 * 60,
  's' => 1,
  'm' => 60,
  'd' => 8 * 60 * 60, # We assume logging a day = 8 hours
  'w' => 8 * 60 * 60 * 7
}
TimesRev =

Reverse format for times

Hash[Times.to_a.collect{|arr| arr.reverse}]
TimesArr =

For a format to output a given int to time (1221 => 20m 21s)

Times.to_a.sort{|x1,x2| x2[1] <=> x1[1]}.collect{|e| e[1]}

Class Method Summary collapse

Class Method Details

.calculate_item(time_item) ⇒ Object

Calculate one time entry



31
32
33
34
# File 'lib/wlog/domain/timelog_helper.rb', line 31

def self.calculate_item(time_item)
  magnitude = Times[time_item[-1]]
  time_item.to_i * magnitude
end

.calculate_time(time_arr) ⇒ Object

Calculate time interface (get an array of time entries, and calculate)



26
27
28
# File 'lib/wlog/domain/timelog_helper.rb', line 26

def self.calculate_time(time_arr)
  time_arr.inject(0){|sum,el| sum += self.calculate_item(el)}
end

.count_occurences(time, step) ⇒ Object



36
37
# File 'lib/wlog/domain/timelog_helper.rb', line 36

def self.count_occurences(time, step)
end

.parse(str) ⇒ Object

Parse a timelog string



7
8
9
10
# File 'lib/wlog/domain/timelog_helper.rb', line 7

def self.parse(str)
  loggings = str.scan(/\d+[A-Za-z]/)
  self.calculate_time(loggings)
end

.time_to_s(time) ⇒ Object

Returns in nice format (2d 1h 20m).

Parameters:

  • time

    is time in seconds

Returns:

  • in nice format (2d 1h 20m)



14
15
16
17
18
19
20
21
22
# File 'lib/wlog/domain/timelog_helper.rb', line 14

def self.time_to_s(time)
  str = ""
  TimesArr.each do |interval|
    rem = time % interval
    occ = (time - rem) / interval
    str.concat("#{occ}#{TimesRev[interval]} ") if occ > 0
    time = rem
  end
str end