Skip to content

Commit ae7782b

Browse files
committed
Cache author info in Comment classes
1 parent d136835 commit ae7782b

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

lib/axlsx/workbook/worksheet/comment.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def vml_shape
4747
# the comment.
4848
# @return [Integer]
4949
def author_index
50-
@comments.authors.index(author)
50+
@comments.author_index(author)
5151
end
5252

5353
# @see ref
@@ -61,7 +61,6 @@ def ref=(v)
6161
# @param [String] str
6262
# @return [String]
6363
def to_xml_string(str = "")
64-
author = @comments.authors[author_index]
6564
str << ('<comment ref="' << ref << '" authorId="' << author_index.to_s << '">')
6665
str << '<text>'
6766
unless author.to_s == ""

lib/axlsx/workbook/worksheet/comments.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,21 @@ def add_comment(options={})
4444
raise ArgumentError, "Comment requires ref" unless options[:ref]
4545
self << Comment.new(self, options)
4646
yield last if block_given?
47+
clear_author_caches
4748
last
4849
end
4950

5051
# A sorted list of the unique authors in the contained comments
5152
# @return [Array]
5253
def authors
53-
map { |comment| comment.author.to_s }.uniq.sort
54+
@authors ||= map { |comment| comment.author.to_s }.uniq.sort
55+
end
56+
57+
# Returns the index of the given author in the sorted authors array
58+
# @param [String] author An author
59+
# @return [Integer]
60+
def author_index(author)
61+
(@author_indexes ||= {})[author] ||= authors.index(author)
5462
end
5563

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

7886
end
7987

88+
protected
89+
90+
def clear_author_caches
91+
@authors = nil
92+
@author_indexes = nil
93+
end
94+
8095
end
8196

8297
end

0 commit comments

Comments
 (0)