From c80cd285fa4ec65eb4a029a8e5ed87dccd39c0f8 Mon Sep 17 00:00:00 2001 From: Jonathan Tron Date: Mon, 29 Nov 2010 17:39:44 +0100 Subject: [PATCH] Add Mongo::GridIO#each which yield each chunks of a file, allowing Rack streaming --- lib/mongo/gridfs/grid_io.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/mongo/gridfs/grid_io.rb b/lib/mongo/gridfs/grid_io.rb index 1409b82..2d9ca36 100644 --- a/lib/mongo/gridfs/grid_io.rb +++ b/lib/mongo/gridfs/grid_io.rb @@ -191,6 +191,20 @@ module Mongo end id end + + # Read a chunk of the data from the file and yield it to the given + # block. It will read from the current file position. + # + # @param [Block] A block called with each chunk + # + # @return [Mongo::GridIO] self + def each + return read_all unless block_given? + while chunk = read(chunk_size) + yield chunk + end + self + end def inspect "#"