Class: RobustExcelOle::BookStore
- Inherits:
-
Object
- Object
- RobustExcelOle::BookStore
- Defined in:
- lib/robust_excel_ole/book_store.rb
Instance Method Summary collapse
-
#ensure_hidden_excel ⇒ Object
creates and returns a separate Excel instance with Visible and DisplayAlerts false.
-
#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.
-
#initialize ⇒ BookStore
constructor
A new instance of BookStore.
-
#print ⇒ Object
prints the book store.
-
#store(book) ⇒ Object
stores a book.
- #try_hidden_excel ⇒ Object
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
#ensure_hidden_excel ⇒ Object
creates and returns a separate Excel instance with Visible and DisplayAlerts false
60 61 62 63 64 65 |
# File 'lib/robust_excel_ole/book_store.rb', line 60 def ensure_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 |
#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 |
# 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?) @filename2books[filename_key].delete(wr_book) 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 |
#print ⇒ Object
prints the book store
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/robust_excel_ole/book_store.rb', line 72 def print p "@filename2books:" if @filename2books @filename2books.each do |filename,books| p " filename: #{filename}" p " books:" p " []" if books == [] if books books.each do |book| if book.weakref_alive? p "#{book}" p "excel: #{book.excel}" p "alive: #{book.alive?}" else p "weakref not alive" end end end end end end |
#store(book) ⇒ Object
stores a book
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/robust_excel_ole/book_store.rb', line 48 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 |
#try_hidden_excel ⇒ Object
67 68 69 |
# File 'lib/robust_excel_ole/book_store.rb', line 67 def try_hidden_excel @hidden_excel_instance.__getobj__ if (@hidden_excel_instance && @hidden_excel_instance.weakref_alive? && @hidden_excel_instance.__getobj__.alive?) end |