Class: ZergXcode::XcodeObject

Inherits:
Object
  • Object
show all
Defined in:
lib/zerg_xcode/objects/xcode_object.rb

Overview

Xcode objects

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ XcodeObject

Inheritance based on isa



18
19
20
21
22
23
24
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 18

def initialize(hash)
  @attrs = hash
  if self.class != ZergXcode::XcodeObject
    class_name = self.class.name
    @attrs['isa'] ||= class_name[(class_name.rindex(':') + 1)..-1]
  end
end

Instance Attribute Details

#archive_idObject

Returns the value of attribute archive_id.



4
5
6
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 4

def archive_id
  @archive_id
end

#versionObject

Returns the value of attribute version.



3
4
5
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 3

def version
  @version
end

Class Method Details

.from(object_or_hash) ⇒ Object

Deep copy



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 148

def self.from(object_or_hash)
  new_object = case object_or_hash
  when ZergXcode::XcodeObject
    object_or_hash.shallow_copy
  else
    self.new object_or_hash.dup
  end
  object_map = { object_or_hash => new_object }
  
  new_object.visit do |object, parent, key, value|
    case value
    when Hash, Array
      next value.dup
    when ZergXcode::XcodeObject
      if object_map[value]
        parent[key] = object_map[value]
        next false
      else
        object_map[value] = value.shallow_copy
        next object_map[value]
      end
    else
      next true
    end
  end
end

.new(*args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 26

def self.new(*args)
  return super unless self == ZergXcode::XcodeObject
  if hash_isa = args.first['isa']
    classes = ZergXcode::Objects.constants
    if classes.include? hash_isa
      return ZergXcode::Objects.const_get(hash_isa).new(*args)
    end
  end
  super
end

Instance Method Details

#[](key) ⇒ Object

Hash-like behavior



8
9
10
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 8

def [](key)
  @attrs[key]
end

#[]=(key, value) ⇒ Object



12
13
14
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 12

def []=(key, value)
  @attrs[key] = value
end

#_attr_hashObject

The (internal) hash holding the file’s attributes.



45
46
47
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 45

def _attr_hash
  @attrs
end

#attrsObject

The names of the object’s attributes.



40
41
42
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 40

def attrs
  @attrs.keys
end

#copy_metadata(source) ⇒ Object



181
182
183
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 181

def (source)
  self.archive_id, self.version = source.archive_id, source.version 
end

#isaObject

Object type helpers



50
51
52
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 50

def isa
  return @attrs['isa'].to_sym
end

#shallow_copyObject



175
176
177
178
179
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 175

def shallow_copy
  new_object = self.class.new @attrs.dup
  new_object. self
  return new_object
end

#visit(&accept) ⇒ Object

Visits an object’s internal structure.

The given block is called like this:

yield object, parent, key, value

Where

object: the object currently visited (can be a sub-object)
parent: the collection currently visited (an object, hash, or array)
key: 
value:

The block can return

false: no recursive visiting for the given value
true: normal recursive visiting for the given value
something else: replace the given value with the return, the recursive
                visiting is done on the new value


70
71
72
73
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 70

def visit(&accept)
  visit_hash(@attrs, &accept)
  self
end

#visit_array(array, &accept) ⇒ Object



92
93
94
95
96
97
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 92

def visit_array(array, &accept)
  array.each_with_index do |value, index|
    visit_value(array, index, value, &accept)
  end
  array
end

#visit_hash(hash, &accept) ⇒ Object



85
86
87
88
89
90
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 85

def visit_hash(hash, &accept)
  hash.each_key do |key|
    visit_value(hash, key, hash[key], &accept)
  end
  hash
end

#visit_once(&accept) ⇒ Object

Convenience method mapping over visit and exploring each object once.



76
77
78
79
80
81
82
83
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 76

def visit_once(&accept)
  visited = Set.new([self])
  self.visit do |object, parent, key, value|
    visited << object
    next_value = yield object, parent, key, value
    visited.include?(value) ? false : next_value
  end
end

#visit_value(parent, key, value, &accept) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 99

def visit_value(parent, key, value, &accept)
  visit_parent = (parent == @attrs) ? self : parent
  recurse = yield self, visit_parent, key, value
  return if recurse == false
  
  if recurse != true
    value = recurse
    parent[key] = recurse
  end
  
  case value
  when ZergXcode::XcodeObject
    value.visit(&accept)
  when Hash
    visit_hash(value, &accept)
  when Array
    visit_array(value, &accept)
  end
  value    
end

#xref_keyObject

Key used for cross-referencing objects in different graphs. If two objects in different graphs have the same key, it is very likely that they represent the same entity.



125
126
127
128
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 125

def xref_key
  # If the object doesn't have a merge name, use its (unique) object_id.
  [isa, xref_name || object_id]
end

#xref_nameObject

Name used in referencing an object.

An object’s name should be unique among objects in the same context. For instance, objects in the same array (e.g. ‘children’ in a PBXGroup) should have distinct names.



135
136
137
138
139
140
141
142
143
144
# File 'lib/zerg_xcode/objects/xcode_object.rb', line 135

def xref_name
  # Do not use this to override xref_name for specific objects. Only use
  # it for object families.
  case isa.to_s
  when /BuildPhase$/
    isa.to_s
  else
    self['name'] || self['explicitPath'] || self['path'] 
  end
end