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

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, 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)

50
51
52
# File 'lib/wings/active_fedora_converter.rb', line 50

def initialize(resource:)
  @resource = resource
end

Instance Attribute Details

#resourceValkyrie::Resource

Returns:

  • (Valkyrie::Resource)

46
47
48
# File 'lib/wings/active_fedora_converter.rb', line 46

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)

23
24
25
# File 'lib/wings/active_fedora_converter.rb', line 23

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>)

31
32
33
# File 'lib/wings/active_fedora_converter.rb', line 31

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

.convert(resource:) ⇒ ActiveFedora::Base

Returns:


39
40
41
# File 'lib/wings/active_fedora_converter.rb', line 39

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
92
# 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

    # 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)
    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

Instance Method Details

#active_fedora_classObject


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

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


58
59
60
61
62
# File 'lib/wings/active_fedora_converter.rb', line 58

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

#convertActiveFedora::Base

Returns:


66
67
68
69
70
71
# File 'lib/wings/active_fedora_converter.rb', line 66

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)

93
94
95
96
97
98
# File 'lib/wings/active_fedora_converter.rb', line 93

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

  resource.alternate_ids.first.to_s
end