class Xcodeproj::Project::ObjectDictionary

This class represents relationships to other objects stored in a Dictionary.

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 To provide full support as the other classes the dictionary should:

Give the following attribute:

     has_many_references_by_keys :project_references, {
       :project_ref   => PBXFileReference,
       :product_group => PBXGroup
     }

This should be possible:

     #=> Note the API:
     root_object.project_references.project_ref = file

     #=> This should raise:
     root_object.project_references.product_group = file

I.e. generate setters and getters from the specification hash.

Also the interface is a dirty hybrid between the
{AbstractObjectAttribute} and the {ObjectList}.

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

which are overridden to inform objects reference count. Ideally all
the hash 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 This class should use a {Hash} as a backing store instead of

inheriting from it. This would prevent the usage of methods which
don't notify the objects.

Attributes

attribute[R]

@return [Object::AbstractObjectAttribute] The attribute that generated

the list.
owner[R]

@return [Object] The object that owns the list.

Public Class Methods

new(attribute, owner) click to toggle source

@param [Object::AbstractObjectAttribute] attribute @see attribute @param [Object] owner @see owner

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

Public Instance Methods

[]=(key, object) click to toggle source

Associates an object to the given key and updates its references count.

@param [String] key

The key.

@param [AbstractObject] object

The object to add to the dictionary.

@return [AbstractObject] The given object.

Calls superclass method
# File lib/xcodeproj/project/object_dictionary.rb, line 86
def []=(key, object)
  key = normalize_key(key)
  if object
    perform_additions_operations(object, key)
  else
    perform_deletion_operations(self[key])
  end
  super(key, object)
end
add_referrer(referrer) click to toggle source

Informs the objects contained in the dictionary that another object is referencing them.

# File lib/xcodeproj/project/object_dictionary.rb, line 153
def add_referrer(referrer)
  values.each { |obj| obj.add_referrer(referrer) }
end
allowed_keys() click to toggle source

@return [Array<Symbol>] The list of the allowed keys.

# File lib/xcodeproj/project/object_dictionary.rb, line 62
def allowed_keys
  attribute.classes_by_key.keys
end
delete(key) click to toggle source

Removes the given key from the dictionary and informs the object that is not longer referenced by the owner.

@param [String] key

The key.
Calls superclass method
# File lib/xcodeproj/project/object_dictionary.rb, line 102
def delete(key)
  key = normalize_key(key)
  object = self[key]
  perform_deletion_operations(object)
  super
end
inspect() click to toggle source

@return [String] A string suitable for debugging.

# File lib/xcodeproj/project/object_dictionary.rb, line 68
def inspect
  "<ObjectDictionary attribute:`#{@attribute.name}` " \
    "owner:`#{@owner.display_name}` values:#{super.inspect}>"
end
remove_reference(object) click to toggle source

Removes all the references to a given object.

# File lib/xcodeproj/project/object_dictionary.rb, line 146
def remove_reference(object)
  each { |key, obj| self[key] = nil if obj == object }
end
remove_referrer(referrer) click to toggle source

Informs the objects contained in the dictionary that another object stopped referencing them.

# File lib/xcodeproj/project/object_dictionary.rb, line 160
def remove_referrer(referrer)
  values.each { |obj| obj.remove_referrer(referrer) }
end
to_ascii_plist() click to toggle source
# File lib/xcodeproj/project/object_dictionary.rb, line 126
def to_ascii_plist
  to_hash
end
to_hash() click to toggle source

@return [Hash<String => String>] The plist representation of the

dictionary where the objects are replaced by their UUIDs.
# File lib/xcodeproj/project/object_dictionary.rb, line 115
def to_hash
  result = {}
  each do |key, obj|
    if obj
      plist_key = Object::CaseConverter.convert_to_plist(key, nil)
      result[plist_key] = Nanaimo::String.new(obj.uuid, obj.ascii_plist_annotation)
    end
  end
  result
end
to_tree_hash() click to toggle source

@return [Hash<String => String>] Returns a cascade representation of

the object without UUIDs.
# File lib/xcodeproj/project/object_dictionary.rb, line 133
def to_tree_hash
  result = {}
  each do |key, obj|
    if obj
      plist_key = Object::CaseConverter.convert_to_plist(key, nil)
      result[plist_key] = obj.to_tree_hash
    end
  end
  result
end

Private Instance Methods

normalize_key(key) click to toggle source

@return [Symbol] Normalizes a key to a symbol converting the camel case

format with underscores.

@param [String, Symbol] key

The key to normalize.
# File lib/xcodeproj/project/object_dictionary.rb, line 175
def normalize_key(key)
  if key.is_a?(String)
    key = Object::CaseConverter.convert_to_ruby(key)
  end

  unless allowed_keys.include?(key)
    raise "[Xcodeproj] Unsupported key `#{key}` (allowed " \
      "`#{allowed_keys}`) for `#{inspect}`"
  end
  key
end
perform_additions_operations(object, key) click to toggle source

Informs an object that it was added to the dictionary. 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_dictionary.rb, line 193
def perform_additions_operations(object, key)
  owner.mark_project_as_dirty!
  object.add_referrer(owner)
  attribute.validate_value_for_key(object, key)
end
perform_deletion_operations(objects) click to toggle source

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

@return [void]

# File lib/xcodeproj/project/object_dictionary.rb, line 204
def perform_deletion_operations(objects)
  owner.mark_project_as_dirty!
  objects.remove_referrer(owner)
end