Class: OpenURL::Marc

Inherits:
ContextObjectEntity show all
Defined in:
lib/openurl/metadata_formats/marc.rb

Instance Attribute Summary collapse

Attributes inherited from ContextObjectEntity

#abbr, #format, #identifiers, #label, #metadata, #private_data, #reference

Instance Method Summary collapse

Methods inherited from ContextObjectEntity

#add_identifier, #delete_identifier, #empty?, #get_metadata, #identifier, #kev, new_from_format, normalize_id, #set_format, #set_metadata, #set_private_data, #set_reference, #to_hash, #xml, #xml_for_ref_entity

Constructor Details

#initializeMarc

Returns a new instance of Marc.



16
17
18
19
20
21
22
23
# File 'lib/openurl/metadata_formats/marc.rb', line 16

def initialize      
  super    
  @xml_ns = "info:ofi/fmt:xml:xsd:MARC21"
  @format = @xml_ns
  @marc = MARC::Record.new
  @marc_ns = "http://www.loc.gov/MARC21/slim"
  @metadata = @marc.fields
end

Instance Attribute Details

#marcObject

Returns the value of attribute marc.



15
16
17
# File 'lib/openurl/metadata_formats/marc.rb', line 15

def marc
  @marc
end

Instance Method Details

#import_xml_metadata(node) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/openurl/metadata_formats/marc.rb', line 43

def (node)         
  marcxml = REXML::XPath.first(node,"./ctx:metadata-by-val/ctx:metadata/fmt:collection | ./ctx:metadata-by-val/ctx:metadata/fmt:record", 
    {"ctx"=>"info:ofi/fmt:xml:xsd:ctx","fmt"=>@marc_ns})
  if marcxml
    marcxml.root.prefix = ''
    records = []
    MARC::XMLReader.new(StringIO.new(marcxml.to_s)).each do | record |
      records << record
    end
    if records.length == 1
      @marc = records[0]
    else
      @marc = records
    end
  end
  @metadata = @marc.fields
end

#serialize_metadata(elem, label) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/openurl/metadata_formats/marc.rb', line 25

def (elem, label)
   = elem.add_element("ctx:metadata")
  if @marc.is_a?(Array)
    container = .add_elements("#{label}:collection")
    container.add_namespace(label, @marc_ns)
    @marc.each do | mrc |
      rec = mrc.to_xml.root          
      mrc_elem = container.add_element rec
      mrc_elem.name = "#{label}:#{mrc_elem.name}"
    end
  else
    rec = @marc.to_xml.root 
    rec.add_namespace(label, @marc_ns)
    rec.name = "#{label}:#{rec.name}"
    mrc_elem = .add_element rec                                
  end      
end