Class: Ruber::World::DocumentFactory

Inherits:
Qt::Object
  • Object
show all
Defined in:
lib/ruber/world/document_factory.rb

Overview

Note:

in the documentation, whenever a file name is mentioned, it can be replaced by a @KDE::Url@.

Factory class to create documents

It ensures that at all times there’s only a single document associated with a given file.

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ DocumentFactory

Returns a new instance of DocumentFactory.

Parameters:

  • parent (Qt::Object, nil) (defaults to: nil)

    the parent object



43
44
45
46
# File 'lib/ruber/world/document_factory.rb', line 43

def initialize parent = nil
  super
  @documents = {}
end

Instance Method Details

#document(file, parent = nil) ⇒ Document?

Returns a document, creating it if needed

If a file name is specified, a document for that file will be returned. If a document for that file name already exists, it will be returned. Otherwise, a new document will be created.

If no file or URL is given, a new document (not associated with a file) is returned.

Parameters:

  • file (String, KDE::Url, nil)

    if not nil, the absolute name or the URL of the file to retrieve the document for. If nil, a new document not associated with any file will be created

  • parent (Qt::Object, nil) (defaults to: nil)

    the object the document should be child of. If nil, the document will be parentless

Returns:

  • (Document, nil)

    a document associated with file or a new document not associated with a file if file is nil. If file represents a local file and that file doesn’t exist, nil is returned



65
66
67
68
69
70
71
72
73
# File 'lib/ruber/world/document_factory.rb', line 65

def document file, parent = nil
  if file
    url = KDE::Url.new file
    return if url.local_file? and !File.exist?(url.path)
    doc = @documents[url]
    doc || create_document(file, parent)
  else create_document nil, parent
  end
end