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


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)


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

def attribute
  @attribute
end

#ownerObject (readonly)


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.


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.


149
150
151
# File 'lib/xcodeproj/project/object_dictionary.rb', line 149

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

#allowed_keysArray<Symbol>


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.


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


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.


142
143
144
# File 'lib/xcodeproj/project/object_dictionary.rb', line 142

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.


156
157
158
# File 'lib/xcodeproj/project/object_dictionary.rb', line 156

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

#to_hashHash<String => String>


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] = obj.uuid
    end
  end
  result
end

#to_tree_hashHash<String => String>


129
130
131
132
133
134
135
136
137
138
# File 'lib/xcodeproj/project/object_dictionary.rb', line 129

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