/
lazy_association.rb
57 lines (46 loc) · 928 Bytes
/
lazy_association.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
51
52
53
54
55
56
57
module Chassis
class Repo
class LazyAssociation < Proxy
def initialize(repo, id)
@repo, @id = repo, id
end
def id
@id
end
def class
@repo.object_class
end
def repo
@repo
end
def instance_of?(klass)
self.class == klass
end
def is_a?(klass)
instance_of?(klass) || self.class > klass
end
def kind_of?(klass)
is_a? klass
end
def ==(other)
if other.instance_of? self.class
other.id == id
else
other.repo == repo && other.id == id
end
end
def eql?(other)
self == other
end
def materialize
@object ||= repo.find id
end
def __getobj__
materialize
end
def inspect
"#<LazyAssociation:#{object_id} @repo=#{repo} @id=#{id}>"
end
end
end
end