-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtarget_constructor.rb
47 lines (38 loc) · 1.12 KB
/
target_constructor.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
module FedoraMigrate
class TargetConstructor
attr_accessor :source, :candidates, :target
def initialize(source)
@source = source
end
def build
raise FedoraMigrate::Errors::MigrationError, "No qualified targets found in #{source.pid}" if target.nil?
target.new(id: FedoraMigrate::Mover.id_component(source))
end
def target
@target ||= determine_target
end
private
def determine_target
Array(candidates).map { |model| vet(model) }.compact.first
end
def vet(model)
klass = class_from_model(model)
klass ||= namespaced_class_from_model(model)
Logger.debug "rejecting #{model} for target" if klass.nil?
klass
end
def class_from_model(model)
FedoraMigrate::Mover.id_component(model).constantize
rescue NameError
nil
end
def namespaced_class_from_model(model)
FedoraMigrate::Mover.id_component(model).split(/_/).map(&:camelize).join('::').constantize
rescue NameError
nil
end
def candidates
@candidates ||= source.models
end
end
end