Class: Ruber::ProjectList
- Includes:
- Enumerable, PluginLike
- Defined in:
- lib/ruber/projects/project_list.rb
Overview
List of all open global projects
It allows to obtain a list of the open projects, to know when a project is closed and keeps trace of which is the current project. The most common usage is the following:
-
use the #project method to retrieve an open project basing on the name of its file, or to open it if it isn’t already open, or to create a new project. This will also cause the project to be added to the list of open projects, if needed
-
use #current_project= to set the current project
-
use one of the methods to iterate or work with one of the projects
-
close one of the projects with the project’s close method, or use the #close_current_project method to close the current project.
Instance Attribute Summary collapse
-
#current_project ⇒ Ruber::Project?
(also: #current)
The current project or nil if there’s no open project.
Attributes included from PluginLike
Instance Method Summary collapse
-
#[](arg) ⇒ Object
Returns the project corresponding to a given file or with a given name.
-
#add_project(prj) ⇒ Ruber::Project
Adds a project to the list.
-
#close_current_project ⇒ nil
Closes the current project If there’s not a current project, nothing is done.
-
#close_project(prj) ⇒ nil
Closes a project.
-
#each_project(&blk) ⇒ Object
(also: #each)
Iterates on all the projects.
-
#initialize(_manager, psf) ⇒ ProjectList
constructor
the component.
-
#new_project(file, name) ⇒ Ruber::Project
Creates a new empty project.
-
#project(file) ⇒ Ruber::Project
Returns the project associated with a project file, opening it if necessary.
-
#project_for_file(file, which = :active_only) ⇒ Ruber::Project?
Returns the project associated with a given project file.
-
#projects ⇒ Array<Ruber::Project>
The existing projects.
-
#query_close ⇒ Boolean
Tells whether it’s all right for the projects to close the application.
-
#save_settings ⇒ nil
Saves each open project.
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) ⇒ ProjectList
the component
91 92 93 94 95 96 |
# File 'lib/ruber/projects/project_list.rb', line 91 def initialize _manager, psf super Ruber[:app] initialize_plugin psf @current_project = nil @projects = {} end |
Instance Attribute Details
#current_project ⇒ Ruber::Project? Also known as: current
Returns the current project or nil if there’s no open project.
83 84 85 |
# File 'lib/ruber/projects/project_list.rb', line 83 def current_project @current_project end |
Instance Method Details
#[](name) ⇒ Ruber::Project? #[](file) ⇒ Ruber::Project?
Returns the project corresponding to a given file or with a given name
166 167 168 169 170 171 |
# File 'lib/ruber/projects/project_list.rb', line 166 def [] arg if arg[0,1] == '/' then @projects[arg] else find{|prj| prj.project_name == arg} end end |
#add_project(prj) ⇒ Ruber::Project
Adds a project to the list
The #project_added signal is emitted after adding the project.
Since this method is automatically called by both #project and #new_project, you usually don’t need to call it, unless you need to create the project using @Project.new@ rather than using one of the above methods.
already in the list
225 226 227 228 229 230 231 232 233 |
# File 'lib/ruber/projects/project_list.rb', line 225 def add_project prj if @projects[prj.project_file] raise "A project with project file #{prj.project_file} is already open" end @projects[prj.project_file] = prj connect prj, SIGNAL('closing(QObject*)'), self, SLOT('close_project(QObject*)') emit project_added(prj) prj end |
#close_current_project ⇒ nil
Closes the current project
If there’s not a current project, nothing is done. Otherwise, it simply calls the #close_project method passing the current project as argument.
243 244 245 246 |
# File 'lib/ruber/projects/project_list.rb', line 243 def close_current_project @current_project.close if @current_project nil end |
#close_project(prj) ⇒ nil
Closes a project
If the project is current, sets the current project to nil before closing it. In all cases, emits the #closing_project signal before closing the project.
258 259 260 261 262 263 |
# File 'lib/ruber/projects/project_list.rb', line 258 def close_project prj self.current_project = nil if @current_project == prj emit closing_project(prj) @projects.delete prj.project_file nil end |
#each_project {|prj| ... } ⇒ Ruber::ProjectList #each_project ⇒ Enumerator Also known as: each
Iterates on all the projects
In both versions of the method, the order in which the projects are passed to the block is arbitrary
112 113 114 115 |
# File 'lib/ruber/projects/project_list.rb', line 112 def each_project &blk #:yields: project res = @projects.each_value &blk res.same?(@projects) ? self : res end |
#new_project(file, name) ⇒ Ruber::Project
Creates a new empty project
After being created, the new project is added to the list. You almost always should use this method, rather than calling AbstractProject.new to create a new empty project.
new project
186 187 188 |
# File 'lib/ruber/projects/project_list.rb', line 186 def new_project file, name add_project Project.new( file, name ) end |
#project(file) ⇒ Ruber::Project
Returns the project associated with a project file, opening it if necessary
This is one of the core methods of this class. It searchs the list of open projects and return the one corresponding to the given project file. If a project for that file isn’t open, it will be opened and added to the list of open projects.
If, for any reason, you create a project using @Project.new@ instead of using this method, you’ll need to add it to the project list yourself using #add_project.
or can’t be opened
145 146 147 |
# File 'lib/ruber/projects/project_list.rb', line 145 def project file @projects[file] || add_project(Project.new(file)) end |
#project_for_file(file, which = :active_only) ⇒ Ruber::Project?
Returns the project associated with a given project file
be returned only if it’s active. If it is @:all@ then it will be returned even if it’s inactive. Any other value will cause this method to always return nil if the list doesn’t contain any such file or if it doesn’t respect the value of which.
301 302 303 304 305 306 307 308 309 |
# File 'lib/ruber/projects/project_list.rb', line 301 def project_for_file file, which = :active_only current_prj = current return nil unless current_prj if current_prj.project_files.file_in_project?(file) then current_prj elsif which == :all find{|prj| prj.project_files.file_in_project?(file)} else nil end end |
#projects ⇒ Array<Ruber::Project>
The existing projects
wont affect the @ProjectList@
124 125 126 |
# File 'lib/ruber/projects/project_list.rb', line 124 def projects @projects.values end |
#query_close ⇒ Boolean
Tells whether it’s all right for the projects to close the application
It calls the @query_close@ method for each project, returning false if any of them returns false and true if all return true
and false if at least one of them say the application can’t be closed
285 286 287 288 |
# File 'lib/ruber/projects/project_list.rb', line 285 def query_close @projects.values.each{|pr| return false unless pr.query_close} true end |
#save_settings ⇒ nil
Saves each open project
271 272 273 274 |
# File 'lib/ruber/projects/project_list.rb', line 271 def save_settings @projects.values.each{|pr| pr.save} nil end |