Class: Wings::ModelRegistry

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/wings/model_registry.rb

Overview

This registry provides an off-ramp for legacy ActiveFedora models.

New valkyrie models can be defined manually and registered as an analogue of an existing `ActiveFedora::Base` implementer. This allows Wings to cast the new model to the legacy model when saving, ensuring backwards compatible data on save.

Several models from Hyrax components have default mappings provided by Wings.

Examples:

Wings::ModelRegistry.register(NewValkyrieModel, OldActiveFedoraModel)

Wings::ModelRegistry.lookup(NewValkyrieModel) # => OldActiveFedoraModel
Wings::ModelRegistry.reverse_lookup(OldActiveFedoraModel) # => NewValkyrieModel

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModelRegistry

Returns a new instance of ModelRegistry.


35
36
37
# File 'lib/wings/model_registry.rb', line 35

def initialize
  @map = {}
end

Class Method Details

.lookup(*args) ⇒ Object


27
28
29
# File 'lib/wings/model_registry.rb', line 27

def self.lookup(*args)
  instance.lookup(*args)
end

.register(*args) ⇒ Object


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

def self.register(*args)
  instance.register(*args)
end

.reverse_lookup(*args) ⇒ Object


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

def self.reverse_lookup(*args)
  instance.reverse_lookup(*args)
end

Instance Method Details

#lookup(valkyrie) ⇒ Object


43
44
45
46
47
48
49
# File 'lib/wings/model_registry.rb', line 43

def lookup(valkyrie)
  valkyrie = valkyrie._canonical_valkyrie_model if
    valkyrie.respond_to?(:_canonical_valkyrie_model)

  @map[valkyrie.name]&.safe_constantize ||
    ActiveFedoraConverter::DefaultWork(valkyrie)
end

#register(valkyrie, active_fedora) ⇒ Object


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

def register(valkyrie, active_fedora)
  @map[valkyrie.name] = active_fedora.name
end

#reverse_lookup(active_fedora) ⇒ Object


51
52
53
# File 'lib/wings/model_registry.rb', line 51

def reverse_lookup(active_fedora)
  @map.rassoc(active_fedora.name)&.first&.safe_constantize
end