/
mapper.rb
59 lines (51 loc) · 1.77 KB
/
mapper.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
# frozen_string_literal: true
module Cocina
# Maps Dor::Items to Cocina objects
class Mapper
# Raised when called on something other than an item (DRO), etd, collection, or adminPolicy (APO)
class UnsupportedObjectType < StandardError; end
# Raised when we can't figure out the title for the object.
class MissingTitle < StandardError; end
# @param [Dor::Abstract] item the Fedora object to convert to a cocina object
# @return [Cocina::Models::DRO,Cocina::Models::Collection,Cocina::Models::AdminPolicy]
# @raises [SolrConnectionError]
def self.build(item)
new(item).build
end
# @param [Dor::Abstract] item the Fedora object to convert to a cocina object
def initialize(item)
@item = item
end
# @return [Cocina::Models::DRO,Cocina::Models::Collection,Cocina::Models::AdminPolicy]
# @raises [SolrConnectionError]
def build
klass = cocina_klass
props = case klass
when Cocina::Models::DRO
FromFedora::DRO.props(item)
when Cocina::Models::Collection
FromFedora::Collection.props(item)
when Cocina::Models::AdminPolicy
FromFedora::APO.props(item)
else
raise "unable to build '#{klass}'"
end
klass.new(props)
end
private
attr_reader :item
# @todo This should have more specific type such as found in identityMetadata.objectType
def cocina_klass
case item
when Dor::Item, Dor::Etd
Cocina::Models::DRO
when Dor::Collection
Cocina::Models::Collection
when Dor::AdminPolicyObject
Cocina::Models::AdminPolicy
else
raise UnsupportedObjectType, "Unknown type for #{item.class}"
end
end
end
end