Class: Toggl::Worktime::Merger

Inherits:
Object
  • Object
show all
Defined in:
lib/toggl/worktime/merger.rb

Overview

Time-entries merger

Constant Summary collapse

ONE_MINUTE_SECONDS =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time_entries, config) ⇒ Merger

Returns a new instance of Merger.



13
14
15
16
17
18
19
20
21
# File 'lib/toggl/worktime/merger.rb', line 13

def initialize(time_entries, config)
  @time_entries = time_entries
  @config = config
  @current_start = nil
  @current_stop = nil
  @continuing = true
  @last_stop = nil
  @total_time = 0
end

Instance Attribute Details

#total_timeObject (readonly)

Returns the value of attribute total_time.



9
10
11
# File 'lib/toggl/worktime/merger.rb', line 9

def total_time
  @total_time
end

Instance Method Details

#continuing(start) ⇒ Object



67
68
69
70
71
72
# File 'lib/toggl/worktime/merger.rb', line 67

def continuing(start)
  return true if @current_stop.nil?

  interval = (start - @current_stop) / ONE_MINUTE_SECONDS
  @continuing = interval < @config.working_interval_min
end

#count_total_timeObject



40
41
42
43
44
# File 'lib/toggl/worktime/merger.rb', line 40

def count_total_time
  return if @current_start.nil? || @current_stop.nil?

  @total_time += @current_stop - @current_start
end

#mergeObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/toggl/worktime/merger.rb', line 23

def merge
  work_time = []
  time_entries_each do |start, stop|
    if continuing(start)
      @current_stop = stop
      next
    end
    work_time << [@current_start, @current_stop]
    count_total_time
    @current_start = start
    @current_stop = stop
  end
  work_time << [@current_start, @last_stop]
  count_total_time
  work_time
end

#parse_date(date, zone_offset) ⇒ Object



61
62
63
64
65
# File 'lib/toggl/worktime/merger.rb', line 61

def parse_date(date, zone_offset)
  return nil if date.nil?

  ::Time.parse(date).getlocal(zone_offset)
end

#time_entries_eachObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/toggl/worktime/merger.rb', line 46

def time_entries_each
  zone_offset = Toggl::Worktime::Time.zone_offset(@config.timezone)
  @time_entries.each do |te|
    start = parse_date(te['start'], zone_offset)
    stop = parse_date(te['stop'], zone_offset)
    @last_stop = stop
    @current_start = start if @current_start.nil?
    @current_stop = stop if @current_stop.nil?
    if start.nil? || stop.nil?
      warn 'start or stop time is nil: total time may be incomplete'
    end
    yield [start, stop]
  end
end