Class: Uniword::Comment

Inherits:
Element
  • Object
show all
Defined in:
lib/uniword/comment.rb

Overview

Represents a comment in a Word document.

Comments are annotations attached to content ranges, containing author information, timestamp, and comment text. They are part of Word’s review and collaboration features.

In OOXML, comments are stored in word/comments.xml and referenced via commentRangeStart, commentRangeEnd, and commentReference elements.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Element

abstract!, abstract?

Constructor Details

#initialize(attributes = {}) ⇒ Comment

Initialize a new comment

Parameters:

  • attributes (Hash) (defaults to: {})

    Comment attributes

Options Hash (attributes):

  • :author (String)

    Author name

  • :text (String)

    Comment text (creates paragraph)

  • :date (Time, String)

    Comment date

  • :initials (String)

    Author initials

  • :comment_id (String)

    Unique ID (auto-generated if not provided)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/uniword/comment.rb', line 59

def initialize(attributes = {})
  # Extract text before calling super to handle it specially
  text_content = attributes.delete(:text)

  super

  # Auto-generate comment_id if not provided
  @comment_id ||= generate_comment_id

  # Set date to current time if not provided
  @date ||= format_date(Time.now)

  # Add text as a paragraph if provided
  return unless text_content && !text_content.empty?

  para = Uniword::Wordprocessingml::Paragraph.new
  run = Uniword::Wordprocessingml::Run.new(text: text_content)
  para.runs << run
  paragraphs << para
end

Instance Attribute Details

#authorString

Comment author name

Returns:

  • (String)

    the current value of author



21
22
23
# File 'lib/uniword/comment.rb', line 21

def author
  @author
end

#comment_idInteger

Unique comment identifier

Returns:

  • (Integer)

    the current value of comment_id



21
22
23
# File 'lib/uniword/comment.rb', line 21

def comment_id
  @comment_id
end

#dateTime

Comment creation date/time

Returns:

  • (Time)

    the current value of date



21
22
23
# File 'lib/uniword/comment.rb', line 21

def date
  @date
end

#initialsString

Author initials (optional)

Returns:

  • (String)

    the current value of initials



21
22
23
# File 'lib/uniword/comment.rb', line 21

def initials
  @initials
end

#textString

Get the plain text content of this comment Concatenates text from all paragraphs

Returns:

  • (String)

    The combined text from all paragraphs



21
22
23
# File 'lib/uniword/comment.rb', line 21

def text
  @text
end

Instance Method Details

#accept(visitor) ⇒ Object

Accept a visitor for the visitor pattern

Parameters:

  • visitor (Object)

    The visitor object

Returns:

  • (Object)

    The result of the visit operation



84
85
86
# File 'lib/uniword/comment.rb', line 84

def accept(visitor)
  visitor.visit_comment(self)
end

#empty?Boolean

Check if comment is empty (has no paragraphs or all paragraphs are empty)

Returns:

  • (Boolean)

    true if empty



99
100
101
# File 'lib/uniword/comment.rb', line 99

def empty?
  paragraphs.empty? || paragraphs.all?(&:empty?)
end

#inspectString

Provide detailed inspection for debugging

Returns:

  • (String)

    A readable representation of the comment



106
107
108
109
110
# File 'lib/uniword/comment.rb', line 106

def inspect
  text_preview = text[0..50]
  text_preview += "..." if text.length > 50
  "#<Uniword::Comment id=#{comment_id.inspect} author=#{author.inspect} text=#{text_preview.inspect}>"
end

#valid?Boolean

Check if comment is valid (has required attributes)

Returns:

  • (Boolean)

    true if valid



115
116
117
# File 'lib/uniword/comment.rb', line 115

def valid?
  required_attributes_valid?
end