Sha256: c85c9eff4ff4a5d12f6bbc60bc0c3b90caf766d9fef850b3b2af263bbe79639c

Contents?: true

Size: 635 Bytes

Versions: 3

Compression:

Stored size: 635 Bytes

Contents

require 'forwardable'

module Fortnox
  module API
    class CircularQueue
      extend Forwardable

      def initialize *items
        @queue = [ *items ]
        @@next_index = random_start_index
      end

      # support some general Array methods that fit Queues well
      def_delegators :@queue, :new, :[], :size

      def next
        value = @queue[ @@next_index ]
        if @@next_index == size - 1
          @@next_index = 0
        else
          @@next_index += 1
        end
        return value
      end

      private

        def random_start_index
          Random.rand(@queue.size)
        end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fortnox-api-0.5.2 lib/fortnox/api/circular_queue.rb
fortnox-api-0.5.1 lib/fortnox/api/circular_queue.rb
fortnox-api-0.5.0 lib/fortnox/api/circular_queue.rb