Class: Uniword::CommentsPart

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Includes:
Enumerable
Defined in:
lib/uniword/comments_part.rb

Overview

Represents the comments part of a Word document (word/comments.xml).

This class manages the collection of all comments in a document. In OOXML packages, comments are stored separately from the main document in word/comments.xml.

The CommentsPart maintains a collection of Comment objects and handles serialization/deserialization to/from the comments.xml file.

Examples:

Create comments part

comments_part = Uniword::CommentsPart.new
comment = Uniword::Comment.new(
  author: "John Doe",
  text: "Please review"
)
comments_part.add_comment(comment)

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commentsArray<Comment>

Collection of all comments

Returns:

  • (Array<Comment>)

    the current value of comments



24
25
26
# File 'lib/uniword/comments_part.rb', line 24

def comments
  @comments
end

Instance Method Details

#[](index) ⇒ Comment?

Array-style access to comments (Array compatibility)

Parameters:

  • index (Integer)

    Position in the collection

Returns:

  • (Comment, nil)

    The comment at the position



109
110
111
# File 'lib/uniword/comments_part.rb', line 109

def [](index)
  comments[index]
end

#add_comment(comment) ⇒ Comment

Add a comment to the collection

Assigns the next sequential decimal ID when the comment has no ID yet or its ID collides with one already in the collection; explicit unique IDs are preserved.

Parameters:

  • comment (Comment)

    The comment to add

Returns:

  • (Comment)

    The added comment with assigned ID



46
47
48
49
50
51
52
53
54
55
# File 'lib/uniword/comments_part.rb', line 46

def add_comment(comment)
  unless comment.is_a?(Comment)
    raise ArgumentError,
          "comment must be a Comment instance"
  end

  comment.comment_id = next_comment_id if assign_id?(comment)
  comments << comment
  comment
end

#authorsArray<String>

Get all unique authors

Returns:

  • (Array<String>)

    List of unique author names



123
124
125
# File 'lib/uniword/comments_part.rb', line 123

def authors
  comments.map(&:author).uniq.compact
end

#clearvoid

This method returns an undefined value.

Clear all comments



130
131
132
# File 'lib/uniword/comments_part.rb', line 130

def clear
  comments.clear
end

#comments_by_author(author) ⇒ Array<Comment>

Get all comments by a specific author

Parameters:

  • author (String)

    The author name

Returns:

  • (Array<Comment>)

    Comments by the author



79
80
81
# File 'lib/uniword/comments_part.rb', line 79

def comments_by_author(author)
  comments.select { |c| c.author == author }
end

#countInteger

Get the number of comments

Returns:

  • (Integer)

    The count of comments



86
87
88
# File 'lib/uniword/comments_part.rb', line 86

def count
  comments.size
end

#each {|Comment| ... } ⇒ Enumerator, Array<Comment>

Iterate over the comments (Array compatibility)

Yields:

  • (Comment)

    Each comment in insertion order

Returns:



101
102
103
# File 'lib/uniword/comments_part.rb', line 101

def each(&block)
  comments.each(&block)
end

#empty?Boolean

Check if there are any comments

Returns:

  • (Boolean)

    true if empty



116
117
118
# File 'lib/uniword/comments_part.rb', line 116

def empty?
  comments.empty?
end

#find_comment(comment_id) ⇒ Comment?

Find a comment by ID

Parameters:

  • comment_id (String)

    The comment ID to find

Returns:

  • (Comment, nil)

    The comment if found, nil otherwise



61
62
63
# File 'lib/uniword/comments_part.rb', line 61

def find_comment(comment_id)
  comments.find { |c| c.comment_id == comment_id.to_s }
end

#inspectString

Provide detailed inspection for debugging

Returns:

  • (String)

    A readable representation of the comments part



137
138
139
# File 'lib/uniword/comments_part.rb', line 137

def inspect
  "#<Uniword::CommentsPart count=#{count} authors=#{authors.count}>"
end

#remove_comment(comment_id) ⇒ Comment?

Remove a comment by ID

Parameters:

  • comment_id (String)

    The comment ID to remove

Returns:

  • (Comment, nil)

    The removed comment if found, nil otherwise



69
70
71
72
73
# File 'lib/uniword/comments_part.rb', line 69

def remove_comment(comment_id)
  comment = find_comment(comment_id)
  comments.delete(comment) if comment
  comment
end

#sizeInteger

Get the number of comments (Array compatibility)

Returns:

  • (Integer)

    The count of comments



93
94
95
# File 'lib/uniword/comments_part.rb', line 93

def size
  comments.size
end