Class: TempestTime::Models::Timer
- Inherits:
-
Object
- Object
- TempestTime::Models::Timer
- 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
-
#issue ⇒ Object
readonly
Returns the value of attribute issue.
Class Method Summary collapse
Instance Method Summary collapse
- #delete ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(issue) ⇒ Timer
constructor
A new instance of Timer.
- #pause ⇒ Object
- #running? ⇒ Boolean
- #runtime ⇒ Object
- #start ⇒ Object
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
#issue ⇒ Object (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_timers ⇒ Object
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
#delete ⇒ Object
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
60 61 62 |
# File 'lib/tempest_time/models/timer.rb', line 60 def exists? log_files.any? end |
#pause ⇒ Object
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
56 57 58 |
# File 'lib/tempest_time/models/timer.rb', line 56 def running? log_files.any? { |log| log_running?(log) } end |
#runtime ⇒ Object
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 |
#start ⇒ Object
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 |