Skip to content

Commit

Permalink
caching fingerprint & test update
Browse files Browse the repository at this point in the history
  • Loading branch information
mankyuhan committed Jun 7, 2012
1 parent a809c32 commit 626a29a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/paperclip/io_adapters/stringio_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion test/io_adapters/stringio_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

1 comment on commit 626a29a

@mankyuhan
Copy link
Owner Author

Choose a reason for hiding this comment

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

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.