From f2ad80516922c2872004c387e86595e18a74f845 Mon Sep 17 00:00:00 2001 From: Thomas <1258170+ThomasSevestre@users.noreply.github.com> Date: Wed, 10 May 2023 11:44:51 +0200 Subject: [PATCH] collections : add support for enum chains --- lib/saxlsx/rows_collection.rb | 6 +++++- lib/saxlsx/shared_string_collection.rb | 6 +++++- lib/saxlsx/sheet_collection.rb | 6 +++++- lib/saxlsx/style_collection.rb | 6 +++++- spec/sheet_spec.rb | 10 ++++++++++ 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/saxlsx/rows_collection.rb b/lib/saxlsx/rows_collection.rb index ae07e07..102b2e1 100644 --- a/lib/saxlsx/rows_collection.rb +++ b/lib/saxlsx/rows_collection.rb @@ -12,7 +12,11 @@ def initialize(index, file_system, workbook) end def each(&block) - RowsCollectionParser.parse @index, @sheet, @workbook, &block + if block + RowsCollectionParser.parse @index, @sheet, @workbook, &block + else + to_enum + end end def count diff --git a/lib/saxlsx/shared_string_collection.rb b/lib/saxlsx/shared_string_collection.rb index aaca9bb..5f63bef 100644 --- a/lib/saxlsx/shared_string_collection.rb +++ b/lib/saxlsx/shared_string_collection.rb @@ -9,7 +9,11 @@ def initialize(file_system) end def each(&block) - SharedStringCollectionParser.parse @file_system, &block + if block + SharedStringCollectionParser.parse @file_system, &block + else + to_enum + end end end diff --git a/lib/saxlsx/sheet_collection.rb b/lib/saxlsx/sheet_collection.rb index ae17a9c..61dfa60 100644 --- a/lib/saxlsx/sheet_collection.rb +++ b/lib/saxlsx/sheet_collection.rb @@ -10,7 +10,11 @@ def initialize(file_system, workbook) end def each(&block) - SheetCollectionParser.parse @file_system, @workbook, &block + if block + SheetCollectionParser.parse @file_system, @workbook, &block + else + to_enum + end end end diff --git a/lib/saxlsx/style_collection.rb b/lib/saxlsx/style_collection.rb index 3e8ce90..57d285c 100644 --- a/lib/saxlsx/style_collection.rb +++ b/lib/saxlsx/style_collection.rb @@ -9,7 +9,11 @@ def initialize(file_system) end def each(&block) - StyleCollectionParser.parse @file_system, &block + if block + StyleCollectionParser.parse @file_system, &block + else + to_enum + end end end diff --git a/spec/sheet_spec.rb b/spec/sheet_spec.rb index a1ccbba..784c7f4 100644 --- a/spec/sheet_spec.rb +++ b/spec/sheet_spec.rb @@ -27,6 +27,16 @@ end end + it 'handle enumeration' do + indexes = [] + Workbook.open filename do |w| + w.sheets[0].rows.each.with_index do |r, i| + indexes << i + end + end + indexes.should eq [0, 1, 2, 3, 4, 5, 6] + end + it 'Rows content' do Workbook.open filename do |w| w.sheets[0].tap do |s|