Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache author info in Comment classes #465

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/axlsx/workbook/worksheet/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def vml_shape
# the comment.
# @return [Integer]
def author_index
@comments.authors.index(author)
@comments.author_index(author)
end

# @see ref
Expand All @@ -61,7 +61,6 @@ def ref=(v)
# @param [String] str
# @return [String]
def to_xml_string(str = "")
author = @comments.authors[author_index]
str << ('<comment ref="' << ref << '" authorId="' << author_index.to_s << '">')
Copy link
Author

@jpslav jpslav Apr 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this line because I'm pretty sure author is already an attribute of this Comment class.

str << '<text>'
unless author.to_s == ""
Expand Down
17 changes: 16 additions & 1 deletion lib/axlsx/workbook/worksheet/comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,21 @@ def add_comment(options={})
raise ArgumentError, "Comment requires ref" unless options[:ref]
self << Comment.new(self, options)
yield last if block_given?
clear_author_caches
last
end

# A sorted list of the unique authors in the contained comments
# @return [Array]
def authors
map { |comment| comment.author.to_s }.uniq.sort
@authors ||= map { |comment| comment.author.to_s }.uniq.sort
end

# Returns the index of the given author in the sorted authors array
# @param [String] author An author
# @return [Integer]
def author_index(author)
(@author_indexes ||= {})[author] ||= authors.index(author)
end

# The relationships required by this object
Expand All @@ -77,6 +85,13 @@ def to_xml_string(str="")

end

protected

def clear_author_caches
@authors = nil
@author_indexes = nil
end

end

end