class Xcodeproj::Project::ObjectList

This class represents an ordered relationship to many objects.

It works in conjunction with the {AbstractObject} class to ensure that the project is not serialized with unreachable objects by updating the with reference count on modifications.

@note Concerning the mutations methods it is safe to call only those

which are overridden to inform objects reference count. Ideally all
the array methods should be covered, but this is not done yet.
Moreover it is a moving target because the methods of array
usually are implemented in C

@todo Cover all the mutations methods of the {Array} class.

Attributes

attribute[R]

@return [Array<Class>] the attribute that generated the list.

owner[R]

@return [Array<Class>] the object that owns the list.

Public Class Methods

new(attribute, owner) click to toggle source

{Xcodeproj} clients are not expected to create instances of {ObjectList}, it is always initialized empty and automatically by the synthesized methods generated by {AbstractObject.has_many}.

# File lib/xcodeproj/project/object_list.rb, line 22
def initialize(attribute, owner)
  @attribute = attribute
  @owner = owner
end

Public Instance Methods

+(other) click to toggle source

Adds an array of objects to list and updates their references count.

@param [Array<AbstractObject, ObjectDictionary>] objects

an array of objects to add to the list.

@return [void]

Calls superclass method
# File lib/xcodeproj/project/object_list.rb, line 63
def +(other)
  perform_additions_operations(other)
  super
end
<<(object) click to toggle source

Appends an object to list the and updates its references count.

@param [AbstractObject, ObjectDictionary] object

The object to add to the list.

@return [void]

Calls superclass method
# File lib/xcodeproj/project/object_list.rb, line 75
def <<(object)
  perform_additions_operations(object)
  super
end
clear() click to toggle source

Removes all the objects contained in the list and updates their reference counts.

@return [void]

Calls superclass method
# File lib/xcodeproj/project/object_list.rb, line 136
def clear
  objects.each do |object|
    perform_deletion_operations(object)
  end
  super
end
delete(object) click to toggle source

Removes an object to list and updates its references count.

@param [AbstractObject, ObjectDictionary] object

the object to delete from the list.

@return [AbstractObject, ObjectDictionary, Nil] the object if found.

Calls superclass method
# File lib/xcodeproj/project/object_list.rb, line 112
def delete(object)
  perform_deletion_operations(object)
  super
end
delete_at(index) click to toggle source

Removes the object at the given index from the list and updates its references count.

@param [Fixnum] from

The index of the object.

@return [AbstractObject, ObjectDictionary, Nil] the object if found.

Calls superclass method
# File lib/xcodeproj/project/object_list.rb, line 125
def delete_at(index)
  object = at(index)
  perform_deletion_operations(object)
  super
end
insert(index, object) click to toggle source

Adds an object to the given index of the list the and updates its references count.

@param [AbstractObject, ObjectDictionary] object

The object to add to the list.

@return [void]

Calls superclass method
# File lib/xcodeproj/project/object_list.rb, line 88
def insert(index, object)
  perform_additions_operations(object)
  super
end
move(object, new_index) click to toggle source

Moves the given object to the given index.

@param [AbstractObject, ObjectDictionary] object

The object to move.

@param [Fixnum] to

The new index for the object.

@return [void]

# File lib/xcodeproj/project/object_list.rb, line 153
def move(object, new_index)
  return if index(object) == new_index
  if obj = delete(object)
    insert(new_index, obj)
  else
    raise "Attempt to move object `#{object}` not present in the list `#{inspect}`"
  end
end
move_from(current_index, new_index) click to toggle source

Moves the object at the given index to the given position.

@param [Fixnum] from

The current index of the object.

@param [Fixnum] to

The new index for the object.

@return [void]

# File lib/xcodeproj/project/object_list.rb, line 172
def move_from(current_index, new_index)
  return if current_index == new_index
  if obj = delete_at(current_index)
    insert(new_index, obj)
  else
    raise "Attempt to move object from index `#{current_index}` which is beyond bounds of the list `#{inspect}`"
  end
end
objects() click to toggle source

@return [Array<AbstractObject>]

a new array generated with the objects contained in the list.
# File lib/xcodeproj/project/object_list.rb, line 45
def objects
  to_a
end
sort!() click to toggle source
Calls superclass method
# File lib/xcodeproj/project/object_list.rb, line 181
def sort!
  return super if owner.project.dirty?
  previous = to_a
  super
  owner.mark_project_as_dirty! unless previous == to_a
  self
end
unshift(object) click to toggle source

Prepends an object to the list and updates its references count.

@param [AbstractObject, ObjectDictionary] object

The object to add to the list.

@return [void]

Calls superclass method
# File lib/xcodeproj/project/object_list.rb, line 100
def unshift(object)
  perform_additions_operations(object)
  super
end
uuids() click to toggle source

@return [Array<String>]

the UUIDs of all the objects referenced by this list.
# File lib/xcodeproj/project/object_list.rb, line 38
def uuids
  map(&:uuid)
end

Private Instance Methods

perform_additions_operations(objects) click to toggle source

Informs an object that it was added to the list. In practice it adds the owner of the list as referrer to the objects. It also validates the value.

@return [void]

# File lib/xcodeproj/project/object_list.rb, line 200
def perform_additions_operations(objects)
  objects = [objects] unless objects.is_a?(Array)
  objects.each do |obj|
    owner.mark_project_as_dirty!
    obj.add_referrer(owner)
    attribute.validate_value(obj) unless obj.is_a?(ObjectDictionary)
  end
end
perform_deletion_operations(objects) click to toggle source

Informs an object that it was removed from to the list, so it can remove its owner from its referrers and take the appropriate actions.

@return [void]

# File lib/xcodeproj/project/object_list.rb, line 214
def perform_deletion_operations(objects)
  objects = [objects] unless objects.is_a?(Array)
  objects.each do |obj|
    owner.mark_project_as_dirty!
    obj.remove_referrer(owner) unless obj.is_a?(ObjectDictionary)
  end
end