Class: TempestTime::Models::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/tempest_time/models/timer.rb

Constant Summary collapse

TEMP_DIR =
'/tmp/timer/logs'
FILE_EXT =
'.timer'
PREFIX_SEPARATOR =
'___'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(issue) ⇒ Timer

Returns a new instance of Timer.



24
25
26
27
# File 'lib/tempest_time/models/timer.rb', line 24

def initialize(issue)
  ensure_tmp_dir
  @issue = issue
end

Instance Attribute Details

#issueObject (readonly)

Returns the value of attribute issue.



22
23
24
# File 'lib/tempest_time/models/timer.rb', line 22

def issue
  @issue
end

Class Method Details

.all_timersObject



13
14
15
16
17
18
19
# File 'lib/tempest_time/models/timer.rb', line 13

def all_timers
  issues = Dir.glob("#{TEMP_DIR}/*#{FILE_EXT}").map do |file|
    file.delete(TEMP_DIR).split(PREFIX_SEPARATOR).first
  end

  issues.uniq.sort.map { |issue| new(issue) }
end

Instance Method Details

#deleteObject



40
41
42
43
# File 'lib/tempest_time/models/timer.rb', line 40

def delete
  return false unless exists?
  log_files.each { |log| File.unlink log }
end

#exists?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/tempest_time/models/timer.rb', line 58

def exists?
  log_files.any?
end

#pauseObject



34
35
36
37
38
# File 'lib/tempest_time/models/timer.rb', line 34

def pause
  log_files.each do |log|
    FileUtils.touch(log) if log_running?(log)
  end
end

#running?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/tempest_time/models/timer.rb', line 54

def running?
  log_files.any? { |log| log_running?(log) }
end

#runtimeObject



45
46
47
48
49
50
51
52
# File 'lib/tempest_time/models/timer.rb', line 45

def runtime
  @logs ||= log_files.each_with_object([]) do |log, array|
    start_time = File.birthtime(log)
    end_time = log_running?(log) ? Time.now : File.mtime(log)

    array << (end_time - start_time)
  end.sum
end

#startObject



29
30
31
32
# File 'lib/tempest_time/models/timer.rb', line 29

def start
  return false if running?
  Tempfile.create([log_file_prefix, FILE_EXT], TEMP_DIR)
end