Class: Prosereflect::TableCell

Inherits:
Node
  • Object
show all
Defined in:
lib/prosereflect/table_cell.rb

Direct Known Subclasses

TableHeader

Constant Summary collapse

PM_TYPE =
'table_cell'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#add_child, #find_all, #find_children, #find_first, #marks, #marks=, #parse_content, #process_attrs_data, #raw_marks, #to_yaml

Constructor Details

#initialize(attributes = {}) ⇒ TableCell

Returns a new instance of TableCell.



18
19
20
21
# File 'lib/prosereflect/table_cell.rb', line 18

def initialize(attributes = {})
  attributes[:content] ||= []
  super
end

Class Method Details

.create(attrs = nil) ⇒ Object



23
24
25
# File 'lib/prosereflect/table_cell.rb', line 23

def self.create(attrs = nil)
  new(type: PM_TYPE, attrs: attrs, content: [])
end

Instance Method Details

#add_paragraph(text = nil) ⇒ Object

Add a paragraph to the cell



42
43
44
45
46
47
48
49
# File 'lib/prosereflect/table_cell.rb', line 42

def add_paragraph(text = nil)
  paragraph = Paragraph.create

  paragraph.add_text(text) if text

  add_child(paragraph)
  paragraph
end

#linesObject



37
38
39
# File 'lib/prosereflect/table_cell.rb', line 37

def lines
  text_content.split("\n").map(&:strip).reject(&:empty?)
end

#paragraphsObject



27
28
29
30
31
# File 'lib/prosereflect/table_cell.rb', line 27

def paragraphs
  return [] unless content

  content.select { |node| node.is_a?(Paragraph) }
end

#text_contentObject



33
34
35
# File 'lib/prosereflect/table_cell.rb', line 33

def text_content
  paragraphs.map(&:text_content).join("\n")
end

#to_hObject

Override to_h to handle empty content and attributes properly



52
53
54
55
56
57
58
59
60
# File 'lib/prosereflect/table_cell.rb', line 52

def to_h
  result = super
  result['content'] ||= []
  if result['attrs']
    result['attrs'] = result['attrs'].is_a?(Hash) && result['attrs'][:attrs] ? result['attrs'][:attrs] : result['attrs']
    result.delete('attrs') if result['attrs'].empty?
  end
  result
end