Class: Xcodeproj::Project::Object::AbstractObject Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/xcodeproj/project/object.rb,
lib/xcodeproj/project/object_attributes.rb

Overview

This class is abstract.

This is the base class of all object types that can exist in a Xcode project. As such it provides common behavior, but you can only use instances of subclasses of AbstractObject, because this class does not exist in actual Xcode projects.

Almost all the methods implemented by this class are not expected to be used by Xcodeproj clients.

Subclasses should clearly identify which methods reflect the xcodeproj document model and which methods are offered as convenience. Object lists always represent a relationship to many of the model while simple arrays represent dynamically generated values offered a convenience for clients.

AbstractObject collapse

AbstractObject collapse

Plist related methods collapse

Object methods collapse

Instance Attribute Details

#isaString (readonly)

Returns the object's class name.

Returns:

  • (String)

    the object's class name.


45
46
47
# File 'lib/xcodeproj/project/object.rb', line 45

def isa
  @isa
end

#projectProject (readonly)

Returns the project that owns the object.

Returns:

  • (Project)

    the project that owns the object.


91
92
93
# File 'lib/xcodeproj/project/object.rb', line 91

def project
  @project
end

#uuidString (readonly)

Returns the object universally unique identifier.

Returns:

  • (String)

    the object universally unique identifier.


87
88
89
# File 'lib/xcodeproj/project/object.rb', line 87

def uuid
  @uuid
end

Class Method Details

.isaString

Returns the ISA of the class.

Returns:

  • (String)

    the ISA of the class.


39
40
41
# File 'lib/xcodeproj/project/object.rb', line 39

def self.isa
  @isa ||= name.split('::').last
end

Instance Method Details

#<=>(other) ⇒ Object


487
488
489
# File 'lib/xcodeproj/project/object.rb', line 487

def <=>(other)
  uuid <=> other.uuid
end

#==(other) ⇒ Object


483
484
485
# File 'lib/xcodeproj/project/object.rb', line 483

def ==(other)
  other.is_a?(AbstractObject) && to_hash == other.to_hash
end

#ascii_plist_annotationObject


416
417
418
# File 'lib/xcodeproj/project/object.rb', line 416

def ascii_plist_annotation
  " #{display_name} "
end

#display_nameString Also known as: to_s

Note:

Not all concrete classes implement the name attribute and this method prevents from overriding it in plist.

Returns the value of the name attribute or returns a generic name for the object.

Returns:

  • (String)

    a name for the object.


133
134
135
136
137
138
139
140
# File 'lib/xcodeproj/project/object.rb', line 133

def display_name
  declared_name = name if self.respond_to?(:name)
  if declared_name && !declared_name.empty?
    declared_name
  else
    isa.gsub(/^(PBX|XC)/, '')
  end
end

#inspectObject


491
492
493
494
495
496
# File 'lib/xcodeproj/project/object.rb', line 491

def inspect
  optional = ''
  optional << " name=`#{name}`" if respond_to?(:name) && name
  optional << " path=`#{path}`" if respond_to?(:path) && path
  "<#{isa}#{optional} UUID=`#{uuid}`>"
end

#nested_object_for_hash(object, method) ⇒ Object


407
408
409
410
411
412
413
414
# File 'lib/xcodeproj/project/object.rb', line 407

def nested_object_for_hash(object, method)
  case method
  when :to_ascii_plist
    Nanaimo::String.new(object.uuid, object.ascii_plist_annotation)
  else
    object.uuid
  end
end

#pretty_printHash{String => Hash}

Returns A hash suitable to display the object to the user.

Returns:

  • (Hash{String => Hash})

    A hash suitable to display the object to the user.


468
469
470
471
472
473
474
475
# File 'lib/xcodeproj/project/object.rb', line 468

def pretty_print
  if to_many_attributes.count == 1
    children = to_many_attributes.first.get_value(self)
    { display_name => children.map(&:pretty_print) }
  else
    display_name
  end
end

#remove_from_projectvoid

Note:

The root object is owned by the project and should not be manipulated with this method.

This method returns an undefined value.

Removes the object from the project by asking to its referrers to remove the reference to it.


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/xcodeproj/project/object.rb', line 101

def remove_from_project
  mark_project_as_dirty!
  project.objects_by_uuid.delete(uuid)

  referrers.dup.each do |referrer|
    referrer.remove_reference(self)
  end

  to_one_attributes.each do |attrb|
    object = attrb.get_value(self)
    object.remove_referrer(self) if object
  end

  to_many_attributes.each do |attrb|
    list = attrb.get_value(self)
    list.clear
  end

  unless referrers.count == 0
    raise "[Xcodeproj] BUG: #{self} should have no referrers instead" \
      "the following objects are still referencing it #{referrers}"
  end
end

#sort(_options = nil) ⇒ Object

Sorts the to many attributes of the object according to the display name.


146
147
148
149
150
151
152
153
# File 'lib/xcodeproj/project/object.rb', line 146

def sort(_options = nil)
  to_many_attributes.each do |attrb|
    list = attrb.get_value(self)
    list.sort! do |x, y|
      x.display_name.downcase <=> y.display_name.downcase
    end
  end
end

#sort_recursively(options = nil) ⇒ Object

Note:

Some objects may in turn refer back to objects higher in the object tree, which will lead to stack level deep errors. These objects should *not* try to perform a recursive sort, also because these objects would get sorted through other paths in the tree anyways.

At the time of writing the only known case is `PBXTargetDependency`.

Sorts the object and the objects that it references.

Parameters:

  • options (Hash) (defaults to: nil)

    the sorting options.

Options Hash (options):

  • :groups_position (Symbol)

    the position of the groups can be either `:above` or `:below`.


172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/xcodeproj/project/object.rb', line 172

def sort_recursively(options = nil)
  to_one_attributes.each do |attrb|
    value = attrb.get_value(self)
    value.sort_recursively(options) if value
  end

  to_many_attributes.each do |attrb|
    list = attrb.get_value(self)
    list.each { |entry| entry.sort_recursively(options) }
  end

  sort(options)
end

#to_ascii_plistObject


420
421
422
# File 'lib/xcodeproj/project/object.rb', line 420

def to_ascii_plist
  Nanaimo::Dictionary.new(to_hash_as(:to_ascii_plist), ascii_plist_annotation)
end

#to_hashHash

Note:

the key for simple and to_one attributes usually appears only if there is a value. To-many keys always appear with an empty array.

Returns a cascade representation of the object with UUIDs.

Returns:

  • (Hash)

    a hash representation of the project.


375
376
377
# File 'lib/xcodeproj/project/object.rb', line 375

def to_hash
  to_hash_as
end