Is there any way to get Roo to accept StringIO objects in place of files?

跟風遠走 提交于 2019-12-23 13:10:55

问题


I'm trying to write some unit tests which involves Roo reading Excel 2007 files. I have the Excel file in my unit test file as a hex string, which in turn is fed into a StringIO instance. I can't simply pass the StringIO object to Roo::Spreadsheet.open, since that function actually checks if the passed object is a File instance:

def open(file, options = {})
  file = File === file ? file.path : file

  # ...

and if it isn't, proceeds to assume it's a string. Manually specifying the extension doesn't help:

doc = Roo::Spreadsheet.open(file, extension: :xlsx)

Are there any clever ways of getting Roo to use the StringIO instance as a file?


回答1:


It looks like this version of roo has support for this. Instead of checking explicitly if it's a File class, it checks in duck-typing style if it's a stream based on whether it responds to #seek. The relevant code is here and here.



来源:https://stackoverflow.com/questions/19695986/is-there-any-way-to-get-roo-to-accept-stringio-objects-in-place-of-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!