Browse files

caching fingerprint & test update

  • Loading branch information...
1 parent a809c32 commit 626a29a288e69dc81bc60137008eb47392d31980 @mankyuhan committed Jun 7, 2012
Showing with 11 additions and 5 deletions.
  1. +6 −4 lib/paperclip/io_adapters/stringio_adapter.rb
  2. +5 −1 test/io_adapters/stringio_adapter_test.rb
View
10 lib/paperclip/io_adapters/stringio_adapter.rb
@@ -24,10 +24,12 @@ def size
end
def fingerprint
- rewind # start reading from the beginning
- finger = Digest::MD5.hexdigest(read)
- rewind
- finger
+ if (@cached_fingerprint.nil?)
+ rewind # start reading from the beginning
+ @cached_fingerprint = Digest::MD5.hexdigest(read)
+ rewind # for later read()
+ end
+ @cached_fingerprint
end
def read(length = nil, buffer = nil)
View
6 test/io_adapters/stringio_adapter_test.rb
@@ -34,11 +34,15 @@ class StringioFileProxyTest < Test::Unit::TestCase
assert_equal Digest::MD5.hexdigest(@contents), @subject.fingerprint
end
+ should "generate correct fingerprint after read" do
+ fingerprint = Digest::MD5.hexdigest(@subject.read)
+ assert_equal fingerprint, @subject.fingerprint
+ end
+
should "generate same fingerprint" do
assert_equal @subject.fingerprint, @subject.fingerprint
end
-
should "return the data contained in the StringIO" do
assert_equal "abc123", @subject.read
end

1 comment on commit 626a29a

@mankyuhan
Owner

I added one more test case to point out why rewind is necessary before generating fingerprint. And thank you for the caching suggestion!

Please sign in to comment.