Sha256: 07dd8d8b1cc2dad796af933790b9d2c13900bfda66f82700e15aab65b90f2804
Contents?: true
Size: 1.27 KB
Versions: 2
Compression:
Stored size: 1.27 KB
Contents
# frozen_string_literal: true require 'active_support' class DHS::Record module Batch extend ActiveSupport::Concern module ClassMethods # Process single entries fetched in batches def find_each(options = {}) find_in_batches(options) do |records| records.each do |record| item = DHS::Item.new(record) yield new(DHS::Data.new(item, records._data, self)) end end end # Process batches of entries def find_in_batches(options = {}) raise 'No block given' unless block_given? options = options.dup start = options.delete(:start) || 1 batch_size = options.delete(:batch_size) || DHS::Pagination::Base::DEFAULT_LIMIT loop do # as suggested by Matz options = options.dup options[:params] = (options[:params] || {}).merge(limit_key(:parameter) => batch_size, pagination_key(:parameter) => start) data = request(options) batch_size = data._raw.dig(*limit_key(:body)) left = data._raw.dig(*total_key).to_i - data._raw.dig(*pagination_key(:body)).to_i - data._raw.dig(*limit_key(:body)).to_i yield new(data) break if left <= 0 start += batch_size end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
dhs-1.0.1 | lib/dhs/concerns/record/batch.rb |
dhs-1.0.0 | lib/dhs/concerns/record/batch.rb |