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



148
149
150
151
152
153
# File 'lib/libis/tools/metadata/marc_record.rb', line 148

def self.load(filename)

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

end

.read(io) ⇒ Object



155
156
157
158
159
160
# File 'lib/libis/tools/metadata/marc_record.rb', line 155

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



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

def all_fields(tag, subfields)
  r = all_tags(tag, subfields).collect { |tag| tag.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(t, s) ⇒ Object



124
125
126
127
128
# File 'lib/libis/tools/metadata/marc_record.rb', line 124

def each_field(t, s)
  all_fields(t, s).each do |field|
    yield field
  end
end

#first_field(t, s) {|result| ... } ⇒ Object

Yields:

  • (result)


115
116
117
118
119
120
121
# File 'lib/libis/tools/metadata/marc_record.rb', line 115

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

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

Find the first field 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.



101
102
103
104
105
106
# File 'lib/libis/tools/metadata/marc_record.rb', line 101

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



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

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

#save(filename) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/libis/tools/metadata/marc_record.rb', line 134

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) ⇒ Object



88
89
90
# File 'lib/libis/tools/metadata/marc_record.rb', line 88

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

#to_aseqObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/libis/tools/metadata/marc_record.rb', line 162

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