Class: Wings::ActiveFedoraConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/wings/active_fedora_converter.rb,
lib/wings/active_fedora_converter/default_work.rb,
lib/wings/active_fedora_converter/nested_resource.rb,
lib/wings/active_fedora_converter/instance_builder.rb,
lib/wings/active_fedora_converter/file_metadata_node.rb

Overview

Note:

the ‘Valkyrie::Resource` object passed to this class must have an `#internal_resource` mapping it to an `ActiveFedora::Base` class.

Converts ‘Valkyrie::Resource` objects to legacy `ActiveFedora::Base` objects.

Examples:

work     = GenericWork.new(title: ['Comet in Moominland'])
resource = GenericWork.valkyrie_resource

ActiveFedoraConverter.new(resource: resource).convert == work # => true

Defined Under Namespace

Classes: DefaultWork, InstanceBuilder, NestedResource, PropertyApplicator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource:) ⇒ ActiveFedoraConverter

Returns a new instance of ActiveFedoraConverter.

Parameters:

  • (Valkyrie::Resource)


52
53
54
# File 'lib/wings/active_fedora_converter.rb', line 52

def initialize(resource:)
  @resource = resource
end

Instance Attribute Details

#resourceValkyrie::Resource

Returns:

  • (Valkyrie::Resource)


48
49
50
# File 'lib/wings/active_fedora_converter.rb', line 48

def resource
  @resource
end

Class Method Details

.apply_properties(klass, schema) ⇒ Object



5
6
7
# File 'lib/wings/active_fedora_converter/default_work.rb', line 5

def self.apply_properties(klass, schema)
  schema.each { |schema_key| PropertyApplicator.new(schema_key).apply(klass) }
end

.attributes_classClass

Accesses the Class implemented for handling resource attributes

Returns:

  • (Class)


25
26
27
# File 'lib/wings/active_fedora_converter.rb', line 25

def self.attributes_class
  ActiveFedoraAttributes
end

.class_cacheHash<Class, Class>

A class level cache mapping from Valkyrie resource classes to generated ActiveFedora classes

Returns:

  • (Hash<Class, Class>)


33
34
35
# File 'lib/wings/active_fedora_converter.rb', line 33

def self.class_cache
  @class_cache ||= {}
end

.convert(resource:) ⇒ ActiveFedora::Base

Returns:



41
42
43
# File 'lib/wings/active_fedora_converter.rb', line 41

def self.convert(resource:)
  new(resource: resource).convert
end

.DefaultWork(resource_class) ⇒ Object

default work class builder



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/wings/active_fedora_converter/default_work.rb', line 78

def self.DefaultWork(resource_class) # rubocop:disable Naming/MethodName
  class_cache[resource_class] ||= Class.new(DefaultWork) do
    self.valkyrie_class = resource_class.respond_to?(:valkyrie_class) ? resource_class.valkyrie_class : resource_class
    # skip reserved attributes, we assume we don't need to translate valkyrie internals
    schema = valkyrie_class.schema.reject do |key|
      valkyrie_class.reserved_attributes.include?(key.name)
    end

    Wings::ActiveFedoraConverter.apply_properties(self, schema)

    # nested attributes in AF don't inherit! this needs to be here until we can drop it completely.y
    accepts_nested_attributes_for :nested_resource
  end
end

.FileMetadataNode(resource_class) ⇒ Object

rubocop:disable Naming/MethodName



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/wings/active_fedora_converter/file_metadata_node.rb', line 5

def self.FileMetadataNode(resource_class) # rubocop:disable Naming/MethodName
  class_cache[resource_class] ||= Class.new(FileMetadataNode) do
    self.valkyrie_class = resource_class

    # skip reserved attributes, we assume we don't need to translate valkyrie internals
    schema = resource_class.schema.reject do |key|
      resource_class.reserved_attributes.include?(key.name) ||
        key.name == :size || key.name == :has_model
    end

    Wings::ActiveFedoraConverter.apply_properties(self, schema)
  end
end

Instance Method Details

#active_fedora_classObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/wings/active_fedora_converter.rb', line 75

def active_fedora_class
  @active_fedora_class ||= # cache the class at the instance level
    begin
      klass = begin
                resource.internal_resource.constantize
              rescue NameError
                Wings::ActiveFedoraClassifier.new(resource.internal_resource).best_model
              end

      return klass if klass <= ActiveFedora::Common

      ModelRegistry.lookup(klass)
    end
end

#attributesHash

Accesses and parses the attributes from the resource through ConverterValueMapper

Returns:

  • (Hash)

    attributes with values mapped for building an ActiveFedora model



60
61
62
63
64
# File 'lib/wings/active_fedora_converter.rb', line 60

def attributes
  @attributes ||= attributes_class.mapped_attributes(attributes: resource.attributes).select do |attr|
    active_fedora_class.supports_property?(attr)
  end
end

#convertActiveFedora::Base

Returns:



68
69
70
71
72
73
# File 'lib/wings/active_fedora_converter.rb', line 68

def convert
  instance.tap do |af_object|
    af_object.id ||= id unless id.empty?
    apply_attributes_to_model(af_object)
  end
end

#idString

In the context of a Valkyrie resource, prefer to use the id if it is provided and fallback to the first of the alternate_ids. If all else fails then the id hasn’t been minted and shouldn’t yet be set.

Returns:

  • (String)


95
96
97
98
99
100
# File 'lib/wings/active_fedora_converter.rb', line 95

def id
  return resource[:id].to_s if resource[:id].present? && resource[:id]&.is_a?(::Valkyrie::ID)
  return "" unless resource.respond_to?(:alternate_ids)

  resource.alternate_ids.first.to_s
end