Class: Ptimelog::Timelog

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/ptimelog/timelog.rb

Overview

Load and tokenize the data from gtimelog

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.loadObject



11
12
13
# File 'lib/ptimelog/timelog.rb', line 11

def load
  instance.load
end

.timelog_txtObject



15
16
17
# File 'lib/ptimelog/timelog.rb', line 15

def timelog_txt
  Pathname.new(Configuration.instance[:timelog]).expand_path
end

Instance Method Details

#loadObject



20
21
22
# File 'lib/ptimelog/timelog.rb', line 20

def load
  @load ||= parse(read)
end

#parse(data) ⇒ Object



32
33
34
35
36
37
# File 'lib/ptimelog/timelog.rb', line 32

def parse(data)
  data.split("\n")
      .map { |line| tokenize(line) }
      .group_by { |match| match && match[:date] }
      .to_a
end

#readObject



28
29
30
# File 'lib/ptimelog/timelog.rb', line 28

def read
  timelog_txt.read
end

#timelog_txtObject



24
25
26
# File 'lib/ptimelog/timelog.rb', line 24

def timelog_txt
  self.class.timelog_txt
end

#tokenize(line) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/ptimelog/timelog.rb', line 39

def tokenize(line)
  re_date = /(?<date>\d{4}-\d{2}-\d{2})/
  re_time = /(?<time>\d{2}:\d{2})/
  re_tick = /(?:(?<ticket>.*?): )/
  re_desc = /(?<description>.*?)/
  re_tags = /(?: -- (?<tags>.*)?)/

  regexp = /^#{re_date} #{re_time}: #{re_tick}?#{re_desc}#{re_tags}?$/
  line.match(regexp)
end