Class: Libis::Tools::Metadata::MarcRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/libis/tools/metadata/marc_record.rb

Overview

Base class for reading MARC based records.

For indicator selection: ‘#’ or ” (empty) is wildcard; ‘_’ or ‘ ’ (space) is blank.

Direct Known Subclasses

Marc21Record

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml_node) ⇒ MarcRecord

Create a new MarcRecord object

Parameters:

  • xml_node (XML node)

    XML node from Nokogiri or XmlDocument that contains child nodes with the data for one MARC record.



28
29
30
31
32
# File 'lib/libis/tools/metadata/marc_record.rb', line 28

def initialize(xml_node)
  @node = xml_node
  @node.document.remove_namespaces!
  @all_records = Hash.new { |h, k| h[k] = Array.new }
end

Class Method Details

.load(filename) ⇒ Object

Load XML document from file and create a new Libis::Tools::Metadata::MarcRecord for it.

Parameters:

  • filename (String)

    name of XML Marc file



166
167
168
169
# File 'lib/libis/tools/metadata/marc_record.rb', line 166

def self.load(filename)
  doc = ::Libis::Tools::XmlDocument.open(filename)
  self.new(doc.root)
end

.read(io) ⇒ Object

Load XML document from stream and create a new Libis::Tools::Metadata::MarcRecord for it.

Parameters:

  • io (IO, String)

    input stream



173
174
175
176
177
# File 'lib/libis/tools/metadata/marc_record.rb', line 173

def self.read(io)
  io = StringIO.new(io) if io.is_a? String
  doc = ::Libis::Tools::XmlDocument.parse(io)
  self.new(doc.root)
end

Instance Method Details

#allHash

Returns the internal data structure (a Hash) with all the MARC data.

The internal structure is a Hash with the tag as key and as value an Array of either FixField or VarField instances.

Returns:

  • (Hash)

    internal data structure



46
47
48
49
# File 'lib/libis/tools/metadata/marc_record.rb', line 46

def all
  return @all_records unless @all_records.empty?
  @all_records = get_all_records
end

#all_fields(tag, subfields) ⇒ Object

