Class: Vedeu::Logging::Timer Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Measure the duration. Used for debugging. The configuration option ‘debug’ must be set to true to enable this functionality.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = '') ⇒ Vedeu::Logging::Timer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Vedeu::Logging::Timer.

Parameters:

  • message (String) (defaults to: '')


32
33
34
35
# File 'lib/vedeu/logging/timer.rb', line 32

def initialize(message = '')
  @message = message
  @started = Vedeu.clock_time
end

Instance Attribute Details

#messageString (readonly, protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


70
71
72
# File 'lib/vedeu/logging/timer.rb', line 70

def message
  @message
end

#startedTime (readonly, protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Time)


66
67
68
# File 'lib/vedeu/logging/timer.rb', line 66

def started
  @started
end

Class Method Details

.timer(message = '', &block) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

  • message (String) (defaults to: '')
  • block (Proc)

Raises:



22
23
24
# File 'lib/vedeu/logging/timer.rb', line 22

def timer(message = '', &block)
  new(message).measure(&block)
end

Instance Method Details

#elapsedFloat (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the elapsed time in milliseconds with 3 decimal places.

Returns:

  • (Float)


78
79
80
# File 'lib/vedeu/logging/timer.rb', line 78

def elapsed
  ((Vedeu.clock_time - started) * 1000).round(3)
end

#measure(&block) ⇒ void|NilClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Write an entry to the log file stating how long a section of code took in milliseconds when debugging is enabled and a block was Useful for debugging performance.

Parameters:

  • block (Proc)

Returns:

  • (void|NilClass)

    The return value of the executed block if given, or nil if debugging is disabled.

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vedeu/logging/timer.rb', line 45

def measure(&block)
  raise Vedeu::Error::RequiresBlock unless block_given?

  if Vedeu.config.debug?
    work = yield

    Vedeu.log(type:    :timer,
              message: "#{message} took #{elapsed}ms.")

    work

  else
    yield

  end
end