Class: Timer

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimer

Returns a new instance of Timer.


8
9
10
# File 'lib/base/timer.rb', line 8

def initialize
  @start_time = Time.now
end

Instance Attribute Details

#start_timeObject

Returns the value of attribute start_time.


6
7
8
# File 'lib/base/timer.rb', line 6

def start_time
  @start_time
end

Class Method Details

.elapsed_exceeds?(name, duration_seconds) ⇒ Boolean

Returns:

  • (Boolean)

21
22
23
24
25
# File 'lib/base/timer.rb', line 21

def self.elapsed_exceeds?(name, duration_seconds)
  return true if Timer.get_elapsed(name).nil? || Timer.get_elapsed(name) > duration_seconds

  false
end

.get_elapsed(name) ⇒ Object


27
28
29
30
31
32
# File 'lib/base/timer.rb', line 27

def self.get_elapsed(name)
  timestamp = get_timestamp(name)
  return Time.now - timestamp unless timestamp.nil?

  nil
end

.get_timestamp(name) ⇒ Object


34
35
36
37
38
39
40
41
# File 'lib/base/timer.rb', line 34

def self.get_timestamp(name)
  dir = Rake.application.original_dir
  if File.exist?("#{DEV[:dev_root]}/log/#{name}.timestamp")
    return Time.parse(File.read("#{DEV[:dev_root]}/log/#{name}.timestamp").strip)
  end

  nil
end

.set_timestamp(name) ⇒ Object


43
44
45
46
# File 'lib/base/timer.rb', line 43

def self.set_timestamp(name)
  Dir.mkdir("#{DEV_TASKS[:dev_root]}/log") unless Dir.exist?("#{DEV_TASKS[:dev_root]}/log")
  File.open("#{DEV_TASKS[:dev_root]}/log/#{name}.timestamp", "w") { |f| f.puts(Time.now.to_s) }
end

Instance Method Details

#elapsedObject

in seconds


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

def elapsed
  Time.now - @start_time
end

#elapsed_strObject


17
18
19
# File 'lib/base/timer.rb', line 17

def elapsed_str
  elapsed_str = "[#{"%.0f" % elapsed}s]"
end