Sha256: b6d67946a746ad0332e0c072e1cef08f0147af2bc7264d5950a80c2899f564d5
Contents?: true
Size: 1.39 KB
Versions: 6
Compression:
Stored size: 1.39 KB
Contents
require 'parallel' class ResponseCollection include Enumerable attr_accessor :paged_objects attr_accessor :total attr_accessor :per_page attr_accessor :load_more_call DEFAULT_NO_THREADS = 4 # seed_page, total, per_page, load_more_call def initialize(options = {}) @paged_objects = {0 => options[:seed_page]} @total = options[:total].to_i @per_page = options[:per_page].to_i @pages = @per_page > 0 ? (@total.to_f / @per_page.to_f).ceil : 0 @load_more_call = options[:load_more_call] @no_threads = options[:no_threads] || DEFAULT_NO_THREADS end def each Parallel.each(0..@pages, in_threads: @no_threads) do |current_page| objects = if @paged_objects[current_page].present? @paged_objects[current_page] else load_more_call.call(current_page) end objects = [objects].flatten.compact @paged_objects[current_page] = objects if objects.present? objects.each do |obj| yield obj end end end def last last_page_no = @pages - 1 if load_more_call and (last_page = @paged_objects[last_page_no]).nil? last_page = @paged_objects[last_page_no] = load_more_call.call(last_page_no) end last_page.last end def loaded_results @paged_objects.values.flatten end alias size total alias length total def empty? total <= 0 end end
Version data entries
6 entries across 6 versions & 1 rubygems