Class: Ruber::World::ProjectList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ruber/world/project_list.rb

Overview

Note:

This list can’t contain more than one project with the same project file.

A list of projects

It’s an immutable Enumerable class with some convenience methods for dealing with projects.

The projects in the list are set in the constructor and can’t be changed later.

The order of projects won’t be kept.

Direct Known Subclasses

MutableProjectList

Instance Method Summary (collapse)

Methods included from Enumerable

#find!

Constructor Details

- (ProjectList) initialize(prjs)

A new instance of ProjectList

Parameters:

  • prjs (Array<Project>, ProjectList)

    the projects to insert in the list when created. If it’s a Ruber::World::ProjectList, changes to prjs will be reflected by the newly created object. This won’t happen if prjs is an array.

    If the list contains multiple projects with the same project file, only the last one will be inserted in the list



49
50
51
52
53
# File 'lib/ruber/world/project_list.rb', line 49

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

Instance Method Details

- (Boolean) ==(other)

Comparison operator

Parameters:

  • other (Object)

    the object to compare self with

Returns:

  • (Boolean)

    true if other is either an Array or a Ruber::World::ProjectList containing the same elements as self and false otherwise



96
97
98
99
100
101
102
103
# File 'lib/ruber/world/project_list.rb', line 96

def == other
  case other
  when ProjectList then @projects == other.project_hash
  when Array
    @projects.values.sort_by(&:object_id) == other.sort_by(&:object_id)
  else false
  end
end

- (Project?) [](filename) - (Project?) [](name)

Element access

Overloads:

  • - (Project?) [](filename)

    Retrieves the project for the given project file

    Parameters:

    • filename (String)

      the absolute path of the project file. It must start with a slash

    Returns:

    • (Project, nil)

      the project having filename as project file or nil if no project having that project file is in the list

  • - (Project?) [](name)

    Retrieves the project having the given project name

    Parameters:

    • name (String)

      the project name. It must not start with a slash

    Returns:

    • (Project, nil)

      the project having the given project name or nil if no such project exists in the list. If there is more than one document with the same project name, one of them is returned

Returns:



142
143
144
145
146
147
148
# File 'lib/ruber/world/project_list.rb', line 142

def [] arg
  if arg.start_with? '/' then @projects[arg]
  else 
    prj = @projects.find{|i| i[1].project_name == arg}
    prj ? prj[1] : nil
  end
end

- (ProjectList) each {|prj| ... } - (Enumerator) each

Iterates on the projects

Overloads:

  • - (ProjectList) each {|prj| ... }

    Calls the block once for each project in the list (the order is arbitrary)

    Yield Parameters:

    • prj (Project)

      the projects in the list

    Returns:

  • - (Enumerator) each

    An enumerator which iterates on the projects

    Returns:

    • (Enumerator)

      an enumerator which iterates on the projects

Returns:



66
67
68
69
70
71
72
# File 'lib/ruber/world/project_list.rb', line 66

def each &blk
  if block_given?
    @projects.each_value &blk
    self
  else to_enum
  end
end

- (Boolean) empty?

Whether or not the list is empty

Returns:

  • (Boolean)

    true if the list is empty and false otherwise



78
79
80
# File 'lib/ruber/world/project_list.rb', line 78

def empty?
  @projects.empty?
end

- (Boolean) eql?(other)

Comparison operator used by Hash

Parameters:

  • other (Object)

    the object to compare self with

Returns:



112
113
114
# File 'lib/ruber/world/project_list.rb', line 112

def eql? other
  other.is_a?(ProjectList) ? @projects.eql?(other.project_hash) : false
end

- (Integer) hash

Override of Object#hash

Returns:

  • (Integer)

    the hash value for self



121
122
123
# File 'lib/ruber/world/project_list.rb', line 121

def hash
  @projects.hash
end

- (Hash{String=>Project}) project_hash (protected)

The internal hash used to keep trace of the projects

Returns:

  • (Hash{String=>Project})

    the internal hash used to keep trace of the projects



155
156
157
# File 'lib/ruber/world/project_list.rb', line 155

def project_hash
  @projects
end

- (Integer) size

The number of projects in the list

Returns:

  • (Integer)

    the number of projects in the list



85
86
87
# File 'lib/ruber/world/project_list.rb', line 85

def size
  @projects.size
end