Class: Datacite::Mapping::BreakPreservingValueNode
- Inherits:
-
XML::Mapping::SingleAttributeNode
- Object
- XML::Mapping::SingleAttributeNode
- Datacite::Mapping::BreakPreservingValueNode
- Defined in:
- lib/datacite/mapping/description.rb
Overview
XML mapping class preserving <br/> tags in description values
Instance Method Summary collapse
-
#obj_to_xml(obj, xml)
Converts a string value to a sequence of text nodes and
<br/>tags. -
#xml_to_obj(obj, xml)
Collapses a sequence of text nodes and
<br/>tags into a single string value.
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.
40 41 42 43 44 45 46 47 |
# File 'lib/datacite/mapping/description.rb', line 40 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.
31 32 33 34 |
# File 'lib/datacite/mapping/description.rb', line 31 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 |