Module: Coradoc::CoreModel::ChildrenContent

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

Overview

Shared module for models that carry mixed content (strings + InlineElements) as a children array alongside a string content attribute.

Included by Block, ListItem, TableCell, and InlineElement to provide:

  • children array management (reader, writer, initialize)

  • renderable_content: prefers children when they contain non-string objects, falls back to the content attribute otherwise

  • flat_text: one-line plain-text extraction from mixed content

  • to_hash: includes children in serialization output

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



15
16
17
# File 'lib/coradoc/core_model/children_content.rb', line 15

def self.included(base)
  base.attr_reader :children
end

Instance Method Details

#children=(value) ⇒ Object



24
25
26
# File 'lib/coradoc/core_model/children_content.rb', line 24

def children=(value)
  @children = value || []
end

#flat_textObject

Flatten renderable_content to a single plain-text string. InlineElements are rendered as their content.to_s.



40
41
42
43
44
45
46
47
# File 'lib/coradoc/core_model/children_content.rb', line 40

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

#initialize(args = {}) ⇒ Object



19
20
21
22
# File 'lib/coradoc/core_model/children_content.rb', line 19

def initialize(args = {})
  @children = args.delete(:children) || []
  super(args)
end

#renderable_contentObject

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



31
32
33
34
35
36
# File 'lib/coradoc/core_model/children_content.rb', line 31

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

  children
end

#to_hashObject



49
50
51
52
53
# File 'lib/coradoc/core_model/children_content.rb', line 49

def to_hash
  super.tap do |h|
    h['children'] = serialize_children(children) if children&.any?
  end
end