Sha256: 1f40b67871a971e8d6aea4af0c4069f1987f78e08828cf50109e93c38082e54b

Contents?: true

Size: 617 Bytes

Versions: 1

Compression:

Stored size: 617 Bytes

Contents

# frozen_string_literal: true

module ActiveRecordDataLoader
  module ActiveRecord
    class List
      def self.for(enumerable, strategy: :random)
        if strategy == :cycle
          Cycle.new(enumerable)
        else
          Random.new(enumerable)
        end
      end

      class Random
        def initialize(enumerable)
          @list = enumerable
        end

        def next
          @list.sample
        end
      end

      class Cycle
        def initialize(enumerable)
          @list = enumerable.cycle
        end

        def next
          @list.next
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record_data_loader-1.3.0 lib/active_record_data_loader/active_record/list.rb