Module: Jacker

Defined in:
lib/jacker.rb

Class Method Summary collapse

Class Method Details

.current(options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/jacker.rb', line 36

def self.current(options={})
  results = self.with_db do |db|
    db.execute("SELECT what, start FROM entries WHERE stop IS NULL")
  end

  return nil if results.empty?

  what, start = results[0][0], Time.parse(results[0][1])
  options[:elapsed] ? "#{what} (#{elapsed(start, Time.now)})" : what
end

.destroyObject



13
14
15
# File 'lib/jacker.rb', line 13

def self.destroy
  FileUtils.rm_f(dbfile)
end

.elapsed(start, stop) ⇒ Object

rounds up to nearest 15 minute increment



48
49
50
51
52
53
# File 'lib/jacker.rb', line 48

def self.elapsed(start, stop)
  seconds = ((stop - start) / 60).to_i
  hours = seconds / 60
  minutes = (seconds % 60 + 14) / 15 * 15
  sprintf("%d:%02d", hours, minutes)
end

.reportObject



55
56
57
58
59
60
61
62
63
# File 'lib/jacker.rb', line 55

def self.report
  self.with_db do |db|
    db.execute("SELECT * FROM entries WHERE stop IS NOT NULL").each do |entry|
      start = Time.parse(entry[1])
      stop  = Time.parse(entry[2])
      yield(entry[0], start, stop)
    end
  end
end

.running?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'lib/jacker.rb', line 29

def self.running?
  results = self.with_db do |db|
    db.execute("SELECT COUNT(what) FROM entries WHERE stop IS NULL")
  end
  results.size > 0 && results[0][0].to_i > 0
end

.setupObject



6
7
8
9
10
11
# File 'lib/jacker.rb', line 6

def self.setup
  return if File.exist?(dbfile)
  db = SQLite3::Database.new(dbfile)
  db.execute("CREATE TABLE IF NOT EXISTS entries(what STRING, start TIMESTAMP, stop TIMESTAMP)")
  db.close
end

.start(what) ⇒ Object



17
18
19
20
21
# File 'lib/jacker.rb', line 17

def self.start(what)
  self.with_db do |db|
    db.execute("INSERT INTO entries(what, start) VALUES('#{what}', '#{Time.now.to_s}')")
  end
end

.statusObject



65
66
67
68
69
70
71
# File 'lib/jacker.rb', line 65

def self.status
  if running?
    "jacking: #{current(:elapsed => true)}"
  else
    "not jacking"
  end
end

.stopObject



23
24
25
26
27
# File 'lib/jacker.rb', line 23

def self.stop
  self.with_db do |db|
    db.execute("UPDATE entries SET stop='#{Time.now.to_s}' WHERE stop IS NULL")
  end
end