voraz / spreadsheet

http://spreadsheet.rubyforge.org/
GNU General Public License v3.0
59 stars 10 forks source link

lazy worksheet initialization is harmful? #9

Open timon opened 12 years ago

timon commented 12 years ago

While writing tests for worksheet protection timon@c2351bd3e2 I've found strange issue:

The following code fails:

  def test_read_protected
      path = File.join @data, "test_merged_and_protected.xls"
      book = Spreadsheet.open path
      sheet = book.worksheet(0)
      assert(sheet.protected?, "Expected sheet to be protected")
      assert_equal(Spreadsheet::Excel::Password.password_hash('testing'), sheet.password_hash)
    end
# => Expected sheet to be protected.

The following code runs fine due to side effects:

   def test_read_protected
      path = File.join @data, "test_merged_and_protected.xls"
      book = Spreadsheet.open path
      sheet = book.worksheet(0)
      sheet.inspect # Hello, magic! 
      assert(sheet.protected?, "Expected sheet to be protected")
      assert_equal(Spreadsheet::Excel::Password.password_hash('testing'), sheet.password_hash)
    end

I'm not sure what's correct way to handle this issue — either load all worksheets greedily after workbook is read, or guard all methods with ensure_rows_read — both approaches seems to have their positive and negative sides

zdavatz commented 12 years ago

What is the reason you want to protect a worksheet?

timon commented 12 years ago

It's not about worksheet I want to protect, it's about leaving worksheet in half-initialized state.

When I ask workbook 'Gimme that sheet' I expect to get an object I can work with, and turns out it is not so. The sheet has to be initialized in some special way before anything useful can be done to it, and I think this is against POLA.

zdavatz commented 11 years ago

so do you have a patch you suggest?