Class: OAI::Provider::Metadata::Format

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/oai/provider/metadata_format.rb

Overview

Metadata Base Class

MetadataFormat is the base class from which all other format classes should inherit. Format classes provide mapping of record fields into XML.

  • prefix - contains the metadata_prefix used to select the format

  • schema - location of the xml schema

  • namespace - location of the namespace document

  • element_namespace - the namespace portion of the XML elements

  • fields - list of fields in this metadata format

See OAI::Metadata::DublinCore for an example

Direct Known Subclasses

DublinCore

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#element_namespaceObject

Returns the value of attribute element_namespace.



20
21
22
# File 'lib/oai/provider/metadata_format.rb', line 20

def element_namespace
  @element_namespace
end

#fieldsObject

Returns the value of attribute fields.



20
21
22
# File 'lib/oai/provider/metadata_format.rb', line 20

def fields
  @fields
end

#namespaceObject

Returns the value of attribute namespace.



20
21
22
# File 'lib/oai/provider/metadata_format.rb', line 20

def namespace
  @namespace
end

#prefixObject

Returns the value of attribute prefix.



20
21
22
# File 'lib/oai/provider/metadata_format.rb', line 20

def prefix
  @prefix
end

#schemaObject

Returns the value of attribute schema.



20
21
22
# File 'lib/oai/provider/metadata_format.rb', line 20

def schema
  @schema
end

Instance Method Details

#encode(model, record) ⇒ Object

Provided a model, and a record belonging to that model this method will return an xml represention of the record. This is the method that should be extended if you need to create more complex xml representations.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/oai/provider/metadata_format.rb', line 26

def encode(model, record)
  if record.respond_to?("to_#{prefix}")
    record.send("to_#{prefix}")
  else
    xml = Builder::XmlMarkup.new
    map = model.respond_to?("map_#{prefix}") ? model.send("map_#{prefix}") : {}
      xml.tag!("#{prefix}:#{element_namespace}", header_specification) do
        fields.each do |field|
          values = value_for(field, record, map)
          if values.respond_to?(:each)
            values.each do |value|
              xml.tag! "#{element_namespace}:#{field}", value
            end
          else
            xml.tag! "#{element_namespace}:#{field}", values
          end
        end
      end
    xml.target!
  end
end