/
has_many.rb
201 lines (171 loc) · 4.42 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
require 'hanami/model/types'
module Hanami
module Model
module Associations
# One-To-Many association
#
# @since 0.7.0
# @api private
class HasMany
# @since 0.7.0
# @api private
def self.schema_type(entity)
type = Sql::Types::Schema::AssociationType.new(entity)
Types::Strict::Array.member(type)
end
# @since 0.7.0
# @api private
attr_reader :repository
# @since 0.7.0
# @api private
attr_reader :source
# @since 0.7.0
# @api private
attr_reader :target
# @since 0.7.0
# @api private
attr_reader :subject
# @since 0.7.0
# @api private
attr_reader :scope
# @since 0.7.0
# @api private
def initialize(repository, source, target, subject, scope = nil)
@repository = repository
@source = source
@target = target
@subject = subject.to_hash unless subject.nil?
@scope = scope || _build_scope
freeze
end
# @since 0.7.0
# @api private
def create(data)
entity.new(command(:create, aggregate(target), use: [:timestamps])
.call(data))
rescue => e
raise Hanami::Model::Error.for(e)
end
# @since 0.7.0
# @api private
def add(data)
command(:create, relation(target), use: [:timestamps])
.call(associate(data))
rescue => e
raise Hanami::Model::Error.for(e)
end
# @since 0.7.0
# @api private
def remove(id)
command(:update, relation(target), use: [:timestamps])
.by_pk(id)
.call(unassociate)
end
# @since 0.7.0
# @api private
def delete
scope.delete
end
# @since 0.7.0
# @api private
def each(&blk)
scope.each(&blk)
end
# @since 0.7.0
# @api private
def map(&blk)
to_a.map(&blk)
end
# @since 0.7.0
# @api private
def to_a
scope.to_a
end
# @since 0.7.0
# @api private
def where(condition)
__new__(scope.where(condition))
end
# @since 0.7.0
# @api private
def count
scope.count
end
private
# @since 0.7.0
# @api private
def command(target, relation, options = {})
repository.command(target, relation, options)
end
# @since 0.7.0
# @api private
def entity
repository.class.entity
end
# @since 0.7.0
# @api private
def relation(name)
repository.relations[name]
end
# @since 0.7.0
# @api private
def aggregate(name)
repository.aggregate(name)
end
# @since 0.7.0
# @api private
def association(name)
relation(target).associations[name]
end
# @since 0.7.0
# @api private
def associate(data)
relation(source)
.associations[target]
.associate(container.relations, data, subject)
end
# @since 0.7.0
# @api private
def unassociate
{ foreign_key => nil }
end
# @since 0.7.0
# @api private
def container
repository.container
end
# @since 0.7.0
# @api private
def primary_key
association_keys.first
end
# @since 0.7.0
# @api private
def foreign_key
association_keys.last
end
# Returns primary key and foreign key
#
# @since 0.7.0
# @api private
def association_keys
relation(source)
.associations[target]
.__send__(:join_key_map, container.relations)
end
# @since 0.7.0
# @api private
def _build_scope
result = relation(target)
result = result.where(foreign_key => subject.fetch(primary_key)) unless subject.nil?
result.as(Model::MappedRelation.mapper_name)
end
# @since 0.7.0
# @api private
def __new__(new_scope)
self.class.new(repository, source, target, subject, new_scope)
end
end
end
end
end