Class: Redpomo::FuzzyConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/redpomo/fuzzy_converter.rb

Class Method Summary collapse

Class Method Details

.convert(entries) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/redpomo/fuzzy_converter.rb', line 7

def self.convert(entries)
  past_entry = nil
  consecutive_entries = []

  entries << nil

  result = []

  entries.each do |entry|
    if past_entry.present?
      if entry.present? && entry.same_date?(past_entry) && entry.same_text?(past_entry)
        consecutive_entries << entry
      else
        result << self.fuzzy_entry(consecutive_entries, entry)
        past_entry = entry
        consecutive_entries = [ entry ]
      end
    else
      past_entry = entry
      consecutive_entries = [ entry ]
    end
  end

  result
end

.fuzzy_entry(entries, next_entry) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/redpomo/fuzzy_converter.rb', line 33

def self.fuzzy_entry(entries, next_entry)
  first_entry = entries.first
  last_entry = entries.last
  total_duration = first_entry.time - last_entry.end_time
  duration_till_next_entry = next_entry.time - last_entry.end_time if next_entry.present?

  entry = if next_entry.nil? || duration_till_next_entry > 1.5 * 3600.0
    duration = last_entry.end_time - first_entry.time
    duration += 1.5 * 3600.0 if next_entry.present? && next_entry.same_date?(last_entry)
    Entry.new(first_entry.text, first_entry.datetime, duration)
  else
    duration = next_entry.time - first_entry.time
    entry = Entry.new(first_entry.text, first_entry.datetime, duration)
  end
  entry
end