Class: Datacite::Mapping::BreakPreservingValueNode

Inherits:
XML::Mapping::SingleAttributeNode
  • Object
show all
Defined in:
lib/datacite/mapping/description.rb

Overview

XML mapping class preserving <br/> tags in description values

Instance Method Summary collapse

Instance Method Details

#obj_to_xml(obj, xml)

Converts a string value to a sequence of text nodes and <br/> tags. Implements SingleAttributeNode#obj_to_xml.

Parameters:

  • obj (Description)

    the object being serialized

  • xml (REXML::Element)

    the XML being written



45
46
47
48
49
50
51
52
# File 'lib/datacite/mapping/description.rb', line 45

def obj_to_xml(obj, xml)
  value_str = obj.value || ''
  values = value_str.split(%r{<br[^/]?/>|<br>[^<]*</br>})
  values.each_with_index do |v, i|
    xml.add_text(v)
    xml.add_element('br') unless i + 1 >= values.size
  end
end

#xml_to_obj(obj, xml)

Collapses a sequence of text nodes and <br/> tags into a single string value. Implements SingleAttributeNode#xml_to_obj.

Parameters:

  • obj (Description)

    the object being created

  • xml (REXML::Element)

    the XML being read



36
37
38
39
# File 'lib/datacite/mapping/description.rb', line 36

def xml_to_obj(obj, xml)
  value_str = xml.children.map { |c| c.respond_to?(:value) ? c.value : c.to_s }.join
  obj.value = value_str.strip
end