Class: Prosereflect::TableHeader

Inherits:
TableCell
  • Object
show all
Defined in:
lib/prosereflect/table_header.rb

Overview

TableHeader class represents a header cell in a table (<th> tag). It inherits from TableCell but adds header-specific attributes.

Constant Summary collapse

PM_TYPE =
'table_header'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TableCell

#add_paragraph, #lines, #paragraphs, #text_content

Methods inherited from Node

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

Constructor Details

#initialize(attributes = {}) ⇒ TableHeader

Returns a new instance of TableHeader.



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

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

Class Method Details

.create(attrs = nil) ⇒ Object



27
28
29
# File 'lib/prosereflect/table_header.rb', line 27

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

Instance Method Details

#abbrObject



57
58
59
# File 'lib/prosereflect/table_header.rb', line 57

def abbr
  attrs&.[]('abbr')
end

#abbr=(abbr_text) ⇒ Object

Set abbreviated version of the header content



52
53
54
55
# File 'lib/prosereflect/table_header.rb', line 52

def abbr=(abbr_text)
  self.attrs ||= {}
  attrs['abbr'] = abbr_text
end

#add_text(text, marks = nil) ⇒ Object

Add text to the last paragraph, or create a new one if none exists



32
33
34
35
36
37
# File 'lib/prosereflect/table_header.rb', line 32

def add_text(text, marks = nil)
  last_paragraph = content&.last
  last_paragraph = add_paragraph if !last_paragraph || !last_paragraph.is_a?(Paragraph)
  last_paragraph.add_text(text, marks)
  self
end

#colspanObject



69
70
71
# File 'lib/prosereflect/table_header.rb', line 69

def colspan
  attrs&.[]('colspan')
end

#colspan=(span) ⇒ Object

Set the number of columns this header spans



62
63
64
65
66
67
# File 'lib/prosereflect/table_header.rb', line 62

def colspan=(span)
  return unless span.to_i.positive?

  self.attrs ||= {}
  attrs['colspan'] = span.to_i
end

#header_attributesObject

Get header attributes as a hash



74
75
76
77
78
79
80
# File 'lib/prosereflect/table_header.rb', line 74

def header_attributes
  {
    scope: scope,
    abbr: abbr,
    colspan: colspan
  }.compact
end

#scopeObject



47
48
49
# File 'lib/prosereflect/table_header.rb', line 47

def scope
  attrs&.[]('scope')
end

#scope=(scope_value) ⇒ Object

Set the scope of the header (row, col, rowgroup, or colgroup)



40
41
42
43
44
45
# File 'lib/prosereflect/table_header.rb', line 40

def scope=(scope_value)
  return unless %w[row col rowgroup colgroup].include?(scope_value)

  self.attrs ||= {}
  attrs['scope'] = scope_value
end

#to_hObject

Override to_h to exclude nil attributes



83
84
85
86
87
88
89
90
# File 'lib/prosereflect/table_header.rb', line 83

def to_h
  result = super
  if result['attrs']
    result['attrs'].reject! { |_, v| v.nil? }
    result.delete('attrs') if result['attrs'].empty?
  end
  result
end