Class: Ruber::World::ProjectFactory

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

Overview

Class whose task is to ensure that there’s only one project open for any given project file.

To create a new project, call the #project method instead of using Project.new. If a project for the given file already exists, it’ll be returned, otherwise a new project will be created.

Defined Under Namespace

Classes: MismatchingNameError

Instance Method Summary (collapse)

Signal Summary

Constructor Details

- (ProjectFactory) initialize(parent = nil)

A new instance of ProjectFactory

Parameters:

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

    the parent object



81
82
83
84
# File 'lib/ruber/world/project_factory.rb', line 81

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

Instance Method Details

- (Project) project(file, name = nil)

Retrieves the project associated with a given project file

If a project associated with the project file file already exists, that project is returned. Otherwise, a new project is created. In this case, the #project_created signal is emitted

Parameters:

  • file (String)

    the path of the project file (it doesn’t need to exist)

  • name (String, nil) (defaults to: nil)

    the name of the project. If the project file already exists, then this should be nil. If the project file doesn’t exist, this should not be nil

Returns:

  • (Project)

    a project associated with file

Raises:

  • (MismatchingNameError)

    if name is specified, a project associated with file already exists but name and the name of the existing project are different



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ruber/world/project_factory.rb', line 98

def project file, name = nil
  prj = @projects[file]
  if prj
    if name and prj.project_name != name
      raise MismatchingNameError.new file, name, prj.project_name
    end
    prj
  else
    prj = Project.new file, name
    connect prj, SIGNAL('closing(QObject*)'), self, SLOT('project_closing(QObject*)')
    @projects[prj.project_file] = prj
    emit project_created prj
    prj
  end
end

- (nil) project_closing(prj) (private)

Method called whenever a project is closed

It ensures that the list of open projects is up to date

Returns:

  • (nil)


122
123
124
125
# File 'lib/ruber/world/project_factory.rb', line 122

def project_closing prj
  @projects.delete prj.project_file
  nil
end

Slot Signature:

project_closing(QObject*)

Signal Details

- project_created(QObject* prj)

Signal emitted when a new project object is created

The signal is emitted when a new project object is created, either from an existing project file or for a new project file.

Parameters:

  • prj (Project)

    the project object