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

Constructor Details

#initialize(parent = nil) ⇒ ProjectFactory

Returns 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(file, name = nil) ⇒ Project

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