Module: Coradoc::CoreModel::ChildrenContent

Included in:
Block, InlineElement, ListItem, TableCell
Defined in:
lib/coradoc/core_model/children_content.rb

Overview

Shared helpers for models that carry mixed-content children (TextContent + InlineElement + Block instances) alongside a string content attribute.

Included by Block, ListItem, TableCell, and InlineElement. The children attribute is declared as

attribute :children, Base, collection: true

on each including class. This module overrides the setter to auto-wrap raw strings as TextContent, keeping all callers simple.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Override the children= setter to auto-wrap strings as TextContent. This is defined via define_method so it always overrides the lutaml-generated setter, regardless of include order.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/coradoc/core_model/children_content.rb', line 18

def self.included(base)
  super

  base.define_method(:children=) do |value|
    wrapped = Array(value).map do |item|
      next nil if item.nil?
      next item if item.is_a?(CoreModel::Base)

      CoreModel::TextContent.new(text: item.to_s)
    end.compact
    instance_variable_set(:@children, wrapped)
  end
end

Instance Method Details

#flat_textObject

Flatten renderable_content to a single plain-text string.



43
44
45
46
47
48
49
50
# File 'lib/coradoc/core_model/children_content.rb', line 43

def flat_text
  rc = renderable_content
  case rc
  when String then rc
  when Array then rc.map { |c| c.is_a?(TextContent) ? c.text : c.content.to_s }.join
  else rc.to_s
  end
end

#renderable_contentObject

Get content for rendering, preferring children over content. When children are all TextContent (plain text), use the content attribute instead since it already has proper spacing between lines.



35
36
37
38
39
40
# File 'lib/coradoc/core_model/children_content.rb', line 35

def renderable_content
  return content if children.nil? || children.none?
  return content if content && children.all?(TextContent)

  children
end