class RobustExcelOle::Bookstore

@private

Public Class Methods

new() click to toggle source
# File lib/robust_excel_ole/bookstore.rb, line 7
def initialize
  @filename2books ||= Hash.new { |hash, key| hash[key] = [] }
  @hidden_excel_instance = nil
end

Public Instance Methods

books() click to toggle source

returns all stored books

# File lib/robust_excel_ole/bookstore.rb, line 87
def books
  @filename2books.map{ |_fn,books| books.map{ |wr_bk| wr_bk.__getobj__ if wr_bk.weakref_alive?}.compact}.flatten
end
excel() click to toggle source

returns an excel

# File lib/robust_excel_ole/bookstore.rb, line 13
def excel
  fn2books = @filename2books.first
  return nil if fn2books.nil? || fn2books.empty? || fn2books[1].empty?  
  book = fn2books[1].first.__getobj__
  book.excel
end
fetch(filename, options = { prefer_writable: true }) click to toggle source

returns a workbook with the given filename, if it was open once @param [String] filename the file name @param [Hash] options the options @option option [Boolean] :prefer_writable @option option [Boolean] :prefer_excel prefers open workbooks to closed workbooks, and among them, prefers more recently opened workbooks excludes hidden Excel instance options: :prefer_writable returns the writable workbook, if it is open (default: true)

                   otherwise returns the workbook according to the preference order mentioned above
:prefer_excel      returns the workbook in the given Excel instance, if it exists,
                   otherwise proceeds according to prefer_writable
# File lib/robust_excel_ole/bookstore.rb, line 31
def fetch(filename, options = { prefer_writable: true })
  return nil unless filename
  filename = General.absolute_path(filename)
  filename_key = General.canonize(filename).downcase
  weakref_books = @filename2books[filename_key]
  return nil if weakref_books.nil? || weakref_books.empty?
  result = open_book = closed_book = nil
  weakref_books = weakref_books.map { |wr_book| wr_book if wr_book.weakref_alive? }.compact
  @filename2books[filename_key] = weakref_books
  weakref_books.each do |wr_book|
    if !wr_book.weakref_alive?
      begin
        @filename2books[filename_key].delete(wr_book)
      rescue
        trace "Warning: deleting dead reference failed: file: #{filename.inspect}"
      end
    else
      book = wr_book.__getobj__
      next if book.excel == try_hidden_excel
      if options[:prefer_excel] && book.excel == options[:prefer_excel]
        result = book
        break
      end
      if book.alive?
        open_book = book
        break if book.writable && options[:prefer_writable]
      else
        closed_book = book
      end
    end
  end
  result ||= (open_book || closed_book)
end
hidden_excel() click to toggle source

creates and returns a separate Excel instance with Visible and DisplayAlerts equal false @private

# File lib/robust_excel_ole/bookstore.rb, line 79
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
print_filename2books() click to toggle source

prints the book store @private

store(book) click to toggle source

stores a workbook @param [Workbook] book a given book

# File lib/robust_excel_ole/bookstore.rb, line 67
def store(book)
  filename_key = General.canonize(book.filename).downcase
  if book.stored_filename
    old_filename_key = General.canonize(book.stored_filename).downcase
    # deletes the weak reference to the book
    @filename2books[old_filename_key].delete(book)
  end
  @filename2books[filename_key] |= [WeakRef.new(book)]      
end

Private Instance Methods

try_hidden_excel() click to toggle source
# File lib/robust_excel_ole/bookstore.rb, line 93
def try_hidden_excel 
  @hidden_excel_instance.__getobj__ if @hidden_excel_instance && @hidden_excel_instance.weakref_alive? && @hidden_excel_instance.__getobj__.alive?
end