Class: Libis::Tools::Metadata::DublinCoreRecord

Inherits:
XmlDocument show all
Defined in:
lib/libis/tools/metadata/dublin_core_record.rb

Constant Summary collapse

DC_ELEMENTS =
%w'contributor coverage creator date description format identifier language' +
%w'publisher relation rights source subject title type'
DCTERMS_ELEMENTS =
%w'abstract accessRights accrualMethod accrualPeriodicity accrualPolicy alternative' +
%w'audience available bibliographicCitation conformsTo contributor coverage created creator date' +
%w'dateAccepted dateCopyrighted dateSubmitted description educationLevel extent format hasFormat' +
%w'hasPart hasVersion identifier instructionalMethod isFormatOf isPartOf isReferencedBy isReplacedBy' +
%w'isRequiredBy issued isVersionOf language license mediator medium modified provenance publisher' +
%w'references relation replaces requires rights rightsHolder source spatial subject tableOfContents' +
%w'temporal title type valid'

Instance Attribute Summary

Attributes inherited from XmlDocument

#document

Instance Method Summary collapse

Methods inherited from XmlDocument

#[], #[]=, add_attributes, #add_attributes, add_namespaces, #add_namespaces, #add_processing_instruction, #build, build, from_hash, get_content, #get_node, #has_element?, #invalid?, #method_missing, open, parse, #root, #root=, #save, #to_hash, #to_xml, #valid?, #validate, #validates_against?, #value, #values

Constructor Details

#initialize(doc = nil) ⇒ DublinCoreRecord

Returns a new instance of DublinCoreRecord.

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/libis/tools/metadata/dublin_core_record.rb', line 21

def initialize(doc = nil)
  super()
  xml_doc = case doc
              when ::Libis::Tools::XmlDocument
                doc
              when String
                # noinspection RubyResolve
                File.exist?(doc) ? Libis::Tools::XmlDocument.load(doc) : Libis::Tools::XmlDocument.parse(doc)
              when IO
                Libis::Tools::XmlDocument.parse(doc.read)
              when Hash
                Libis::Tools::XmlDocument.from_hash(doc)
              when NilClass
                Libis::Tools::XmlDocument.new.build do |xml|
                  xml[:dc].record('xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
                             'xmlns:dc' => 'http://purl.org/dc/elements/1.1/',
                             'xmlns:dcterms' => 'http://purl.org/dc/terms/') {
                    yield xml if block_given?
                  }
                end
              else
                raise ArgumentError, "Invalid argument: #{doc.inspect}"
            end
  @document = xml_doc.document if xml_doc
  raise ArgumentError, 'XML document not valid.' if self.invalid?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Libis::Tools::XmlDocument

Instance Method Details

#add_node(name, value = nil, parent = nil, attributes = {}) ⇒ Object



59
60
61
62
63
# File 'lib/libis/tools/metadata/dublin_core_record.rb', line 59

def add_node(name, value = nil, parent = nil, attributes = {})
  ns, tag = get_namespace(name.to_s)
  (attributes[:namespaces] ||= {})[:node_ns] ||= ns if ns
  super tag, value, parent, attributes
end

#allObject



48
49
50
# File 'lib/libis/tools/metadata/dublin_core_record.rb', line 48

def all
  @all_records ||= get_all_records
end

#xpath(path) ⇒ Object



52
53
54
55
56
57
# File 'lib/libis/tools/metadata/dublin_core_record.rb', line 52

def xpath(path)
  m = /^([\/.]*\/)?(dc(terms)?:)?(.*)/.match(path.to_s)
  return [] unless m[4]
  path = (m[1] || '') + ('dc:' || m[2]) + m[4]
  @document.xpath(path.to_s)
end