Class: RobustExcelOle::BookStore
- Inherits:
-
Object
- Object
- RobustExcelOle::BookStore
- Defined in:
- lib/robust_excel_ole/book_store.rb
Instance Method Summary collapse
-
#fetch(filename, options = {:prefer_writable => true }) ⇒ Object
returns a book with the given filename, if it was open once prefers open books to closed books, and among them, prefers more recently opened books excludes hidden Excel instance options: :prefer_writable return the writable book, if it is open (default: true) otherwise return the book according to the preference order mentioned above :prefer_excel return the book in the given excel instance, if it exists, otherwise proceed according to prefer_writable.
-
#hidden_excel ⇒ Object
creates and returns a separate Excel instance with Visible and DisplayAlerts false.
-
#initialize ⇒ BookStore
constructor
A new instance of BookStore.
-
#store(book) ⇒ Object
stores a book.
Constructor Details
#initialize ⇒ BookStore
Returns a new instance of BookStore.
8 9 10 11 |
# File 'lib/robust_excel_ole/book_store.rb', line 8 def initialize @filename2books = Hash.new {|hash, key| hash[key] = [] } @hidden_excel_instance = nil end |
Instance Method Details
#fetch(filename, options = {:prefer_writable => true }) ⇒ Object
returns a book with the given filename, if it was open once prefers open books to closed books, and among them, prefers more recently opened books excludes hidden Excel instance options: :prefer_writable return the writable book, if it is open (default: true)
otherwise return the book according to the preference order mentioned above
:prefer_excel return the book in the given excel instance, if it exists,
otherwise proceed according to prefer_writable
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/robust_excel_ole/book_store.rb', line 20 def fetch(filename, = {:prefer_writable => true }) filename_key = RobustExcelOle::canonize(filename) weakref_books = @filename2books[filename_key] return nil unless weakref_books result = open_book = closed_book = nil weakref_books.each do |wr_book| if (not wr_book.weakref_alive?) begin @filename2books[filename_key].delete(wr_book) rescue puts "Warning: deleting dead reference failed! (file: #{filename})" end else book = wr_book.__getobj__ next if book.excel == try_hidden_excel if [:prefer_excel] && book.excel == [:prefer_excel] result = book break end if book.alive? open_book = book break if (book.writable && [:prefer_writable]) else closed_book = book end end end result = result ? result : (open_book ? open_book : closed_book) result if result end |
#hidden_excel ⇒ Object
creates and returns a separate Excel instance with Visible and DisplayAlerts false
64 65 66 67 68 69 |
# File 'lib/robust_excel_ole/book_store.rb', line 64 def hidden_excel unless (@hidden_excel_instance && @hidden_excel_instance.weakref_alive? && @hidden_excel_instance.__getobj__.alive?) @hidden_excel_instance = WeakRef.new(Excel.create) end @hidden_excel_instance.__getobj__ end |
#store(book) ⇒ Object
stores a book
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/robust_excel_ole/book_store.rb', line 52 def store(book) filename_key = RobustExcelOle::canonize(book.filename) if book.stored_filename old_filename_key = RobustExcelOle::canonize(book.stored_filename) # deletes the weak reference to the book @filename2books[old_filename_key].delete(book) end @filename2books[filename_key] |= [WeakRef.new(book)] book.stored_filename = book.filename end |