This repository has been archived by the owner on Apr 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
/
persistence_state.rb
75 lines (56 loc) · 1.65 KB
/
persistence_state.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module DataMapper
module Resource
# the state of the resource (abstract)
class PersistenceState
extend Equalizer
equalize :resource
attr_reader :resource
def initialize(resource)
@resource = resource
@model = resource.model
end
def get(subject, *args)
subject.get(resource, *args)
end
def set(subject, value)
subject.set(resource, value)
self
end
def delete
raise NotImplementedError, "#{self.class}#delete should be implemented"
end
def commit
raise NotImplementedError, "#{self.class}#commit should be implemented"
end
def rollback
raise NotImplementedError, "#{self.class}#rollback should be implemented"
end
private
attr_reader :model
def properties
@properties ||= model.properties(repository.name)
end
def relationships
@relationships ||= model.relationships(repository.name)
end
def identity_map
@identity_map ||= repository.identity_map(model)
end
def remove_from_identity_map
identity_map.delete(resource.key)
end
def add_to_identity_map
identity_map[resource.key] = resource
end
def set_child_keys
relationships.each do |relationship|
set_child_key(relationship)
end
end
def set_child_key(relationship)
return unless relationship.loaded?(resource) && relationship.respond_to?(:resource_for)
set(relationship, get(relationship))
end
end # class PersistenceState
end # module Resource
end # module DataMapper