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.



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

def initialize
  @map = {}
end

Class Method Details

.lookup(*args) ⇒ Object



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

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



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

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

.unregister(*args) ⇒ Object



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

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

Instance Method Details

#lookup(valkyrie) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/wings/model_registry.rb', line 51

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



43
44
45
# File 'lib/wings/model_registry.rb', line 43

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

#reverse_lookup(active_fedora) ⇒ Object



59
60
61
# File 'lib/wings/model_registry.rb', line 59

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

#unregister(valkyrie) ⇒ Object



47
48
49
# File 'lib/wings/model_registry.rb', line 47

def unregister(valkyrie)
  @map.delete(valkyrie.name)
end