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

.previous_entryObject



19
20
21
22
23
24
25
# File 'lib/ptimelog/timelog.rb', line 19

def previous_entry
  lines = timelog_txt.readlines.last(2)
  last_line = lines.map(&:chomp).delete_if(&:empty?).last
  last_entry = instance.tokenize(last_line)

  Entry.from_timelog(last_entry)
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



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

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

#parse(data) ⇒ Object



44
45
46
47
48
49
# File 'lib/ptimelog/timelog.rb', line 44

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

#previous_entryObject



36
37
38
# File 'lib/ptimelog/timelog.rb', line 36

def previous_entry
  self.class.previous_entry
end

#readObject



40
41
42
# File 'lib/ptimelog/timelog.rb', line 40

def read
  timelog_txt.read
end

#timelog_txtObject



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

def timelog_txt
  self.class.timelog_txt
end

#tokenize(line) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/ptimelog/timelog.rb', line 51

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