Class: Uniword::Wordprocessingml::Text

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/uniword/wordprocessingml/text.rb

Overview

Text content with xml:space attribute support

Element: <w:t xml:space="preserve">

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cast(value) ⇒ Text?

Handle type conversion for lutaml-model's attribute system. When used as an attribute type, lutaml-model calls cast() to convert incoming values to the proper type.

Parameters:

  • value (String, Text, nil)

    The value to cast

Returns:

  • (Text, nil)

    The properly typed Text object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/uniword/wordprocessingml/text.rb', line 29

def self.cast(value)
  case value
  when Text
    value
  when nil
    nil
  else
    content_str = value.to_s
    text_obj = new(content: content_str)
    text_obj.xml_space = "preserve" if preserve_whitespace?(content_str)
    text_obj
  end
end

.preserve_whitespace?(content) ⇒ Boolean

Whether the content requires xml:space="preserve" per OOXML spec. Leading/trailing spaces, tabs, and newlines all need preservation.

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/uniword/wordprocessingml/text.rb', line 45

def self.preserve_whitespace?(content)
  content.start_with?(" ") || content.end_with?(" ") ||
    content.include?("\t") || content.include?("\n")
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Allow comparison with strings for test compatibility



61
62
63
64
65
# File 'lib/uniword/wordprocessingml/text.rb', line 61

def ==(other)
  return super unless other.is_a?(String)

  content == other
end

#include?(substr) ⇒ Boolean

Support String#include? for test compatibility

Returns:

  • (Boolean)


70
71
72
# File 'lib/uniword/wordprocessingml/text.rb', line 70

def include?(substr)
  content.to_s.include?(substr)
end

#space_preserve?Boolean

Check if space should be preserved

Returns:

  • (Boolean)


75
76
77
# File 'lib/uniword/wordprocessingml/text.rb', line 75

def space_preserve?
  xml_space == "preserve"
end

#textObject

Get the actual text value



51
52
53
# File 'lib/uniword/wordprocessingml/text.rb', line 51

def text
  content
end

#to_sObject

String conversion for compatibility with code expecting plain strings



56
57
58
# File 'lib/uniword/wordprocessingml/text.rb', line 56

def to_s
  content.to_s
end