Class: Wings::ModelTransformer

Inherits:
Object
  • Object
show all
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.

Examples:

getting a valkyrie resource

work     = GenericWork.new(id: 'an_identifier')
resource = Wings::ModelTransformer.for(work)

resource.alternate_ids # => [#<Valkyrie::ID:0x... id: 'an_identifier'>]

See Also:

Defined Under Namespace

Classes: ResourceClassCache

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pcdm_object:) ⇒ ModelTransformer

Returns a new instance of ModelTransformer.

Parameters:



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_objectActiveFedora::Base

Returns:



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

Parameters:

Returns:

  • (::Valkyrie::Resource)

    a resource mirroring ‘pcdm_object`



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

Returns:

  • (::Valkyrie::Resource)

    a resource mirroring ‘pcdm_object`



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.embargo = pcdm_object.embargo&.valkyrie_resource if pcdm_object.respond_to?(:embargo) && pcdm_object.embargo
    check_size(resource)
    check_pcdm_use(resource)
    ensure_current_permissions(resource)
  end
end

#cacheResourceClassCache

Returns:



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 ensure_current_permissions(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.permission_manager.acl.permissions = acl.permissions
end