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)

Signal Summary

Constructor Details

- (DocumentFactory) initialize(parent = nil)

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

- (Ruber::Document) create_document(file, parent) (private)

Creates a new document

After creating the document, makes all the necessary signal-slot connections with it and adds it to the internal document list if needed

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)

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

Returns:



108
109
110
111
112
113
114
115
# File 'lib/ruber/world/document_factory.rb', line 108

def create_document file, parent
  doc = Document.new file, parent
  @documents[doc.url] = doc if file
  connect doc, SIGNAL('closing(QObject*)'), self, SLOT('document_closed(QObject*)')
  connect doc, SIGNAL('document_url_changed(QObject*)'), self, SLOT('document_url_changed(QObject*)')
  emit document_created(doc)
  doc
end

- (Document?) document(file, parent = nil)

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

- (Object) document_closed(doc) (private)

Slot called whenever a document is closed

It ensures that the document is removed from the internal document list

Parameters:

  • doc (Document)

    the document being closed



83
84
85
# File 'lib/ruber/world/document_factory.rb', line 83

def document_closed doc
  @documents.delete doc.url
end

Slot Signature:

document_closed(QObject*)

- (Object) document_url_changed(doc) (private)

Slot called whenever a document’s URL changes

It updates the internal list of documents

Parameters:

  • doc (Document)

    the document whose URL has changed



94
95
96
97
# File 'lib/ruber/world/document_factory.rb', line 94

def document_url_changed doc
  @documents.reject!{|k, v| v == doc}
  @documents[doc.url] = doc
end

Slot Signature:

document_url_changed(QObject*)

Signal Details

- document_created(QObject* arg1)