This repository has been archived by the owner on Apr 17, 2018. It is now read-only.
/
relationship.rb
656 lines (570 loc) · 20.1 KB
/
relationship.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
module DataMapper
module Associations
# Base class for relationships. Each type of relationship
# (1 to 1, 1 to n, n to m) implements a subclass of this class
# with methods like get and set overridden.
class Relationship
include Extlib::Assertions
OPTIONS = [ :child_repository_name, :parent_repository_name, :child_key, :parent_key, :min, :max, :inverse, :reader_visibility, :writer_visibility ].to_set
# Relationship name
#
# @example for :parent association in
#
# class VersionControl::Commit
# # ...
#
# belongs_to :parent
# end
#
# name is :parent
#
# @api semipublic
attr_reader :name
# Options used to set up association of this relationship
#
# @example for :author association in
#
# class VersionControl::Commit
# # ...
#
# belongs_to :author, :model => 'Person'
# end
#
# options is a hash with a single key, :model
#
# @api semipublic
attr_reader :options
# ivar used to store collection of child options in source
#
# @example for :commits association in
#
# class VersionControl::Branch
# # ...
#
# has n, :commits
# end
#
# instance variable name for source will be @commits
#
# @api semipublic
attr_reader :instance_variable_name
# Repository from where child objects are loaded
#
# @api semipublic
attr_reader :child_repository_name
# Repository from where parent objects are loaded
#
# @api semipublic
attr_reader :parent_repository_name
# Minimum number of child objects for relationship
#
# @example for :cores association in
#
# class CPU::Multicore
# # ...
#
# has 2..n, :cores
# end
#
# minimum is 2
#
# @api semipublic
attr_reader :min
# Maximum number of child objects for
# relationship
#
# @example for :fouls association in
#
# class Basketball::Player
# # ...
#
# has 0..5, :fouls
# end
#
# maximum is 5
#
# @api semipublic
attr_reader :max
# Returns the visibility for the source accessor
#
# @return [Symbol]
# the visibility for the accessor added to the source
#
# @api semipublic
attr_reader :reader_visibility
# Returns the visibility for the source mutator
#
# @return [Symbol]
# the visibility for the mutator added to the source
#
# @api semipublic
attr_reader :writer_visibility
# Returns query options for relationship.
#
# For this base class, always returns query options
# has been initialized with.
# Overriden in subclasses.
#
# @api private
attr_reader :query
# Returns a hash of conditions that scopes query that fetches
# target object
#
# @return [Hash]
# Hash of conditions that scopes query
#
# @api private
def source_scope(source)
{ inverse => source }
end
# Creates and returns Query instance that fetches
# target resource(s) (ex.: articles) for given target resource (ex.: author)
#
# @api semipublic
def query_for(source, other_query = nil)
repository_name = relative_target_repository_name_for(source)
DataMapper.repository(repository_name).scope do
query = target_model.query.dup
query.update(self.query)
query.update(source_scope(source))
query.update(other_query) if other_query
query.update(:fields => query.fields | target_key)
end
end
# Returns model class used by child side of the relationship
#
# @return [Resource]
# Model for association child
#
# @api private
def child_model
@child_model ||= (@parent_model || Object).find_const(child_model_name)
rescue NameError
raise NameError, "Cannot find the child_model #{child_model_name} for #{parent_model_name} in #{name}"
end
# @api private
def child_model?
child_model
true
rescue NameError
false
end
# @api private
def child_model_name
@child_model ? child_model.name : @child_model_name
end
# Returns a set of keys that identify the target model
#
# @return [PropertySet]
# a set of properties that identify the target model
#
# @api semipublic
def child_key
return @child_key if defined?(@child_key)
repository_name = child_repository_name || parent_repository_name
properties = child_model.properties(repository_name)
@child_key = if @child_properties
child_key = properties.values_at(*@child_properties)
properties.class.new(child_key).freeze
else
properties.key
end
end
# Access Relationship#child_key directly
#
# @api private
alias relationship_child_key child_key
private :relationship_child_key
# Returns model class used by parent side of the relationship
#
# @return [Resource]
# Class of association parent
#
# @api private
def parent_model
@parent_model ||= (@child_model || Object).find_const(parent_model_name)
rescue NameError
raise NameError, "Cannot find the parent_model #{parent_model_name} for #{child_model_name} in #{name}"
end
# @api private
def parent_model?
parent_model
true
rescue NameError
false
end
# @api private
def parent_model_name
@parent_model ? parent_model.name : @parent_model_name
end
# Returns a set of keys that identify parent model
#
# @return [PropertySet]
# a set of properties that identify parent model
#
# @api private
def parent_key
return @parent_key if defined?(@parent_key)
repository_name = parent_repository_name || child_repository_name
properties = parent_model.properties(repository_name)
@parent_key = if @parent_properties
parent_key = properties.values_at(*@parent_properties)
properties.class.new(parent_key).freeze
else
properties.key
end
end
# Loads and returns "other end" of the association.
# Must be implemented in subclasses.
#
# @api semipublic
def get(resource, other_query = nil)
raise NotImplementedError, "#{self.class}#get not implemented"
end
# Gets "other end" of the association directly
# as @ivar on given resource. Subclasses usually
# use implementation of this class.
#
# @api semipublic
def get!(resource)
resource.instance_variable_get(instance_variable_name)
end
# Sets value of the "other end" of association
# on given resource. Must be implemented in subclasses.
#
# @api semipublic
def set(resource, association)
raise NotImplementedError, "#{self.class}#set not implemented"
end
# Sets "other end" of the association directly
# as @ivar on given resource. Subclasses usually
# use implementation of this class.
#
# @api semipublic
def set!(resource, association)
resource.instance_variable_set(instance_variable_name, association)
end
# Eager load the collection using the source as a base
#
# @param [Resource, Collection] source
# the source to query with
# @param [Query, Hash] query
# optional query to restrict the collection
#
# @return [Collection]
# the loaded collection for the source
#
# @api private
def eager_load(source, query = nil)
targets = source.model.all(query_for(source, query))
# FIXME: cannot associate targets to m:m collection yet
unless source.kind_of?(ManyToMany::Collection)
associate_targets(source, targets)
end
targets
end
# Checks if "other end" of association is loaded on given
# resource.
#
# @api semipublic
def loaded?(resource)
assert_kind_of 'resource', resource, source_model
resource.instance_variable_defined?(instance_variable_name)
end
# Test the resource to see if it is a valid target
#
# @param [Object] source
# the resource or collection to be tested
#
# @return [Boolean]
# true if the resource is valid
#
# @api semipulic
def valid?(value, negated = false)
case value
when Enumerable then valid_target_collection?(value, negated)
when Resource then valid_target?(value)
when nil then true
else
raise ArgumentError, "+value+ should be an Enumerable, Resource or nil, but was a #{value.class.name}"
end
end
# Compares another Relationship for equality
#
# @param [Relationship] other
# the other Relationship to compare with
#
# @return [Boolean]
# true if they are equal, false if not
#
# @api public
def eql?(other)
return true if equal?(other)
instance_of?(other.class) && cmp?(other, :eql?)
end
# Compares another Relationship for equivalency
#
# @param [Relationship] other
# the other Relationship to compare with
#
# @return [Boolean]
# true if they are equal, false if not
#
# @api public
def ==(other)
return true if equal?(other)
return false if kind_of_inverse?(other)
other.respond_to?(:cmp_repository?, true) &&
other.respond_to?(:cmp_model?, true) &&
other.respond_to?(:cmp_key?, true) &&
other.respond_to?(:query) &&
cmp?(other, :==)
end
# Get the inverse relationship from the target model
#
# @api semipublic
def inverse
return @inverse if defined?(@inverse)
if kind_of_inverse?(options[:inverse])
return @inverse = options[:inverse]
end
relationships = target_model.relationships(relative_target_repository_name).values
@inverse = relationships.detect { |relationship| inverse?(relationship) } ||
invert
@inverse.child_key
@inverse
end
# @api private
def relative_target_repository_name
target_repository_name || source_repository_name
end
# @api private
def relative_target_repository_name_for(source)
target_repository_name || if source.respond_to?(:repository)
source.repository.name
else
source_repository_name
end
end
private
# @api private
attr_reader :child_properties
# @api private
attr_reader :parent_properties
# Initializes new Relationship: sets attributes of relationship
# from options as well as conventions: for instance, @ivar name
# for association is constructed by prefixing @ to association name.
#
# Once attributes are set, reader and writer are created for
# the resource association belongs to
#
# @api semipublic
def initialize(name, child_model, parent_model, options = {})
initialize_object_ivar('child_model', child_model)
initialize_object_ivar('parent_model', parent_model)
@name = name
@instance_variable_name = "@#{@name}".freeze
@options = options.dup.freeze
@child_repository_name = @options[:child_repository_name]
@parent_repository_name = @options[:parent_repository_name]
@child_properties = @options[:child_key].try_dup.freeze
@parent_properties = @options[:parent_key].try_dup.freeze
@min = @options[:min]
@max = @options[:max]
@reader_visibility = @options.fetch(:reader_visibility, :public)
@writer_visibility = @options.fetch(:writer_visibility, :public)
# TODO: normalize the @query to become :conditions => AndOperation
# - Property/Relationship/Path should be left alone
# - Symbol/String keys should become a Property, scoped to the target_repository and target_model
# - Extract subject (target) from Operator
# - subject should be processed same as above
# - each subject should be transformed into AbstractComparison
# object with the subject, operator and value
# - transform into an AndOperation object, and return the
# query as :condition => and_object from self.query
# - this should provide the best performance
@query = @options.except(*self.class::OPTIONS).freeze
create_reader
create_writer
end
# Set the correct ivars for the named object
#
# This method should set the object in an ivar with the same name
# provided, plus it should set a String form of the object in
# a second ivar.
#
# @param [String]
# the name of the ivar to set
# @param [#name, #to_str, #to_sym] object
# the object to set in the ivar
#
# @return [String]
# the String value
#
# @raise [ArgumentError]
# raise when object does not respond to expected methods
#
# @api private
def initialize_object_ivar(name, object)
if object.respond_to?(:name)
instance_variable_set("@#{name}", object)
initialize_object_ivar(name, object.name)
elsif object.respond_to?(:to_str)
instance_variable_set("@#{name}_name", object.to_str.dup.freeze)
elsif object.respond_to?(:to_sym)
instance_variable_set("@#{name}_name", object.to_sym)
else
raise ArgumentError, "#{name} does not respond to #to_str or #name"
end
object
end
# Dynamically defines reader method for source side of association
# (for instance, method article for model Paragraph)
#
# @api semipublic
def create_reader
reader_name = name.to_s
return if source_model.resource_method_defined?(reader_name)
source_model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
#{reader_visibility} # public
def #{reader_name}(query = nil) # def author(query = nil)
relationships[#{name.inspect}].get(self, query) # relationships[:author].get(self, query)
end # end
RUBY
end
# Dynamically defines writer method for source side of association
# (for instance, method article= for model Paragraph)
#
# @api semipublic
def create_writer
writer_name = "#{name}="
return if source_model.resource_method_defined?(writer_name)
source_model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
#{writer_visibility} # public
def #{writer_name}(target) # def author=(target)
relationships[#{name.inspect}].set(self, target) # relationships[:author].set(self, target)
end # end
RUBY
end
# Sets the association targets in the resource
#
# @param [Resource] source
# the source to set
# @param [Array<Resource>] targets
# the targets for the association
# @param [Query, Hash] query
# the query to scope the association with
#
# @return [undefined]
#
# @api private
def eager_load_targets(source, targets, query)
raise NotImplementedError, "#{self.class}#eager_load_targets not implemented"
end
# @api private
def valid_target_collection?(collection, negated)
if collection.kind_of?(Collection)
# TODO: move the check for model_key into Collection#reloadable?
# since what we're really checking is a Collection's ability
# to reload itself, which is (currently) only possible if the
# key was loaded.
model_key = target_model.key(repository.name)
collection.model <= target_model &&
(collection.query.fields & model_key) == model_key &&
(collection.loaded? ? (collection.any? || negated) : true)
else
collection.all? { |resource| valid_target?(resource) }
end
end
# @api private
def valid_target?(target)
target.kind_of?(target_model) &&
source_key.valid?(target_key.get(target))
end
# @api private
def valid_source?(source)
source.kind_of?(source_model) &&
target_key.valid?(source_key.get(source))
end
# @api private
def inverse?(other)
return true if @inverse.equal?(other)
other != self &&
kind_of_inverse?(other) &&
cmp_repository?(other, :==, :child) &&
cmp_repository?(other, :==, :parent) &&
cmp_model?(other, :==, :child) &&
cmp_model?(other, :==, :parent) &&
cmp_key?(other, :==, :child) &&
cmp_key?(other, :==, :parent)
# TODO: match only when the Query is empty, or is the same as the
# default scope for the target model
end
# @api private
def inverse_name
if options[:inverse].kind_of?(Relationship)
options[:inverse].name
else
options[:inverse]
end
end
# @api private
def invert
inverse_class.new(inverse_name, child_model, parent_model, inverted_options)
end
# @api private
def inverted_options
options.only(*OPTIONS - [ :min, :max ]).update(:inverse => self)
end
# @api private
def kind_of_inverse?(other)
other.kind_of?(inverse_class)
end
# @api private
def cmp?(other, operator)
name.send(operator, other.name) &&
cmp_repository?(other, operator, :child) &&
cmp_repository?(other, operator, :parent) &&
cmp_model?(other, operator, :child) &&
cmp_model?(other, operator, :parent) &&
cmp_key?(other, operator, :child) &&
cmp_key?(other, operator, :parent) &&
query.send(operator, other.query)
end
# @api private
def cmp_repository?(other, operator, type)
# if either repository is nil, then the relationship is relative,
# and the repositories are considered equivalent
return true unless repository_name = send("#{type}_repository_name")
return true unless other_repository_name = other.send("#{type}_repository_name")
repository_name.send(operator, other_repository_name)
end
# @api private
def cmp_model?(other, operator, type)
send("#{type}_model?") &&
other.send("#{type}_model?") &&
send("#{type}_model").base_model.send(operator, other.send("#{type}_model").base_model)
end
# @api private
def cmp_key?(other, operator, type)
property_method = "#{type}_properties"
self_key = send(property_method)
other_key = other.send(property_method)
self_key.send(operator, other_key)
end
def associate_targets(source, targets)
# TODO: create an object that wraps this logic, and when the first
# kicker is fired, then it'll load up the collection, and then
# populate all the other methods
target_maps = Hash.new { |h,k| h[k] = [] }
targets.each do |target|
target_maps[target_key.get(target)] << target
end
Array(source).each do |source|
key = source_key.get(source)
eager_load_targets(source, target_maps[key], query)
end
end
end # class Relationship
end # module Associations
end # module DataMapper