/
has_many.rb
51 lines (45 loc) · 1.62 KB
/
has_many.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module Xcodeproj
class Project
module Object
class Association
class HasMany < Association
def direct_get
uuids = @owner.send(@reflection.attribute_getter)
if @block
# Evaluate the block, which was specified at the class level, in
# the instance’s context.
@owner.list_by_class(uuids, @reflection.klass) do |list|
list.let(:push) do |new_object|
@owner.instance_exec(new_object, &@block)
end
end
else
@owner.list_by_class(uuids, @reflection.klass)
end
end
def inverse_get
PBXObjectList.new(@reflection.klass, @owner.project) do |list|
list.let(:uuid_scope) do
@owner.project.objects.list_by_class(@reflection.klass).select do |object|
object.send(@reflection.inverse.attribute_getter) == @owner.uuid
end.map(&:uuid)
end
list.let(:push) do |new_object|
new_object.send(@reflection.inverse.attribute_setter, @owner.uuid)
end
end
end
# @todo Currently this does not call the @block, which means that
# in theory (like with a group's children) the object stays
# asociated with its old group.
def direct_set(list)
@owner.send(@reflection.attribute_setter, list.map(&:uuid))
end
def inverse_set(list)
raise NotImplementedError
end
end
end
end
end
end