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:



28
29
30
# File 'lib/wings/model_transformer.rb', line 28

def initialize(pcdm_object:)
  self.pcdm_object = pcdm_object
end

Instance Attribute Details

#pcdm_objectActiveFedora::Base

Returns:



24
25
26
# File 'lib/wings/model_transformer.rb', line 24

def pcdm_object
  @pcdm_object
end

Class Method Details

.for(pcdm_object) ⇒ ::Valkyrie::Resource

Factory

Parameters:

Returns:

  • (::Valkyrie::Resource)

    a resource mirroiring ‘pcdm_object`



38
39
40
# File 'lib/wings/model_transformer.rb', line 38

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`

Returns:

  • (::Valkyrie::Resource)

    a resource mirroring ‘pcdm_object`



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/wings/model_transformer.rb', line 46

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 { |resource| ensure_current_permissions(resource) }
end

#cacheResourceClassCache

Returns:



72
73
74
# File 'lib/wings/model_transformer.rb', line 72

def cache
  ResourceClassCache.instance
end

#ensure_current_permissions(resource) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/wings/model_transformer.rb', line 59

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)

  resource.permission_manager.acl.permissions =
    pcdm_object.access_control.valkyrie_resource.permissions
end