Class: ContentDm::GenericMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/contentdm/mapper.rb

Overview

GenericMapper acts as a fallback formatter for instances when no other Mapper is defined

Direct Known Subclasses

Mapper

Constant Summary collapse

SaveOptions =
Nokogiri::XML::Node::SaveOptions

Instance Method Summary collapse

Instance Method Details

#to_html(record, opts = {}) ⇒ Object

Serialize the given Record to an HTML string



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/contentdm/mapper.rb', line 52

def to_html(record, opts = {})
  save_options = { :encoding => 'UTF-8', :save_with => (SaveOptions::AS_XML | SaveOptions::NO_DECLARATION), :indent => 2 }.merge(opts)
  builder = Nokogiri::XML::Builder.new do |doc|
    doc.span {
      record..each_pair { |k,v|
        unless v.nil? or v.to_s.empty?
          (prefix,tag) = k.split(/\./)
          # Convert from camelCase to Human Readable Label
          tag = tag.gsub(/(\S)([A-Z])/,'\1 \2').gsub(/\b('?[a-z])/) { $1.capitalize }
          doc.p {
            doc.b {
              doc.text "#{tag}:"
            }
            doc.text " "
            if v.is_a?(Array)
              doc.br
              v.each { |value|
                doc.text value unless value.empty?
                doc.br
              }
            else
              doc.text v
            end
          }
        end
      }
    }
  end
  builder.to_xml(save_options)
end

#to_xml(record, opts = {}) ⇒ Object

Serialize the given Record to a Qualified Dublin Core XML string



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/contentdm/mapper.rb', line 27

def to_xml(record, opts = {})
  builder = Nokogiri::XML::Builder.new do |doc|
    doc.qualifieddc('xmlns:qdc' => "http://epubs.cclrc.ac.uk/xmlns/qdc/", 
      'xmlns:dc' => "http://purl.org/dc/elements/1.1/", 
      'xmlns:dcterms' => "http://purl.org/dc/terms/") {
        record..each_pair { |k,v|
          (prefix,tag) = k.split(/\./)
          if v.is_a?(Array)
            v.each { |value|
              doc[prefix].send(tag.to_sym) {
                doc.text(value)
              }
            }
          else
            doc[prefix].send(tag.to_sym) {
              doc.text(v)
            }
          end
        }
      }
  end
  builder.to_xml
end