Class: Wings::ModelTransformer
- Inherits:
-
Object
- Object
- Wings::ModelTransformer
- Defined in:
- lib/wings/model_transformer.rb
Overview
Transforms ActiveFedora models or objects into Valkyrie::Resource models or objects
Similar to an orm_converter class in other valkyrie persisters. Also used by the Valkyrizable mixin to make AF objects able to return their Valkyrie::Resource representation.
Defined Under Namespace
Classes: ResourceClassCache
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
-
#build ⇒ ::Valkyrie::Resource
Builds a ‘Valkyrie::Resource` equivalent to the `pcdm_object`.
- #cache ⇒ ResourceClassCache
- #check_pcdm_use(resource) ⇒ Object
- #check_size(resource) ⇒ Object
- #ensure_current_permissions(resource) ⇒ Object
-
#initialize(pcdm_object:) ⇒ ModelTransformer
constructor
A new instance of ModelTransformer.
Constructor Details
#initialize(pcdm_object:) ⇒ ModelTransformer
Returns a new instance of ModelTransformer.
29 30 31 |
# File 'lib/wings/model_transformer.rb', line 29 def initialize(pcdm_object:) self.pcdm_object = pcdm_object end |
Instance Attribute Details
#pcdm_object ⇒ ActiveFedora::Base
25 26 27 |
# File 'lib/wings/model_transformer.rb', line 25 def pcdm_object @pcdm_object end |
Class Method Details
.for(pcdm_object) ⇒ ::Valkyrie::Resource
Factory
39 40 41 |
# File 'lib/wings/model_transformer.rb', line 39 def self.for(pcdm_object) new(pcdm_object: pcdm_object).build end |
Instance Method Details
#build ⇒ ::Valkyrie::Resource
Builds a ‘Valkyrie::Resource` equivalent to the `pcdm_object`
rubocop:disable Metrics/AbcSize
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/wings/model_transformer.rb', line 48 def build klass = cache.fetch(pcdm_object.class) do OrmConverter.to_valkyrie_resource_class(klass: pcdm_object.class) end mint_id unless pcdm_object.id attrs = attributes.tap { |hash| hash[:new_record] = pcdm_object.new_record? } attrs[:alternate_ids] = [::Valkyrie::ID.new(pcdm_object.id)] if pcdm_object.id klass.new(**attrs).tap do |resource| resource.lease = pcdm_object.lease&.valkyrie_resource if pcdm_object.respond_to?(:lease) && pcdm_object.lease resource. = pcdm_object.&.valkyrie_resource if pcdm_object.respond_to?(:embargo) && pcdm_object. check_size(resource) check_pcdm_use(resource) (resource) end end |
#cache ⇒ ResourceClassCache
109 110 111 |
# File 'lib/wings/model_transformer.rb', line 109 def cache ResourceClassCache.instance end |
#check_pcdm_use(resource) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/wings/model_transformer.rb', line 72 def check_pcdm_use(resource) return unless resource.respond_to?(:pcdm_use) && pcdm_object.respond_to?(:metadata_node) && pcdm_object&.&.respond_to?(:type) resource.pcdm_use = pcdm_object..type.to_a end |
#check_size(resource) ⇒ Object
67 68 69 70 |
# File 'lib/wings/model_transformer.rb', line 67 def check_size(resource) return unless resource.respond_to?(:recorded_size) && pcdm_object.respond_to?(:size) resource.recorded_size = [pcdm_object.size.to_i] end |
#ensure_current_permissions(resource) ⇒ Object
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 |
# File 'lib/wings/model_transformer.rb', line 79 def (resource) return if pcdm_object.try(:access_control).blank? # set permissions on the locally cached permission manager if one is present, # otherwise, we can just rely on the `access_control_ids`. return unless resource.respond_to?(:permission_manager) # When the pcdm_object has an access_control (see above) but there's no access_control_id, we # need to rely on the computed access_control object. Why? There are tests that fail. acl = if pcdm_object.access_control_id.nil? pcdm_object.access_control.valkyrie_resource else begin # Given that we have an access_control AND an access_control_id, we want to ensure # that we fetch the access_control from persistence. Why? Because when we update # an ACL and are using those adapters, we will write the ACL to the Valkyrie adapter # without writing the work to the Valkyrie adapter. This might be a failing, but # migrations in place are hard. acl_id = pcdm_object.access_control_id acl_id = ::Valkyrie::ID.new(acl_id) unless acl_id.is_a?(::Valkyrie::ID) Hyrax.query_service.find_by(id: acl_id) rescue ::Valkyrie::Persistence::ObjectNotFoundError pcdm_object.access_control.valkyrie_resource end end resource..acl. = acl. end |