Class: Metacrunch::Marcxml::Document
- Inherits:
-
Object
- Object
- Metacrunch::Marcxml::Document
- Defined in:
- lib/metacrunch/marcxml/document.rb,
lib/metacrunch/marcxml/document/subfield.rb,
lib/metacrunch/marcxml/document/datafield.rb,
lib/metacrunch/marcxml/document/controlfield.rb,
lib/metacrunch/marcxml/document/subfield_set.rb,
lib/metacrunch/marcxml/document/datafield_set.rb
Defined Under Namespace
Classes: Controlfield, Datafield, DatafieldSet, Subfield, SubfieldSet
Instance Method Summary collapse
-
#[](query_string) ⇒ Array<String>
Returns a control field value or data field/sub field values matching the given query string.
-
#controlfield(tag) ⇒ Metacrunch::Marcxml::Document::Controlfield?
Returns the control field matching the given tag or nil if a control field with the given tag does not exist.
-
#datafields(tag = nil, ind1: nil, ind2: nil) ⇒ Metacrunch::Marcxml::Document::DatafieldSet
Returns the data fields matching the given tag(s) and/or ind1/ind2.
- #empty? ⇒ Boolean
-
#initialize ⇒ Document
constructor
A new instance of Document.
Constructor Details
#initialize ⇒ Document
Returns a new instance of Document.
11 12 13 14 |
# File 'lib/metacrunch/marcxml/document.rb', line 11 def initialize @controlfields_map = {} @datafields_map = {} end |
Instance Method Details
#[](query_string) ⇒ Array<String>
Returns a control field value or data field/sub field values matching the given query string.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/metacrunch/marcxml/document.rb', line 100 def [](query_string) # Control field query if query_string.starts_with?("00") # Example: "005" # [0..2] => Control field tag tag = query_string[0..2].presence controlfield(tag)&.value # Data field / sub field query else # Example: "100**a,e" # [0..2] => Data field tag (required). # [3] => Ind1, defaults to `*`, which matches any indicator 1 (optional). ` `, `-` or `_` will be interpreted as `blank`. # [4] => Ind2, defaults to `*`, which matches any indicator 2 (optional). ` `, `-` or `_` will be interpreted as `blank`. # [5] => Sub field code(s) (optional). tag = query_string[0..2].presence ind1 = query_string[3].presence ind1 = nil if ind1 == "*" ind1 = :blank if ind1 == "-" || ind1 == "_" || ind1 == " " ind2 = query_string[4].presence ind2 = nil if ind2 == "*" ind2 = :blank if ind2 == "-" || ind2 == "_" || ind2 == " " subfield_codes = query_string[5..-1]&.split(",")&.map(&:strip).compact.presence datafields(tag, ind1: ind1, ind2: ind2).subfields(subfield_codes).values end end |
#controlfield(tag) ⇒ Metacrunch::Marcxml::Document::Controlfield?
Returns the control field matching the given tag or nil if a control field with the given tag does not exist.
33 34 35 |
# File 'lib/metacrunch/marcxml/document.rb', line 33 def controlfield(tag) @controlfields_map[normalize_tag(tag)] end |
#datafields(tag = nil, ind1: nil, ind2: nil) ⇒ Metacrunch::Marcxml::Document::DatafieldSet
Returns the data fields matching the given tag(s) and/or ind1/ind2.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/metacrunch/marcxml/document.rb', line 63 def datafields(tag = nil, ind1: nil, ind2: nil) matched_datafields = if tag.nil? @datafields_map.values.flatten(1) elsif tag.is_a?(Enumerable) tag.map{ |_tag| @datafields_map[normalize_tag(_tag)] }.compact.flatten(1) else @datafields_map[normalize_tag(tag)] end matched_datafields = (matched_datafields || []).select do |datafield| match_indicator(ind1, datafield.ind1) && match_indicator(ind2, datafield.ind2) end DatafieldSet.new(matched_datafields) end |
#empty? ⇒ Boolean
16 17 18 |
# File 'lib/metacrunch/marcxml/document.rb', line 16 def empty? @controlfields_map.blank? && @datafields_map.blank? end |