Class: Dor::MetadataService

Inherits:
Object
  • Object
show all
Defined in:
lib/dor/services/metadata_service.rb

Overview

class MetadataHandler

  def fetch(prefix, identifier)
    ### Return metadata for prefix/identifier combo
  end

  def label(metadata)
    ### Return a Fedora-compatible label from the metadata format returned by #fetch
  end

end

Constant Summary collapse

@@cache =
Cache.new(nil, nil, 250, 300)

Class Method Summary collapse

Class Method Details

.can_resolve?(identifier) ⇒ Boolean


41
42
43
44
# File 'lib/dor/services/metadata_service.rb', line 41

def can_resolve?(identifier)
  (prefix, identifier) = identifier.split(/:/,2)
  handlers.keys.include?(prefix.to_sym)
end

.fetch(identifier) ⇒ Object


51
52
53
54
55
56
57
# File 'lib/dor/services/metadata_service.rb', line 51

def fetch(identifier)
  @@cache.fetch(identifier) do
    (prefix, identifier) = identifier.split(/:/,2)
    handler = handler_for(prefix)
    handler.fetch(prefix, identifier)
  end
end

.handler_for(prefix) ⇒ Object


65
66
67
68
69
70
71
# File 'lib/dor/services/metadata_service.rb', line 65

def handler_for(prefix)
  handler = handlers[prefix.to_sym]
  if handler.nil?
    raise MetadataError, "Unkown metadata prefix: #{prefix}"
  end
  return handler
end

.known_prefixesObject


37
38
39
# File 'lib/dor/services/metadata_service.rb', line 37

def known_prefixes
  return handlers.keys
end

.label_for(identifier) ⇒ Object


59
60
61
62
63
# File 'lib/dor/services/metadata_service.rb', line 59

def label_for(identifier)
  (prefix, identifier) = identifier.split(/:/,2)
  handler = handler_for(prefix)
  handler.label(handler.fetch(prefix, identifier))
end

.register(handler_class) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dor/services/metadata_service.rb', line 24

def register(handler_class)
  ['fetch', 'label', 'prefixes'].each do |method|
    unless handler_class.instance_methods.include?(method) or handler_class.instance_methods.include?(method.to_sym) 
      raise TypeError, "Metadata handlers must define ##{method.to_s}"
    end
  end
  handler = handler_class.new
  handler.prefixes.each do |prefix|
    handlers[prefix.to_sym] = handler
  end
  return handler
end

.resolvable(identifiers) ⇒ Object

TODO: Return a prioritized list


47
48
49
# File 'lib/dor/services/metadata_service.rb', line 47

def resolvable(identifiers)
  identifiers.select { |identifier| self.can_resolve?(identifier) }
end