Class: Uniword::CommentsPart

Inherits:
Lutaml::Model::Serializable
  • Object
show all
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

Constructor Details

#initialize(attributes = {}) ⇒ CommentsPart

Initialize a new comments part

Parameters:

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

    Comments part attributes



39
40
41
42
# File 'lib/uniword/comments_part.rb', line 39

def initialize(attributes = {})
  super
  @comment_counter = 0
end

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

#add_comment(comment) ⇒ Comment

Add a comment to the collection

Parameters:

  • comment (Comment)

    The comment to add

Returns:

  • (Comment)

    The added comment with assigned ID



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/uniword/comments_part.rb', line 48

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

  # Assign sequential ID if not already set
  comment.comment_id = next_comment_id unless comment.comment_id && !comment.comment_id.empty?

  comments << comment
  comment
end

#authorsArray<String>

Get all unique authors

Returns:

  • (Array<String>)

    List of unique author names



104
105
106
# File 'lib/uniword/comments_part.rb', line 104

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

#clearvoid

This method returns an undefined value.

Clear all comments



111
112
113
114
# File 'lib/uniword/comments_part.rb', line 111

def clear
  comments.clear
  @comment_counter = 0
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



83
84
85
# File 'lib/uniword/comments_part.rb', line 83

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

#countInteger

Get the number of comments

Returns:

  • (Integer)

    The count of comments



90
91
92
# File 'lib/uniword/comments_part.rb', line 90

def count
  comments.size
end

#empty?Boolean

Check if there are any comments

Returns:

  • (Boolean)

    true if empty



97
98
99
# File 'lib/uniword/comments_part.rb', line 97

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



65
66
67
# File 'lib/uniword/comments_part.rb', line 65

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



119
120
121
# File 'lib/uniword/comments_part.rb', line 119

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



73
74
75
76
77
# File 'lib/uniword/comments_part.rb', line 73

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