Class: Timmy::TargetedTimer

Inherits:
Object
  • Object
show all
Defined in:
lib/timmy/targeted_timer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, label: nil) ⇒ TargetedTimer

Returns a new instance of TargetedTimer.



56
57
58
59
60
# File 'lib/timmy/targeted_timer.rb', line 56

def initialize(definition, label: nil)
  @definition = definition
  @start_time = MasterTimer.get
  @label = label
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



54
55
56
# File 'lib/timmy/targeted_timer.rb', line 54

def definition
  @definition
end

#durationObject (readonly)

Returns the value of attribute duration.



54
55
56
# File 'lib/timmy/targeted_timer.rb', line 54

def duration
  @duration
end

#labelObject (readonly)

Returns the value of attribute label.



54
55
56
# File 'lib/timmy/targeted_timer.rb', line 54

def label
  @label
end

Class Method Details

.put_stopped_profilesObject



45
46
47
48
49
50
51
# File 'lib/timmy/targeted_timer.rb', line 45

def put_stopped_profiles
  puts ""
  puts "Slowest targeted timers:"
  TargetedTimer.stopped.sort_by { |timer| -timer.duration }[0..9].each do |timer|
    timer.put_profile
  end
end

.start_for_line(line) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/timmy/targeted_timer.rb', line 4

def start_for_line(line)
  TargetedTimerDefinition.all.each do |definition|
    if match = line.match(definition.start_regex)
      stop_by_id(definition.id)
      label = match.send(:[], :label) rescue nil
      started.push(self.new(definition, label: label))
    end
  end
end

.startedObject



37
38
39
# File 'lib/timmy/targeted_timer.rb', line 37

def started
  @started ||= []
end

.stop(timer) ⇒ Object



22
23
24
25
26
# File 'lib/timmy/targeted_timer.rb', line 22

def stop(timer)
  timer.stop
  started.delete(timer)
  stopped.push(timer)
end

.stop_allObject



33
34
35
# File 'lib/timmy/targeted_timer.rb', line 33

def stop_all
  started.each { |timer| stop(timer) }
end

.stop_by_id(id) ⇒ Object



28
29
30
31
# File 'lib/timmy/targeted_timer.rb', line 28

def stop_by_id(id)
  matches = started.select { |timer| timer.definition.id == id }
  matches.each { |timer| stop(timer) }
end

.stop_for_line(line) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/timmy/targeted_timer.rb', line 14

def stop_for_line(line)
  started.each do |timer|
    if (stop_regex = timer.definition.stop_regex) && line.match?(stop_regex)
      stop(timer)
    end
  end
end

.stoppedObject



41
42
43
# File 'lib/timmy/targeted_timer.rb', line 41

def stopped
  @stopped ||= []
end

Instance Method Details

#put_profileObject



67
68
69
70
# File 'lib/timmy/targeted_timer.rb', line 67

def put_profile
  duration = Logger.format_duration(@duration)
  puts " - #{duration} - #{formatted_id} - #{@label}"
end

#stopObject



62
63
64
65
# File 'lib/timmy/targeted_timer.rb', line 62

def stop
  put
  @duration = MasterTimer.get - @start_time
end