class Metacrunch::Mab2::Document

Public Class Methods

from_aleph_mab_xml(xml) click to toggle source

TODO: Remove form 2.0

# File lib/metacrunch/mab2/document.rb, line 29
def self.from_aleph_mab_xml(xml)
  d = ActiveSupport::Deprecation.new("2.0", "metacrunch-mab2")
  d.deprecation_warning("#from_aleph_mab_xml", "use #from_mab_xml instead")
  self.from_mab_xml(xml)
end
from_mab_xml(xml) click to toggle source

@param [String] xml repesenting a MAB document in Aleph MAB XML format @return [Metacrunch::Mab2::Document]

# File lib/metacrunch/mab2/document.rb, line 24
def self.from_mab_xml(xml)
  MabXmlParser.new.parse(xml)
end
new() click to toggle source
# File lib/metacrunch/mab2/document.rb, line 35
def initialize
  @controlfields = {}
  @datafields = {}
end

Public Instance Methods

add_controlfield(controlfield) click to toggle source

Adds a new control field.

@param [Metacrunch::Mab2::Document::Controlfield] controlfield

# File lib/metacrunch/mab2/document.rb, line 59
def add_controlfield(controlfield)
  @controlfields[controlfield.tag] = controlfield
end
add_datafield(datafield) click to toggle source

Adds a new data field.

@param [Metacrunch::Mab2::Document::Datafield] datafield

# File lib/metacrunch/mab2/document.rb, line 97
def add_datafield(datafield)
  (@datafields[datafield.tag] ||= []) << datafield
end
controlfield(tag) click to toggle source

Returns the control field matching the given tag.

@param [String] tag of the control field @return [Controlfield] control field with the given tag.

# File lib/metacrunch/mab2/document.rb, line 50
def controlfield(tag)
  @controlfields[tag] || Controlfield.new(tag)
end
datafields(tag = nil, ind1: nil, ind2: nil) click to toggle source

Returns the data fields matching the given tag and ind1/ind2.

@param [String, nil] tag of the data field. Can be nil to match any data field. @param [String, nil] ind1 filter for ind1. Can be nil to match any indicator 1. @param [String, nil] ind2 filter for ind2. Can be nil to match any indicator 2. @return [Metacrunch::Mab2::Document::DatafieldSet] data field with the given tag and ind1/ind2.

The set is empty if a matching field with the tag and/or ind1/ind2 doesn't exists.
# File lib/metacrunch/mab2/document.rb, line 76
def datafields(tag = nil, ind1: nil, ind2: nil)
  if tag.nil?
    DatafieldSet.new(@datafields.values.flatten(1))
  else
    set = DatafieldSet.new(@datafields[tag] || [])
    return set if set.empty? || (ind1.nil? && ind2.nil?)

    ind1 = map_indicator(ind1)
    ind2 = map_indicator(ind2)

    set.select do |_datafield|
      check_indicator(ind1, _datafield.ind1) && check_indicator(ind2, _datafield.ind2)
    end
  end
end
to_xml() click to toggle source

Serialization


# File lib/metacrunch/mab2/document.rb, line 105
def to_xml
  builder = ::Builder::XmlMarkup.new(indent: 2)
  builder.instruct!(:xml, :encoding => "UTF-8")
  builder.mab_xml do
    controlfields_struct.values.each do |_controlfield|
      _controlfield.to_xml(builder)
    end

    @datafields.values.each do |_datafield_set|
      _datafield_set.to_xml(builder)
    end
  end
end

Private Instance Methods

check_indicator(requested_ind, datafield_ind) click to toggle source
# File lib/metacrunch/mab2/document.rb, line 125
def check_indicator(requested_ind, datafield_ind)
  if !requested_ind
    true
  elsif requested_ind == :blank && (datafield_ind == " " || datafield_ind == "-" || datafield_ind.nil?)
    true
  elsif requested_ind == datafield_ind
    true
  elsif requested_ind.is_a?(Array) && requested_ind.include?(datafield_ind)
    true
  else
    false
  end
end
map_indicator(ind) click to toggle source
# File lib/metacrunch/mab2/document.rb, line 121
def map_indicator(ind)
  ind.is_a?(Array) ? ind.map { |_el| _el == :blank ? [" ", "-", nil] : _el }.flatten(1) : ind
end