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, #copy, #cut, #descendants, #eq?, #find_all, #find_children, #find_first, #marks, #marks=, #node, #node_size, #nodes_between, #parse_content, #process_attrs_data, #raw_marks, #resolve, #text?, #to_yaml

Constructor Details

#initialize(attributes = {}) ⇒ TableCell

Returns a new instance of TableCell.



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

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

Class Method Details

.create(attrs = nil) ⇒ Object



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

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



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

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

  paragraph.add_text(text) if text

  add_child(paragraph)
  paragraph
end

#linesObject



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

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

#paragraphsObject



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

def paragraphs
  return [] unless content

  content.grep(Paragraph)
end

#text_contentObject



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

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

#to_hObject

Override to_h to handle empty content and attributes properly



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

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