lib/bill_hicks/inventory.rb in bill_hicks-1.1.5 vs lib/bill_hicks/inventory.rb in bill_hicks-1.1.6

- old
+ new

@@ -18,10 +18,17 @@ def self.all(options = {}) requires!(options, :username, :password) new(options).all end + def self.process_as_chunks(size = 15, options = {}) + requires!(options, :username, :password) + new(options).process_as_chunks(size) do |chunk| + yield(chunk) + end + end + # Returns an array of hashes with the inventory item details. def all inventory = [] connect(@options) do |ftp| @@ -37,9 +44,27 @@ } end end inventory + end + + # Streams csv and chunks it + # + # @size integer The number of items in each chunk + def process_as_chunks(size) + connect(@options) do |ftp| + temp_csv_file = Tempfile.new + + ftp.chdir(BillHicks.config.top_level_dir) + ftp.gettextfile(INVENTORY_FILENAME, temp_csv_file.path) + + SmarterCSV.process(temp_csv_file, { :chunk_size => size }) do |chunk| + yield(chunk) + end + + temp_csv_file.unlink + end end end end