Find all fields matching the criteria. (see #first_tag)

Parameters:

  • tag (String)

    Tag selection string. Tag name with indicators, ‘#’ for wildcard, ‘_’ for blank.

  • subfields (String)

    Subfield specification. See FieldFormat class for more info; ignored for controlfields.



119
120
121
122
123
124
# File 'lib/libis/tools/metadata/marc_record.rb', line 119

def all_fields(tag, subfields)
  r = all_tags(tag, subfields).collect { |t| t.subfields_array(subfields) }.flatten.compact
  return r unless block_given?
  r.map { |field| yield field }
  r.size > 0
end

#all_tags(tag, subfields = '', select_block = Proc.new { |_| true}) ⇒ Array Also known as: each_tag

Get all fields matching search criteria.

A block with one parameter can be supplied when calling this method. Each time a match is found, the block will be called with the field data as argument and the return value of the block will be added to the method’s return value. This could for example be used to narrow the selection of the fields:

# Only select 700 tags where $4 subfield contains 'abc', 'def' or 'xyz'
record.all_tags('700') { |v| v.subfield['4'] =~ /^(abc|def|xyz)$/ ? v : nil }.compact

Parameters:

  • tag (String)

    Tag selection string. Tag name with indicators, ‘#’ for wildcard, ‘_’ for blank. If an extra subfield name is added, a result will be created for each instance found of that subfield.

  • subfields (String) (defaults to: '')

    Subfield specification. See FieldFormat class for more info; ignored for controlfields.

  • select_block (Proc) (defaults to: Proc.new { |_| true})

    block that will be executed once for each field found. The block takes one argument (the field) and should return true or false. True selects the field, false rejects it.

Returns:

  • (Array)

    If a block was supplied to the method call, the array will contain the result of the block for each tag found. Otherwise the array will just contain the data for each matching tag.



79
80
81
82
83
84
# File 'lib/libis/tools/metadata/marc_record.rb', line 79

def all_tags(tag, subfields = '', select_block = Proc.new { |_| true})
  t, ind1, ind2, subfield = tag =~ /^\d{3}/ ? [tag[0..2], tag[3], tag[4], tag[5]] : [tag, nil, nil, nil]
  result = get_records(t, ind1, ind2, subfield, subfields, &select_block)
  return result unless block_given?
  result.map { |record| yield record }
end

#eachArray

Iterates over all the MARC fields.

If a block is supplied it will be called for each field in the MARC record. The supplied argument will be the FixField or VarField instance for each field.

Returns:

  • (Array)

    The list of the field data or return values for each block call.



57
58
59
60
61
# File 'lib/libis/tools/metadata/marc_record.rb', line 57

def each
  all.map { |_, field_array| field_array }.flatten.map do |field|
    block_given? ? yield(field) : field
  end
end

#each_field(tag, subfields) ⇒ Object

Perform action on each field found. Code block required.

Parameters:

  • tag (String)

    Tag selection string. Tag name with indicators, ‘#’ for wildcard, ‘_’ for blank.

  • subfields (String)

    Subfield specification. See FieldFormat class for more info; ignored for controlfields.



139
140
141
142
143
# File 'lib/libis/tools/metadata/marc_record.rb', line 139

def each_field(tag, subfields)
  all_fields(tag, subfields).each do |field|
    yield field
  end
end

#first_field(tag, subfields) {|result| ... } ⇒ Object

Find the first field matching the criteria (see #all_fields)

Parameters:

  • tag (String)

    Tag selection string. Tag name with indicators, ‘#’ for wildcard, ‘_’ for blank.

  • subfields (String)

    Subfield specification. See FieldFormat class for more info; ignored for controlfields.

Yields:

  • (result)


129
130
131
132
133
134
135
# File 'lib/libis/tools/metadata/marc_record.rb', line 129

def first_field(tag, subfields)
  result = all_fields(tag, subfields).first
  return result unless block_given?
  return false unless result
  yield result
  true
end

#first_tag(tag, subfields = '') {|result| ... } ⇒ Object

Find the first tag matching the criteria.

If a block is supplied, it will be called with the found field data. The return value will be whatever the block returns. If no block is supplied, the field data will be returned. If nothing was found, the return value is nil.

Parameters:

  • tag (String)

    Tag selection string. Tag name with indicators, ‘#’ for wildcard, ‘_’ for blank.

  • subfields (String) (defaults to: '')

    Subfield specification. See FieldFormat class for more info; ignored for controlfields.

Yields:

  • (result)

Returns:

  • (Object)

    nil if nothing found; field data or whatever block returns.



109
110
111
112
113
114
# File 'lib/libis/tools/metadata/marc_record.rb', line 109

def first_tag(tag, subfields = '')
  result = all_tags(tag, subfields).first
  return nil unless result
  return result unless block_given?
  yield result
end

#marc_dumpObject

Dump content to string.



146
147
148
# File 'lib/libis/tools/metadata/marc_record.rb', line 146

def marc_dump
  all.values.flatten.each_with_object([]) { |record, m| m << record.dump }.join
end

#save(filename) ⇒ Object

Save the current MARC record to file.

Parameters:

  • filename (String)

    name of the file



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/libis/tools/metadata/marc_record.rb', line 152

def save(filename)
  doc = ::Libis::Tools::XmlDocument.new
  doc.root = @node

  return doc unless filename

  doc.save filename, save_with: (::Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS |
                       ::Nokogiri::XML::Node::SaveOptions::AS_XML |
                       ::Nokogiri::XML::Node::SaveOptions::FORMAT
                   )
end

#select_fields(tag, select_block = nil, &block) ⇒ Array

Get all fields matching search criteria. As #all_tags but without subfield criteria.

Parameters:

  • tag (String)

    Tag selection string. Tag name with indicators, ‘#’ for wildcard, ‘_’ for blank. If an extra subfield name is added, a result will be created for each instance found of that subfield.

  • select_block (Proc) (defaults to: nil)

    block that will be executed once for each field found. The block takes one argument (the field) and should return true or false. True selects the field, false rejects it.

Returns:

  • (Array)

    If a block was supplied to the method call, the array will contain the result of the block for each tag found. Otherwise the array will just contain the data for each matching tag.



96
97
98
# File 'lib/libis/tools/metadata/marc_record.rb', line 96

def select_fields(tag, select_block = nil, &block)
  all_tags(tag, nil, select_block, &block)
end

#to_aseqString

Dump Marc record in Aleph Sequential format

Returns:

  • (String)

    Aleph sequential output



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/libis/tools/metadata/marc_record.rb', line 181

def to_aseq
  record = ''
  doc_number = tag('001').datas

  all.select { |t| t.is_a? Libis::Tools::Metadata::FixField }.each { |t| record += "#{format('%09s', doc_number)} #{t.tag}   L #{t.datas}\n" }
  all.select { |t| t.is_a? Libis::Tools::Metadata::VarField }.each { |t|
    record += "#{format('%09s', doc_number)} #{t.tag}#{t.ind1}#{t.ind2} L "
    t.keys.each { |k|
      t.subfield_array(k).each { |f|
        record += "$$#{k}#{CGI::unescapeHTML(f)}"
      }
    }
    record += "\n"
  }

  record
end

#to_rawXML node

Access to the XML node that was supplied to the constructor

Returns:

  • (XML node)


36
37
38
# File 'lib/libis/tools/metadata/marc_record.rb', line 36

def to_raw
  @node
end