Class: TempestTime::Models::Timer

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

Constant Summary collapse

TEMP_DIR =
Dir.home + '/.tempest/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.



26
27
28
29
# File 'lib/tempest_time/models/timer.rb', line 26

def initialize(issue)
  ensure_tmp_dir
  @issue = issue
end

Instance Attribute Details

#issueObject (readonly)

Returns the value of attribute issue.



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

def issue
  @issue
end

Class Method Details

.all_timersObject



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

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

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

Instance Method Details

#deleteObject



42
43
44
45
# File 'lib/tempest_time/models/timer.rb', line 42

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

#exists?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/tempest_time/models/timer.rb', line 60

def exists?
  log_files.any?
end

#pauseObject



36
37
38
39
40
# File 'lib/tempest_time/models/timer.rb', line 36

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

#running?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/tempest_time/models/timer.rb', line 56

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

#runtimeObject



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

def runtime
  @runtime ||= 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.reduce(:+)
end

#startObject



31
32
33
34
# File 'lib/tempest_time/models/timer.rb', line 31

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