Class: Libis::Tools::Metadata::MarcRecord
- 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
Class Method Summary collapse
Instance Method Summary collapse
-
#all ⇒ Hash
Returns the internal data structure (a Hash) with all the MARC data.
- #all_fields(tag, subfields) ⇒ Object
-
#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(t, s) ⇒ Object
- #first_field(t, s) {|result| ... } ⇒ Object
-
#first_tag(tag, subfields = '') {|result| ... } ⇒ Object
Find the first field matching the criteria.
-
#initialize(xml_node) ⇒ MarcRecord
constructor
Create a new MarcRecord object.
- #marc_dump ⇒ Object
- #save(filename) ⇒ Object
- #select_fields(tag, select_block = nil, &block) ⇒ Object
- #to_aseq ⇒ Object
-
#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
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 |
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.
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 = (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.('700') { |v| v.subfield['4'] =~ /^(abc|def|xyz)$/ ? v : nil }.compact
79 80 81 82 83 84 |
# File 'lib/libis/tools/metadata/marc_record.rb', line 79 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.
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
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.
101 102 103 104 105 106 |
# File 'lib/libis/tools/metadata/marc_record.rb', line 101 def first_tag(tag, subfields = '') result = (tag, subfields).first return nil unless result return result unless block_given? yield result end |
#marc_dump ⇒ Object
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) (tag, nil, select_block, &block) end |
#to_aseq ⇒ Object
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_raw ⇒ XML node
Access to the XML node that was supplied to the constructor
36 37 38 |
# File 'lib/libis/tools/metadata/marc_record.rb', line 36 def to_raw @node end |