Class: Xcodeproj::Project::ObjectDictionary
- Inherits:
-
Hash
- Object
- Hash
- Xcodeproj::Project::ObjectDictionary
- Defined in:
- lib/xcodeproj/project/object_dictionary.rb
Overview
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.
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.
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
-
#attribute ⇒ Object::AbstractObjectAttribute
readonly
The attribute that generated the list.
-
#owner ⇒ Object
readonly
The object that owns the list.
Notification enabled methods collapse
-
#[]=(key, object) ⇒ AbstractObject
Associates an object to the given key and updates its references count.
-
#delete(key) ⇒ Object
Removes the given key from the dictionary and informs the object that is not longer referenced by the owner.
AbstractObject Methods collapse
-
#add_referrer(referrer) ⇒ Object
Informs the objects contained in the dictionary that another object is referencing them.
-
#remove_reference(object) ⇒ Object
Removes all the references to a given object.
-
#remove_referrer(referrer) ⇒ Object
Informs the objects contained in the dictionary that another object stopped referencing them.
- #to_ascii_plist ⇒ Object
-
#to_hash ⇒ Hash<String => String>
The plist representation of the dictionary where the objects are replaced by their UUIDs.
-
#to_tree_hash ⇒ Hash<String => String>
Returns a cascade representation of the object without UUIDs.
Instance Method Summary collapse
-
#allowed_keys ⇒ Array<Symbol>
The list of the allowed keys.
-
#initialize(attribute, owner) ⇒ ObjectDictionary
constructor
A new instance of ObjectDictionary.
-
#inspect ⇒ String
A string suitable for debugging.
Constructor Details
#initialize(attribute, owner) ⇒ ObjectDictionary
Returns a new instance of 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
#attribute ⇒ Object::AbstractObjectAttribute (readonly)
Returns The attribute that generated the list.
54 55 56 |
# File 'lib/xcodeproj/project/object_dictionary.rb', line 54 def attribute @attribute end |
#owner ⇒ Object (readonly)
Returns 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.
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_keys ⇒ Array<Symbol>
Returns 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.
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 |
#inspect ⇒ String
Returns 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_plist ⇒ Object
126 127 128 |
# File 'lib/xcodeproj/project/object_dictionary.rb', line 126 def to_ascii_plist to_hash end |
#to_hash ⇒ Hash<String => String>
Returns 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_hash ⇒ 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 |