Class: ESM::Command::Base::Timers

Inherits:
Hash
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper
Defined in:
lib/esm/command/base/timers.rb

Instance Method Summary collapse

Constructor Details

#initialize(command_name) ⇒ Timers

Returns a new instance of Timers.



9
10
11
12
# File 'lib/esm/command/base/timers.rb', line 9

def initialize(command_name)
  @command_name = command_name
  super
end

Instance Method Details

#humanized_totalObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/esm/command/base/timers.rb', line 42

def humanized_total
  time_elapsed = values.sum(&:time_elapsed)

  # Milliseconds
  if (milliseconds = (time_elapsed * 1_000).round) && milliseconds <= 1_000
    return "#{number_with_delimiter(milliseconds)} #{"millisecond".pluralize(milliseconds)}"
  end

  # Microseconds
  if (microseconds = (time_elapsed * 1_000_000).round) && microseconds <= 1_000_000
    return "#{number_with_delimiter(microseconds)} #{"microsecond".pluralize(microseconds)}"
  end

  # Seconds and above
  start_time = values.map(&:started_at).min
  ESM::Time.distance_of_time_in_words(start_time + time_elapsed.seconds, from_time: start_time)
end

#reset_all!Object



14
15
16
# File 'lib/esm/command/base/timers.rb', line 14

def reset_all!
  values.each(&:reset!)
end

#stop_all!Object



18
19
20
# File 'lib/esm/command/base/timers.rb', line 18

def stop_all!
  values.each(&:stop!)
end

#time!(timer_name, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/esm/command/base/timers.rb', line 26

def time!(timer_name, &block)
  timer = create_timer(timer_name)

  timer.start!
  yield
  timer.stop!

  info!(
    timer: timer_name,
    command: @command_name,
    time_elapsed: "#{timer.time_elapsed * 1000} ms"
  )

  nil
end

#to_hObject



22
23
24
# File 'lib/esm/command/base/timers.rb', line 22

def to_h
  transform_values(&:to_h)
end