Class: Wings::ActiveFedoraConverter
- Inherits:
-
Object
- Object
- Wings::ActiveFedoraConverter
- 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
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.
Defined Under Namespace
Classes: DefaultWork, NestedResource, PropertyApplicator
Instance Attribute Summary collapse
Class Method Summary collapse
- .apply_properties(klass, schema) ⇒ Object
-
.attributes_class ⇒ Class
Accesses the Class implemented for handling resource attributes.
-
.class_cache ⇒ Hash<Class, Class>
A class level cache mapping from Valkyrie resource classes to generated ActiveFedora classes.
- .convert(resource:) ⇒ ActiveFedora::Base
-
.DefaultWork(resource_class) ⇒ Object
default work class builder.
Instance Method Summary collapse
- #active_fedora_class ⇒ Object
-
#attributes ⇒ Hash
Accesses and parses the attributes from the resource through ConverterValueMapper.
- #convert ⇒ ActiveFedora::Base
-
#id ⇒ String
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.
-
#initialize(resource:) ⇒ ActiveFedoraConverter
constructor
A new instance of ActiveFedoraConverter.
Constructor Details
#initialize(resource:) ⇒ ActiveFedoraConverter
Returns a new instance of ActiveFedoraConverter.
50 51 52 |
# File 'lib/wings/active_fedora_converter.rb', line 50 def initialize(resource:) @resource = resource end |
Instance Attribute Details
#resource ⇒ 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_class ⇒ Class
Accesses the Class implemented for handling resource attributes
23 24 25 |
# File 'lib/wings/active_fedora_converter.rb', line 23 def self.attributes_class ActiveFedoraAttributes end |
.class_cache ⇒ Hash<Class, Class>
A class level cache mapping from Valkyrie resource classes to generated ActiveFedora classes
31 32 33 |
# File 'lib/wings/active_fedora_converter.rb', line 31 def self.class_cache @class_cache ||= {} end |
.convert(resource:) ⇒ ActiveFedora::Base
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_class ⇒ Object
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 |
#attributes ⇒ Hash
Accesses and parses the attributes from the resource through ConverterValueMapper
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 |
#convert ⇒ ActiveFedora::Base
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 |
#id ⇒ String
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.
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 |