Class: Timer::Timer

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title = nil) ⇒ Timer

Returns a new instance of Timer.



54
55
56
57
# File 'lib/prick/local/timer.rb', line 54

def initialize(title = nil)
  @title = title
  start
end

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



41
42
43
# File 'lib/prick/local/timer.rb', line 41

def title
  @title
end

Class Method Details

.factorObject



70
# File 'lib/prick/local/timer.rb', line 70

def self.factor() { s: 1, ms: 1000 }[::Timer.unit] end

.format(time) ⇒ Object



72
73
74
# File 'lib/prick/local/timer.rb', line 72

def self.format(time)
  time ? sprintf("%.#{::Timer.scale}f#{::Timer.unit}", Timer.factor * time) : time.inspect
end

.on!Object



45
# File 'lib/prick/local/timer.rb', line 45

def self.on!() ::Timer.on! end

.on?Boolean

Returns:

  • (Boolean)


46
# File 'lib/prick/local/timer.rb', line 46

def self.on?() ::Timer.on? end

Instance Method Details

#emit(title = self.title) ⇒ Object



65
66
67
68
# File 'lib/prick/local/timer.rb', line 65

def emit(title = self.title)
  stop
  ::Timer.file.puts to_s(title) if ::Timer.on?
end

#fileObject



43
# File 'lib/prick/local/timer.rb', line 43

def file() ::Timer.file end

#scaleObject



51
# File 'lib/prick/local/timer.rb', line 51

def scale() ::Timer.scale end

#scale=(scale) ⇒ Object



52
# File 'lib/prick/local/timer.rb', line 52

def scale=(scale) ::Timer.scale scale end

#start(title = self.title) ⇒ Object



59
# File 'lib/prick/local/timer.rb', line 59

def start(title = self.title) @t0 = Time.now; @title = title end

#stopObject



60
# File 'lib/prick/local/timer.rb', line 60

def stop() @t1 ||= Time.now end

#timeObject



61
# File 'lib/prick/local/timer.rb', line 61

def time() @t1 - @t0 end

#to_s(title = self.title) ⇒ Object



63
# File 'lib/prick/local/timer.rb', line 63

def to_s(title = self.title) "#{title}: #{Timer.format(time)}" end

#unitObject



48
# File 'lib/prick/local/timer.rb', line 48

def unit() ::Timer.unit end

#unit=(unit) ⇒ Object



49
# File 'lib/prick/local/timer.rb', line 49

def unit=(unit) ::Timer.unit = unit end