Class: Vendor::XCode::Proxy::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/xcode/proxy/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Base

Returns a new instance of Base.



45
46
47
48
49
50
51
# File 'lib/vendor/xcode/proxy/base.rb', line 45

def initialize(options)
  @id = options[:id]
  @project = options[:project]
  @attributes = options[:attributes]

  @attributes['isa'] = self.class.name.split('::').last unless @attributes['isa']
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(v, *args) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vendor/xcode/proxy/base.rb', line 67

def method_missing(v, *args)
  setting = v.to_s.match(/\=$/)
  name = self.class.attribute_name(v)

  if @attributes.has_key?(name)
    if setting
      write_attribute(name, args.first)
    else
      read_attribute(name)
    end
  else
    super
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/vendor/xcode/proxy/base.rb', line 7

def attributes
  @attributes
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/vendor/xcode/proxy/base.rb', line 5

def id
  @id
end

#parentObject

Returns the value of attribute parent.



6
7
8
# File 'lib/vendor/xcode/proxy/base.rb', line 6

def parent
  @parent
end

Class Method Details

.attribute_name(name) ⇒ Object

References are stored in camel case in the project file



38
39
40
41
# File 'lib/vendor/xcode/proxy/base.rb', line 38

def attribute_name(name)
  camelized = name.to_s.gsub(/[^\w]/, '').gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  camelized[0].chr.downcase + camelized[1..-1]
end

.generate_idObject

I don’t know what the ID’s are made up of in XCode, so lets just generate a 24 character string.



13
14
15
# File 'lib/vendor/xcode/proxy/base.rb', line 13

def generate_id
  (0...24).map { '%02X' % rand(256) }.join
end

.object_referencesObject



17
18
19
# File 'lib/vendor/xcode/proxy/base.rb', line 17

def object_references
  @references || []
end

.reference(name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vendor/xcode/proxy/base.rb', line 21

def reference(name)
  attribute = attribute_name(name)

  define_method name do
    value = @attributes[attribute]
    if value.kind_of?(Array)
      value.map { |id| @project.find_object(id) }
    else
      @project.find_object(value)
    end
  end

  @references ||= []
  @references << name
end

Instance Method Details

#file?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/vendor/xcode/proxy/base.rb', line 108

def file?
  false
end

#group?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/vendor/xcode/proxy/base.rb', line 104

def group?
  false
end

#inspectObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/vendor/xcode/proxy/base.rb', line 53

def inspect
  properties = { "id" => @id }.merge(@attributes)

  # The class name contains the ISA (the type of the object)
  properties.delete("isa")

  # Always show the ID first
  keys = properties.keys.map(&:to_s)
  keys.delete("id")
  keys.unshift("id")

  "#<#{self.class.name} #{keys.map{ |key| "#{underscore(key)}: #{properties[key].inspect}" }.join(', ')}>"
end

#read_attribute(name) ⇒ Object



96
97
98
# File 'lib/vendor/xcode/proxy/base.rb', line 96

def read_attribute(name)
  @attributes[name.to_s]
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
# File 'lib/vendor/xcode/proxy/base.rb', line 82

def respond_to?(method)
  name = self.class.attribute_name(method)

  if @attributes.has_key?(name)
    true
  else
    super
  end
end

#to_ascii_plistObject



100
101
102
# File 'lib/vendor/xcode/proxy/base.rb', line 100

def to_ascii_plist
  @attributes.to_ascii_plist
end

#write_attribute(name, value) ⇒ Object



92
93
94
# File 'lib/vendor/xcode/proxy/base.rb', line 92

def write_attribute(name, value)
  @attributes[name.to_s] = value
end