Class: Prosereflect::TableCell
- Defined in:
- lib/prosereflect/table_cell.rb
Direct Known Subclasses
Constant Summary collapse
- PM_TYPE =
'table_cell'
Class Method Summary collapse
Instance Method Summary collapse
-
#add_paragraph(text = nil) ⇒ Object
Add a paragraph to the cell.
-
#initialize(attributes = {}) ⇒ TableCell
constructor
A new instance of TableCell.
- #lines ⇒ Object
- #paragraphs ⇒ Object
- #text_content ⇒ Object
-
#to_h ⇒ Object
Override to_h to handle empty content and attributes properly.
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 |
#lines ⇒ Object
37 38 39 |
# File 'lib/prosereflect/table_cell.rb', line 37 def lines text_content.split("\n").map(&:strip).reject(&:empty?) end |
#paragraphs ⇒ Object
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_content ⇒ Object
33 34 35 |
# File 'lib/prosereflect/table_cell.rb', line 33 def text_content paragraphs.map(&:text_content).join("\n") end |
#to_h ⇒ Object
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 |