Module: DHS::Record::Batch::ClassMethods
- Defined in:
- lib/dhs/concerns/record/batch.rb
Instance Method Summary collapse
-
#find_each(options = {}) ⇒ Object
Process single entries fetched in batches.
-
#find_in_batches(options = {}) ⇒ Object
Process batches of entries.
Instance Method Details
#find_each(options = {}) ⇒ Object
Process single entries fetched in batches
12 13 14 15 16 17 18 19 |
# File 'lib/dhs/concerns/record/batch.rb', line 12 def find_each( = {}) find_in_batches() do |records| records.each do |record| item = DHS::Item.new(record) yield new(DHS::Data.new(item, records._data, self)) end end end |
#find_in_batches(options = {}) ⇒ Object
Process batches of entries
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dhs/concerns/record/batch.rb', line 22 def find_in_batches( = {}) raise 'No block given' unless block_given? = .dup start = [:start] || self.pagination_class::DEFAULT_OFFSET batch_size = .delete(:batch_size) || self.pagination_class::DEFAULT_LIMIT loop do # as suggested by Matz = .dup [:params] = ([:params] || {}).merge(limit_key(:parameter) => batch_size, pagination_key(:parameter) => start) data = request() pagination = self.pagination(data) batch_size = pagination.limit left = pagination.pages_left yield new(data) break if left <= 0 start = pagination.next_offset end end |