Class: RobustExcelOle::Bookstore

Inherits:
REOCommon show all
Defined in:
lib/robust_excel_ole/bookstore.rb

Instance Method Summary collapse

Methods inherited from REOCommon

#excel, #own_methods, puts_hash, tr1, trace

Constructor Details

#initializeBookstore

Returns a new instance of Bookstore.



8
9
10
11
# File 'lib/robust_excel_ole/bookstore.rb', line 8

def initialize
  @filename2books ||= Hash.new {|hash, key| hash[key] = [] }
  @hidden_excel_instance = nil
end

Instance Method Details

#booksObject

returns all stored books



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/robust_excel_ole/bookstore.rb', line 83

def books
  result = []
  if @filename2books
    @filename2books.each do |filename,books|
      unless books.empty? 
        books.each do |wr_book|
          result << wr_book.__getobj__ if wr_book.weakref_alive?
        end
      end
    end      
  end
  result
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 returns the writable book, if it is open (default: true)

                   otherwise returns the book according to the preference order mentioned above
:prefer_excel      returns the book in the given Excel instance, if it exists,
                   otherwise proceeds according to prefer_writable

Parameters:

  • filename (String)

    the file name

  • options (Hash) (defaults to: {:prefer_writable => true })

    the options

  • option (Hash)

    a customizable set of options



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
50
51
52
53
54
55
56
57
58
59
# File 'lib/robust_excel_ole/bookstore.rb', line 24

def fetch(filename, options = {:prefer_writable => true })
  return nil unless filename
  filename = General::absolute_path(filename)
  filename_key = General::canonize(filename)
  weakref_books = @filename2books[filename_key]
  return nil unless weakref_books
  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 (not wr_book.weakref_alive?)
      #trace "warn: this should never happen"
      begin 
        @filename2books[filename_key].delete(wr_book)
      rescue 
        #trace "#{$!.message}"
        #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 = result ? result : (open_book ? open_book : closed_book)
  result if result
end

#hidden_excelObject

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



75
76
77
78
79
80
# File 'lib/robust_excel_ole/bookstore.rb', line 75

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

prints the book store



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/robust_excel_ole/bookstore.rb', line 106

def print             
  #trace "@filename2books:"
  if @filename2books
    @filename2books.each do |filename,books|
      #trace " filename: #{filename}"
      #trace " books:"
      if books.empty? 
        #trace " []" 
      else
        books.each do |book|
          if book.weakref_alive?
            #trace "#{book}"
          else # this should never happen
            #trace "weakref not alive"
          end
        end
      end
    end
  end
end

#store(book) ⇒ Object

stores a workbook

Parameters:

  • book (Book)

    a given book



63
64
65
66
67
68
69
70
71
72
# File 'lib/robust_excel_ole/bookstore.rb', line 63

def store(book)
  filename_key = General::canonize(book.filename)      
  if book.stored_filename
    old_filename_key = General::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