Skip to content

Commit

Permalink
Cache author info in Comment classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jpslav committed Apr 30, 2016
1 parent d136835 commit ae7782b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
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 << '">')
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

0 comments on commit ae7782b

Please sign in to comment.