class Perforator::Meter

Constants

NoExpectedTimeError
NotCallableCallbackError
NotFixnumExpectedTimeError

Attributes

expected_time[R]
finish_time[R]
logger[R]
name[R]
negative_callback[R]
positive_callback[R]
process_logger?[R]
process_puts[R]
process_puts?[R]
spent_time[R]
start_time[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/perforator.rb, line 12
def initialize(options = {})
  @name              = options.fetch(:name,              nil)
  @logger            = options.fetch(:logger,            nil)
  @process_puts      = options.fetch(:puts,            false)
  @expected_time     = options.fetch(:expected_time,     nil)
  @positive_callback = options.fetch(:positive_callback, nil)
  @negative_callback = options.fetch(:negative_callback, nil)

  raise NoExpectedTimeError if callbacks_without_expected_time?
  raise NotFixnumExpectedTimeError unless expected_time_valid?
  raise NotCallableCallbackError unless callable_callbacks?
end

Public Instance Methods

call() { |self| ... } click to toggle source
# File lib/perforator.rb, line 25
def call(&block)
  start

  yield(self)

  finish
end
log!(content) click to toggle source
# File lib/perforator.rb, line 33
def log!(content)
  log_items << content
end

Private Instance Methods

callable_callbacks?() click to toggle source
# File lib/perforator.rb, line 90
def callable_callbacks?
  callbacks = [positive_callback, negative_callback].compact

  return true if callbacks.empty?

  callbacks.map { |c| c.respond_to?(:call) }.uniq == [true]
end
callbacks_without_expected_time?() click to toggle source
# File lib/perforator.rb, line 86
def callbacks_without_expected_time?
  (positive_callback || negative_callback) && !expected_time
end
execute_callbacks!() click to toggle source
# File lib/perforator.rb, line 67
def execute_callbacks!
  return unless expected_time

  if spent_time < expected_time && positive_callback
    log! "Spent time less than exepcted. Executing: #{positive_callback.inspect}"
    positive_callback.call
  elsif spent_time > expected_time && negative_callback
    log! "Spent time more than exepcted. Executing: #{negative_callback.inspect}"
    negative_callback.call
  end
end
expected_time_valid?() click to toggle source
# File lib/perforator.rb, line 98
def expected_time_valid?
  expected_time.nil? || expected_time.is_a?(Fixnum)
end
finish() click to toggle source
# File lib/perforator.rb, line 56
def finish
  @finish_time = Time.now
  @spent_time = finish_time - start_time

  log! "Finish: #{finish_time}"
  log! "Spent: #{spent_time}"

  execute_callbacks!
  release_logs!
end
log_items() click to toggle source
# File lib/perforator.rb, line 44
def log_items
  @log_items ||= []
end
release_logs!() click to toggle source
# File lib/perforator.rb, line 79
def release_logs!
  log_items.each do |log_item|
    puts(log_item)        if process_puts?
    logger.info(log_item) if process_logger?
  end
end
start() click to toggle source
# File lib/perforator.rb, line 48
def start
  log! "=======> #{name}"

  @start_time = Time.now

  log! "Start: #{start_time}"
end