Class: Libis::Metadata::MarcRecord
- Inherits:
-
Object
- Object
- Libis::Metadata::MarcRecord
- Defined in:
- lib/libis/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
Class Method Summary collapse
-
.load(filename) ⇒ Object
Load XML document from file and create a new MarcRecord for it.
-
.read(io) ⇒ Object
Load XML document from stream and create a new MarcRecord for it.
Instance Method Summary collapse
-
#all ⇒ Hash
Returns the internal data structure (a Hash) with all the MARC data.
-
#all_fields(tag, subfields) ⇒ Object
Find all fields matching the criteria.
-
#all_tags(tag, subfields = '', select_block = Proc.new {|_| true}) ⇒ Array
(also: #each_tag)
Get all fields matching search criteria.
-
#each ⇒ Array
Iterates over all the MARC fields.
-
#each_field(tag, subfields) ⇒ Object
Perform action on each field found.
-
#first_field(tag, subfields) {|result| ... } ⇒ Object
Find the first field matching the criteria (see #all_fields).
-
#first_tag(tag, subfields = '') {|result| ... } ⇒ Object
Find the first tag matching the criteria.
-
#initialize(xml_node) ⇒ MarcRecord
constructor
Create a new MarcRecord object.
-
#marc_dump ⇒ Object
Dump content to string.
-
#save(filename) ⇒ Object
Save the current MARC record to file.
-
#select_fields(tag, select_block = nil, &block) ⇒ Array
Get all fields matching search criteria.
-
#to_aseq ⇒ String
Dump Marc record in Aleph Sequential format.
-
#to_raw ⇒ XML node
Access to the XML node that was supplied to the constructor.
Constructor Details
#initialize(xml_node) ⇒ MarcRecord
Create a new MarcRecord object
27 28 29 30 31 |
# File 'lib/libis/metadata/marc_record.rb', line 27 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::Metadata::MarcRecord for it.
165 166 167 168 |
# File 'lib/libis/metadata/marc_record.rb', line 165 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::Metadata::MarcRecord for it.
172 173 174 175 176 |
# File 'lib/libis/metadata/marc_record.rb', line 172 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
#all ⇒ Hash
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.
45 46 47 48 |
# File 'lib/libis/metadata/marc_record.rb', line 45 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)
118 119 120 121 122 123 |
# File 'lib/libis/metadata/marc_record.rb', line 118 def all_fields(tag, subfields) r = (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.('700') { |v| v.subfield['4'] =~ /^(abc|def|xyz)$/ ? v : nil }.compact
78 79 80 81 82 83 |
# File 'lib/libis/metadata/marc_record.rb', line 78 def (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 |
#each ⇒ Array
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.
56 57 58 59 60 |
# File 'lib/libis/metadata/marc_record.rb', line 56 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.
138 139 140 141 142 |
# File 'lib/libis/metadata/marc_record.rb', line 138 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)
128 129 130 131 132 133 134 |
# File 'lib/libis/metadata/marc_record.rb', line 128 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.
108 109 110 111 112 113 |
# File 'lib/libis/metadata/marc_record.rb', line 108 def first_tag(tag, subfields = '') result = (tag, subfields).first return nil unless result return result unless block_given? yield result end |
#marc_dump ⇒ Object
Dump content to string.
145 146 147 |
# File 'lib/libis/metadata/marc_record.rb', line 145 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.
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/libis/metadata/marc_record.rb', line 151 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.
95 96 97 |
# File 'lib/libis/metadata/marc_record.rb', line 95 def select_fields(tag, select_block = nil, &block) (tag, nil, select_block, &block) end |
#to_aseq ⇒ String
Dump Marc record in Aleph Sequential format
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/libis/metadata/marc_record.rb', line 180 def to_aseq record = '' doc_number = tag('001').datas all.select {|t| t.is_a? Libis::Metadata::FixField}.each {|t| record += "#{format('%09s', doc_number)} #{t.tag} L #{t.datas}\n"} all.select {|t| t.is_a? Libis::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_raw ⇒ XML node
Access to the XML node that was supplied to the constructor
35 36 37 |
# File 'lib/libis/metadata/marc_record.rb', line 35 def to_raw @node end |