Class: Xcodeproj::Project::ObjectDictionary

Inherits:
Hash
  • Object
show all
Defined in:
lib/xcodeproj/project/object_dictionary.rb

Overview

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.

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 Xcodeproj::Project::Object::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.

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

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

Instance Attribute Summary collapse

Notification enabled methods collapse

AbstractObject Methods collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute, owner) ⇒ ObjectDictionary

Returns a new instance of ObjectDictionary.

Parameters:


46
47
48
49
# File 'lib/xcodeproj/project/object_dictionary.rb', line 46

def initialize(attribute, owner)
  @attribute = attribute
  @owner = owner
end

Instance Attribute Details

#attributeObject::AbstractObjectAttribute (readonly)

Returns The attribute that generated the list.

Returns:


54
55
56
# File 'lib/xcodeproj/project/object_dictionary.rb', line 54

def attribute
  @attribute
end

#ownerObject (readonly)

Returns The object that owns the list.

Returns:

  • (Object)

    The object that owns the list.


58
59
60
# File 'lib/xcodeproj/project/object_dictionary.rb', line 58

def owner
  @owner
end

Instance Method Details

#[]=(key, object) ⇒ AbstractObject

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

Parameters:

  • key (String)

    The key.

  • object (AbstractObject)

    The object to add to the dictionary.

Returns:


86
87
88
89
90
91
92
93
94
# 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) ⇒ Object

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


153
154
155
# File 'lib/xcodeproj/project/object_dictionary.rb', line 153

def add_referrer(referrer)
  values.each { |obj| obj.add_referrer(referrer) }
end

#allowed_keysArray<Symbol>

Returns The list of the allowed keys.

Returns:

  • (Array<Symbol>)

    The list of the allowed keys.


62
63
64
# File 'lib/xcodeproj/project/object_dictionary.rb', line 62

def allowed_keys
  attribute.classes_by_key.keys
end

#delete(key) ⇒ Object

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

Parameters:

  • key (String)

    The key.


102
103
104
105
106
107
# 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

#inspectString

Returns A string suitable for debugging.

Returns:

  • (String)

    A string suitable for debugging.


68
69
70
71
# 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) ⇒ Object

Removes all the references to a given object.


146
147
148
# 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) ⇒ Object

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


160
161
162
# File 'lib/xcodeproj/project/object_dictionary.rb', line 160

def remove_referrer(referrer)
  values.each { |obj| obj.remove_referrer(referrer) }
end

#to_ascii_plistObject


126
127
128
# File 'lib/xcodeproj/project/object_dictionary.rb', line 126

def to_ascii_plist
  to_hash
end

#to_hashHash<String => String>

Returns The plist representation of the dictionary where the objects are replaced by their UUIDs.

Returns:

  • (Hash<String => String>)

    The plist representation of the dictionary where the objects are replaced by their UUIDs.


115
116
117
118
119
120
121
122
123
124
# 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_hashHash<String => String>

Returns a cascade representation of the object without UUIDs.

Returns:

  • (Hash<String => String>)

    Returns a cascade representation of the object without UUIDs.


133
134
135
136
137
138
139
140
141
142
# 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