Sha256: b5a878b7e3d06ce665e758baf50acddbca0590dbea4f5f026ed42e7d8be09958

Contents?: true

Size: 1.84 KB

Versions: 6

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module RubySmart
  module SimpleLogger
    module Extensions
      module Timer
        def timer(action, key = :default, opts = {})
          return if key.nil?

          @timers      ||= {}
          @timers[key] ||= {
            start:   nil,
            stop:    nil,
            total: 0
          }

          case action
          when :start, :restart
            @timers[key][:start]   = Time.now
            @timers[key][:stop]    = nil
            @timers[key][:total] = 0

            true
          when :continue
            @timers[key][:start] = Time.now
            @timers[key][:stop]  = nil

            true
          when :stop
            return false if !@timers[key][:start] || @timers[key][:stop]
            @timers[key][:stop]    = Time.now
            @timers[key][:total] += @timers[key][:stop] - @timers[key][:start]

            true
          when :pause
            return false if !@timers[key][:start] || @timers[key][:stop]

            @timers[key][:total] += Time.now - @timers[key][:start]
            @timers[key][:start]   = nil
            @timers[key][:stop]    = nil

            true
          when :clear
            self.timer(:stop, key)
            current = self.timer(:current, key)
            @timers.delete(key)

            # time_ago_in_words in only available if activesupport & actionview gems are loaded
            if opts[:humanized] && respond_to?(:time_ago_in_words)
              time_ago_in_words(current.to_i.seconds.from_now, include_seconds: true)
            else
              current
            end
          when :current
            current = @timers[key][:total]
            current += Time.now - @timers[key][:start] if @timers[key][:start] && @timers[key][:stop].nil?
            current
          else
            nil
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ruby_smart-simple_logger-1.5.3 lib/ruby_smart/simple_logger/extensions/timer.rb
ruby_smart-simple_logger-1.5.2 lib/ruby_smart/simple_logger/extensions/timer.rb
ruby_smart-simple_logger-1.5.1 lib/ruby_smart/simple_logger/extensions/timer.rb
ruby_smart-simple_logger-1.5.0 lib/ruby_smart/simple_logger/extensions/timer.rb
ruby_smart-simple_logger-1.4.0 lib/ruby_smart/simple_logger/extensions/timer.rb
ruby_smart-simple_logger-1.3.0 lib/ruby_smart/simple_logger/extensions/timer.rb