Commit f2e8783a authored by Eloy Duran's avatar Eloy Duran

stash

parent 2b4e6250
...@@ -8,6 +8,58 @@ module Pod ...@@ -8,6 +8,58 @@ module Pod
module Xcode module Xcode
class Project class Project
class PBXObject class PBXObject
class AssociationReflection
def initialize(name, options)
@name, @options = name.to_s, options
end
attr_reader :name, :options
def klass
@options[:class] ||= Project.const_get("PBX#{@name.classify}")
end
def uuids_on_target?
!!@options[:uuids]
end
def uuid_method_name
@options[:uuid] || @options[:uuids].to_s.singularize
end
def uuid_getter
uuid_method_name
end
def uuid_setter
"#{uuid_method_name}="
end
def uuids_method_name
@options[:uuids] || @options[:uuid].to_s.pluralize
end
def uuids_getter
uuids_method_name
end
def uuids_setter
"#{uuids_method_name}="
end
end
def self.reflections
@reflections ||= []
end
def self.create_reflection(name, options)
(reflections << AssociationReflection.new(name, options)).last
end
def self.reflection(name)
reflections.find { |r| r.name.to_s == name.to_s }
end
def self.attribute(attribute_name, accessor_name = nil) def self.attribute(attribute_name, accessor_name = nil)
attribute_name = attribute_name.to_s attribute_name = attribute_name.to_s
name = (accessor_name || attribute_name).to_s name = (accessor_name || attribute_name).to_s
...@@ -20,28 +72,28 @@ module Pod ...@@ -20,28 +72,28 @@ module Pod
end end
def self.has_many(plural_attr_name, options = {}, &block) def self.has_many(plural_attr_name, options = {}, &block)
klass = options[:class] || Project.const_get("PBX#{plural_attr_name.to_s.classify}") reflection = create_reflection(plural_attr_name, options)
if options[:fkey_on_target] if options[:inverse_of]
define_method(plural_attr_name) do define_method(reflection.name) do
scoped = @project.objects.select_by_class(klass).select do |object| scoped = @project.objects.select_by_class(reflection.klass).select do |object|
object.send(options[:uuid]) == self.uuid object.send(reflection.uuid_getter) == self.uuid
end end
PBXObjectList.new(klass, @project, scoped) do |object| PBXObjectList.new(reflection.klass, @project, scoped) do |object|
object.send("#{options[:uuid]}=", self.uuid) object.send(reflection.uuid_setter, self.uuid)
end end
end end
else else
singular_attr_name = plural_attr_name.to_s.singularize singular_attr_name = plural_attr_name.to_s.singularize
uuid_list_name = "#{singular_attr_name}References" uuid_list_name = "#{singular_attr_name}References"
attribute(plural_attr_name, uuid_list_name) attribute(reflection.name, uuid_list_name)
define_method(plural_attr_name) do define_method(reflection.name) do
uuids = send(uuid_list_name) uuids = send(uuid_list_name)
if block if block
list_by_class(uuids, klass) do |object| list_by_class(uuids, reflection.klass) do |object|
instance_exec(object, &block) instance_exec(object, &block)
end end
else else
list_by_class(uuids, klass) list_by_class(uuids, reflection.klass)
end end
end end
define_method("#{plural_attr_name}=") do |objects| define_method("#{plural_attr_name}=") do |objects|
...@@ -51,33 +103,32 @@ module Pod ...@@ -51,33 +103,32 @@ module Pod
end end
def self.belongs_to(singular_attr_name, options = {}) def self.belongs_to(singular_attr_name, options = {})
if uuid_name = options[:uuids] reflection = create_reflection(singular_attr_name, options)
# part of another objects uuid list if reflection.uuids_on_target?
klass = options[:class] || Project.const_get("PBX#{singular_attr_name.to_s.classify}") define_method(reflection.name) do
define_method(singular_attr_name) do
# Loop over all objects of the class and find the one that includes # Loop over all objects of the class and find the one that includes
# this object in the specified uuid list. # this object in the specified uuid list.
@project.objects.select_by_class(klass).find do |object| @project.objects.select_by_class(reflection.klass).find do |object|
object.send(uuid_name).include?(self.uuid) object.send(reflection.uuids_getter).include?(self.uuid)
end end
end end
define_method("#{singular_attr_name}=") do |object| define_method("#{reflection.name}=") do |object|
# Remove this object from the uuid list of the target # Remove this object from the uuid list of the target
# that this object was associated to. # that this object was associated to.
if previous = send(singular_attr_name) if previous = send(reflection.name)
previous.send(uuid_name).delete(self.uuid) previous.send(reflection.uuids_getter).delete(self.uuid)
end end
# Now assign this object to the new object # Now assign this object to the new object
object.send(uuid_name) << self.uuid object.send(reflection.uuids_getter) << self.uuid
end end
else else
uuid_name = options[:uuid] || "#{singular_attr_name}Reference" uuid_name = options[:uuid] || "#{reflection.name}Reference"
attribute(options[:uuid] || singular_attr_name, uuid_name) attribute(options[:uuid] || reflection.name, uuid_name)
define_method(singular_attr_name) do define_method(reflection.name) do
uuid = send(uuid_name) uuid = send(uuid_name)
@project.objects[uuid] @project.objects[uuid]
end end
define_method("#{singular_attr_name}=") do |object| define_method("#{reflection.name}=") do |object|
send("#{uuid_name}=", object.uuid) send("#{uuid_name}=", object.uuid)
end end
end end
...@@ -150,7 +201,7 @@ module Pod ...@@ -150,7 +201,7 @@ module Pod
class PBXFileReference < PBXObject class PBXFileReference < PBXObject
attributes :path, :sourceTree, :explicitFileType, :includeInIndex attributes :path, :sourceTree, :explicitFileType, :includeInIndex
has_many :buildFiles, :uuid => :fileRef, :fkey_on_target => true has_many :buildFiles, :uuid => :fileRef, :inverse_of => :file
belongs_to :group, :uuids => :childReferences belongs_to :group, :uuids => :childReferences
def initialize(project, uuid, attributes) def initialize(project, uuid, attributes)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment