Class: Ruber::World::MutableProjectList

Inherits:
ProjectList show all
Defined in:
lib/ruber/world/project_list.rb

Overview

A ProjectList which allows to change the contents of the list.

Instance Method Summary collapse

Methods inherited from ProjectList

#==, #[], #each, #empty?, #eql?, #hash, #size

Methods included from Enumerable

#find!

Constructor Details

#initialize(prjs = []) ⇒ MutableProjectList

Returns a new instance of MutableProjectList.

Parameters:

  • prjs (Array<Project>, ProjectList) (defaults to: [])

    the projects to insert in the list when created. Further changes to prjs won’t change the new instance and vice versa



171
172
173
# File 'lib/ruber/world/project_list.rb', line 171

def initialize prjs = []
  @projects = Hash[prjs.map{|prj| [prj.project_name, prj]}]
end

Instance Method Details

#add(*projects) ⇒ MutableProjectList

Adds projects to the list

Parameters:

  • projects (Array<Project,Array<Project>>)

    the projects to add. If it contains nested arrays, they’ll be flattened. If the list contains multiple projects with the same project file, only the last one will be kept (if a project with the same project name was already in the list, it’ll be overwritten)

Returns:



205
206
207
208
209
# File 'lib/ruber/world/project_list.rb', line 205

def add *projects
  projects.flatten.each do |prj|
    @projects[prj.project_file] = prj
  end
end

#clearMutableProjectList

Removes all the elements from the list

Returns:



243
244
245
246
# File 'lib/ruber/world/project_list.rb', line 243

def clear
  @projects.clear
  self
end

#cloneMutableProjectList

Override of @Object#clone@

Returns:



187
188
189
190
191
192
193
194
# File 'lib/ruber/world/project_list.rb', line 187

def clone
  res = self.class.new self
  if frozen?
    res.freeze
    res.project_hash.freeze
  end
  res
end

#delete_if {|prj| ... } ⇒ MutableProjectList

Removes from the list all the projects for which the block returns true

Yield Parameters:

  • prj (Project)

    the projects in the list

Yield Returns:

  • (Boolean)

    true for projects which should be removed from the list and false otherwise

Returns:



256
257
258
259
# File 'lib/ruber/world/project_list.rb', line 256

def delete_if &blk
  @projects.delete_if{|_, prj| blk.call prj}
  self
end

#dupMutableProjectList

Override of @Object#dup@

Returns:



179
180
181
# File 'lib/ruber/world/project_list.rb', line 179

def dup
  self.class.new self
end

#merge!(prjs) ⇒ MutableProjectList

Adds the projects contained in another list to this list

Parameters:

Returns:



218
219
220
221
222
223
224
# File 'lib/ruber/world/project_list.rb', line 218

def merge! prjs
  if prjs.is_a? ProjectList then @projects.merge! prjs.project_hash
  else
    @projects.merge! Hash[prjs.map{|prj| [prj.project_file, prj]}]
  end
  self
end

#remove(prj) ⇒ Project?

Removes a project from the list

If the given project isn’t in the list, nothing is done

Parameters:

  • doc (Project)

    the project to remove

Returns:

  • (Project, nil)

    the removed project or nil if no project was removed



234
235
236
# File 'lib/ruber/world/project_list.rb', line 234

def remove prj
  @projects.delete prj.project_file
end