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
42
# 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)
    # Preserve whitespace for XML serialization
    text_obj.xml_space = "preserve" if content_str.start_with?(" ") || content_str.end_with?(" ") || content_str.include?("\t")
    text_obj
  end
end

Instance Method Details

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

Allow comparison with strings for test compatibility



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

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

  content == other
end

#include?(substr) ⇒ Boolean

Support String#include? for test compatibility

Returns:

  • (Boolean)


64
65
66
# File 'lib/uniword/wordprocessingml/text.rb', line 64

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

#space_preserve?Boolean

Check if space should be preserved

Returns:

  • (Boolean)


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

def space_preserve?
  xml_space == "preserve"
end

#textObject

Get the actual text value



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

def text
  content
end

#to_sObject

String conversion for compatibility with code expecting plain strings



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

def to_s
  content.to_s
end