Class: Ruber::DocumentList
- Includes:
- Enumerable, PluginLike
- Defined in:
- lib/ruber/documents/document_list.rb
Overview
List of all open documents
It contains convenience methods to iterate on the open documents and to create them. Whenever possible, you should use them. In particular, when you need a document associated with a given file or URL, you should always use #document, so that if a document for that file is already installed, no new document will be created.
If, for any reason, you need to create a document using Document.new, please don’t forget to add the document to the list using #add_document.
Instance Attribute Summary
Attributes included from PluginLike
Instance Method Summary collapse
-
#[](key) ⇒ Document?
The document corresponding to the given key.
-
#add_document(doc) ⇒ Document?
Adds a new document to the list.
-
#close_all(ask = true) ⇒ Boolean
Closes all the documents If there are modified files and ask is true, the user will be asked whether he wants to save them (see MainWindow#save_documents).
-
#document(file, create_if_needed = true) ⇒ Document?
The document for a given file or url.
-
#document_for_file(file) ⇒ Document?
The document associated with a given file.
-
#document_for_file?(file) ⇒ Boolean
Whether there’s a document associated with a given file.
-
#document_for_url(url) ⇒ Document?
The document associated with a given URL.
-
#document_for_url?(url) ⇒ Boolean
Whether there’s a document associated with a given URL.
-
#document_with_name(name) ⇒ Document?
The document with a given name.
-
#document_with_name?(name) ⇒ Boolean
Whether there’s a document with a given name.
-
#documents ⇒ Array<Document>
(also: #to_a)
A list of open documents.
-
#documents_with_file(which = :any) ⇒ Array<Document>
The documents which are associated with a file.
-
#each_document {|Document| ... } ⇒ DocumentList, Enumerator
(also: #each)
Calls the block for each document otherwise.
-
#empty? ⇒ Boolean
Whether the document list is empty or not.
-
#initialize(_manager, psf) ⇒ DocumentList
constructor
A new instance of DocumentList.
-
#new_document ⇒ Ruber::Document
Creates a new empty document.
-
#query_close ⇒ Boolean
Override of PluginLike#query_close.
-
#save_documents(docs, stop_on_failure = false) ⇒ Array<Document>
Attempts to save the given documents.
-
#save_settings ⇒ nil
Saves the settings for all open documents.
-
#size ⇒ Integer
(also: #length)
The number of documents in the list.
Methods included from Enumerable
Methods included from PluginLike
#add_extensions_to_project, #add_options_to_project, #add_widgets_to_project, #plugin_name, #register_with_project, #remove_extensions_from_project, #remove_from_project, #remove_options_from_project, #remove_widgets_from_project, #restore_session, #session_data, #shutdown, #unload, #update_project
Constructor Details
#initialize(_manager, psf) ⇒ DocumentList
Returns a new instance of DocumentList.
67 68 69 70 71 |
# File 'lib/ruber/documents/document_list.rb', line 67 def initialize _manager, psf super Ruber[:app] initialize_plugin psf @docs = [] end |
Instance Method Details
#[](key) ⇒ Document?
The document corresponding to the given key
How key is interpreted depends on its class:
-
if it’s an @Integer@, the document in the corresponding position in the list will be returned
-
if it’s a @KDE::Url@, then the document associated with that url will be returned
-
if it’s a @String@ starting with a @/@ (that is, an absolute path) then the document associated with that file will be returned
-
if it’s a string not startng with a @/@, then the document with that @document_name@ will be returned
270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/ruber/documents/document_list.rb', line 270 def [] key case key when String if Pathname.new(key).absolute? then @docs.find{|d| d.path == key} else @docs.find{|d| d.document_name == key} end when KDE::Url @docs.find{|d| d.url == key} when Integer then @docs[key] else raise TypeError end end |
#add_document(doc) ⇒ Document?
Adds a new document to the list
If you use #new_document and #document to create documents, you don’t need to call this method.
Note: this method doesn’t check whether the document has already been added to the list. If so, the results may cause errors. Please, always make sure the document isn’t already in the list before calling this.
164 165 166 167 168 169 |
# File 'lib/ruber/documents/document_list.rb', line 164 def add_document doc if doc connect doc, SIGNAL('closing(QObject*)'), self, SLOT('close_document(QObject*)') @docs << doc end end |
#close_all(ask = true) ⇒ Boolean
Closes all the documents
If there are modified files and ask is true, the user will be asked whether he wants to save them (see MainWindow#save_documents). If he chooses to abort closing, nothing will be done
243 244 245 246 247 248 249 250 |
# File 'lib/ruber/documents/document_list.rb', line 243 def close_all ask = true docs = @docs.dup if !ask or Ruber[:main_window].save_documents docs docs.each {|d| d.close false} true else false end end |
#document(file, create_if_needed = true) ⇒ Document?
The document for a given file or url
If there’s no open document associated with the given file or url, depending on the value of create_if_needed a new document associated with the file or url will be created. In this case, the #document_created signal will be emitted.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/ruber/documents/document_list.rb', line 109 def document file, create_if_needed = true if file.is_a? String url = KDE::Url.new file url.path = File.(file) if url.relative? else url = file end doc = document_for_url file return doc if doc or !create_if_needed if !doc and create_if_needed if url.local_file? raise ArgumentError, "File #{url.path} doesn't exist" unless File.exist?(url.path) end doc = Document.new Ruber[:main_window], url add_document doc emit document_created(doc) end doc end |
#document_for_file(file) ⇒ Document?
The document associated with a given file
306 307 308 309 |
# File 'lib/ruber/documents/document_list.rb', line 306 def document_for_file file file = File. file @docs.find{|d| d.path == file} end |
#document_for_file?(file) ⇒ Boolean
Whether there’s a document associated with a given file
368 369 370 371 |
# File 'lib/ruber/documents/document_list.rb', line 368 def document_for_file? file file = File. file @docs.any?{|d| d.path == file} end |
#document_for_url(url) ⇒ Document?
The document associated with a given URL
318 319 320 |
# File 'lib/ruber/documents/document_list.rb', line 318 def document_for_url url @docs.find{|d| d.url == url} end |
#document_for_url?(url) ⇒ Boolean
Whether there’s a document associated with a given URL
otherwise
380 381 382 |
# File 'lib/ruber/documents/document_list.rb', line 380 def document_for_url? url @docs.any?{|d| d.url == url} end |
#document_with_name(name) ⇒ Document?
The document with a given name
329 330 331 |
# File 'lib/ruber/documents/document_list.rb', line 329 def document_with_name name @docs.find{|d| d.document_name == name} end |
#document_with_name?(name) ⇒ Boolean
Whether there’s a document with a given name
391 392 393 |
# File 'lib/ruber/documents/document_list.rb', line 391 def document_with_name? name @docs.any?{|d| d.document_name == name} end |
#documents ⇒ Array<Document> Also known as: to_a
Returns a list of open documents.
131 132 133 |
# File 'lib/ruber/documents/document_list.rb', line 131 def documents @docs.dup end |
#documents_with_file(which = :any) ⇒ Array<Document>
The documents which are associated with a file
which can be used to restrict the list of documents to only those associated with local or remote files:
-
if which is @:local@ only documents associated with local files will be returned;
-
if which is @:remote@ only documents associated with remoted files will be returned;
-
if which has any other value, both documents associated with local and with remote files will be returned
346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/ruber/documents/document_list.rb', line 346 def documents_with_file which = :any @docs.select do |d| if d.has_file? case which when :local then d.url.local_file? when :remote then !d.url.local_file? else true end end end end |
#each_document {|Document| ... } ⇒ DocumentList, Enumerator Also known as: each
Calls the block for each document otherwise
289 290 291 292 293 294 295 |
# File 'lib/ruber/documents/document_list.rb', line 289 def each_document if block_given? @docs.each{|d| yield d} self else self.to_enum end end |
#empty? ⇒ Boolean
Returns whether the document list is empty or not.
139 140 141 |
# File 'lib/ruber/documents/document_list.rb', line 139 def empty? @docs.empty? end |
#new_document ⇒ Ruber::Document
Creates a new empty document
The document is automatically added to the list and the #document_created signal is emitted
82 83 84 85 86 87 |
# File 'lib/ruber/documents/document_list.rb', line 82 def new_document doc = Document.new Ruber[:main_window] add_document doc emit document_created(doc) doc end |
#query_close ⇒ Boolean
Override of PluginLike#query_close
It first calls the query_close of each document’s own project, returning false as soon as one of them returns false, then attempts to save each document
405 406 407 408 |
# File 'lib/ruber/documents/document_list.rb', line 405 def query_close @docs.each{|d| return false unless d.own_project.query_close} Ruber[:main_window].save_documents end |
#save_documents(docs, stop_on_failure = false) ⇒ Array<Document>
Attempts to save the given documents
What happens if a document can’t be saved depends on the value of stop_on_failure: if it’s false, documents which can’t be saved are skipped, while if it’s true, this method will return as soon as one document fails to save.
Note: by can’t be saved, we mean that the user chose to save a document but, for any reason, it couldn’t be saved (for example this can happen if the user doesn’t have write permission on the file, or if the disk is full). If the user decides not to save the file, instead, it is considered a success.
192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/ruber/documents/document_list.rb', line 192 def save_documents docs, stop_on_failure = false failed = [] docs.each_with_index do |d, i| success = d.save failed << d unless success if !success and stop_on_failure failed += docs[(i+1)..-1] break end end failed end |
#save_settings ⇒ nil
Saves the settings for all open documents
210 211 212 213 |
# File 'lib/ruber/documents/document_list.rb', line 210 def save_settings @docs.each{|d| d.save_settings} nil end |
#size ⇒ Integer Also known as: length
Returns the number of documents in the list.
146 147 148 |
# File 'lib/ruber/documents/document_list.rb', line 146 def size @docs.size end |