summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx/rels/relationship.rb
diff options
context:
space:
mode:
authorStefan Daschek <[email protected]>2013-07-08 16:36:42 +0200
committerStefan Daschek <[email protected]>2013-07-08 16:36:42 +0200
commite5e79814670569838621e67249927c104006bcca (patch)
tree011363a114926d71aaa15c995c6ea132440c5ba6 /lib/axlsx/rels/relationship.rb
parent2a4c39f7ae863ac7e4d14e0110aba11fce4a822f (diff)
downloadcaxlsx-e5e79814670569838621e67249927c104006bcca.tar.gz
caxlsx-e5e79814670569838621e67249927c104006bcca.zip
Implement Relationship.clear_cached_instances, use it before serializing the package.
This is necessary to make serialization idempotent (i.e. make sure that Relationship instances are generated with the same IDs everytime the package is serialized). It also fixes a memory leak if Axlsx is used in a long running server process (eg a Rails app).
Diffstat (limited to 'lib/axlsx/rels/relationship.rb')
-rw-r--r--lib/axlsx/rels/relationship.rb18
1 files changed, 17 insertions, 1 deletions
diff --git a/lib/axlsx/rels/relationship.rb b/lib/axlsx/rels/relationship.rb
index 5e75a98a..a6b7bdd2 100644
--- a/lib/axlsx/rels/relationship.rb
+++ b/lib/axlsx/rels/relationship.rb
@@ -11,7 +11,23 @@ module Axlsx
@instances ||= []
end
- # Generate and return a unique id. Used for setting {#Id}.
+ # Clear cached instances.
+ #
+ # This should be called before serializing a package (see {Package#serialize} and
+ # {Package#to_stream}) to make sure that serialization is idempotent (i.e.
+ # Relationship instances are generated with the same IDs everytime the package
+ # is serialized).
+ #
+ # Also, calling this avoids memory leaks (cached instances lingering around
+ # forever).
+ def clear_cached_instances
+ @instances = []
+ end
+
+ # Generate and return a unique id (eg. `rId123`) Used for setting {#Id}.
+ #
+ # The generated id depends on the number of cached instances, so using
+ # {clear_cached_instances} will automatically reset the generated ids, too.
# @return [String]
def next_free_id
"rId#{@instances.size + 1}"