Skip to content

Commit

Permalink
Add Spreadsheet#stream delegate method
Browse files Browse the repository at this point in the history
  • Loading branch information
dmolesUC committed Mar 20, 2023
1 parent a5c999b commit 39e70e2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/berkeley_library/util/xlsx/spreadsheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Spreadsheet

attr_reader :workbook, :xlsx_path

delegate :stream, to: :workbook

def initialize(xlsx_path = nil)
@workbook = xlsx_path ? ensure_xlsx_workbook!(xlsx_path) : RubyXL::Workbook.new
@xlsx_path = xlsx_path
Expand Down
23 changes: 23 additions & 0 deletions spec/berkeley_library/util/xlsx/spreadsheet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ module XLSX
expect(v_actual).to eq(v_expected)
end
end

describe :stream do
it 'returns the spreadsheet as a readable IO' do
out_stream = ss_write.stream
expect(out_stream).to be_a(StringIO)

# Bytewise comparison is unstable, probably b/c timestamps or something,
# so instead we copy the stream to a new file and read that

ss_read = Dir.mktmpdir(File.basename(__FILE__)) do |tmpdir|
xlsx_path = File.join(tmpdir, "#{num_rows}.xlsx")
File.binwrite(xlsx_path, out_stream.string)

Spreadsheet.new(xlsx_path)
end

c_indices.each_with_index do |c_index, r_index|
v_expected = values[r_index]
v_actual = ss_read.value_at(r_index, c_index)
expect(v_actual).to eq(v_expected)
end
end
end
end
end

Expand Down

0 comments on commit 39e70e2

Please sign in to comment.