Sha256: a04458e950ab319e5837d4bbe190a1513037ea87d5026d9022fba39bd9641158

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require 'singleton'

module Loba
  module Internal
    # Internal class for tracking time stamps; should not be used directly
    # @!attribute [r] timewas
    #   Previous timestamped Time value
    # @!attribute [r] timenum
    #   Count of timestamping occurances so far
    class TimeKeeper
      include Singleton
      attr_reader :timewas, :timenum

      def initialize
        reset!
      end

      # Increments timestamping, including attributes `timenum` and `timewas`
      # @return [Hash] timestamp details
      #   * :number => [Integer] incremented count of pings so far (attribute `timenum`)
      #   * :now => [Time] current date and time
      #   * :change => [Float] difference in seconds from any previous ping or reset
      def ping
        @timenum += 1
        now = Time.now
        change = now - @timewas
        @timewas = now

        { number: @timenum, now: now, change: change }
      end

      # Resets timestamping
      # @return [NilClass] nil
      def reset!
        @timewas = Time.now
        @timenum = 0

        nil
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
loba-2.0.0 lib/loba/internal/time_keeper.rb