Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ActiveFedora::Checksum class to encapsulate file digest info. #919

Merged
merged 1 commit into from
Oct 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/active_fedora.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module ActiveFedora #:nodoc:
autoload :CachingConnection
autoload :Callbacks
autoload :ChangeSet
autoload :Checksum
autoload :CleanConnection
autoload :Config
autoload :Core
Expand Down
15 changes: 15 additions & 0 deletions lib/active_fedora/checksum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module ActiveFedora
class Checksum

attr_reader :uri, :value, :algorithm

def initialize(file)
@uri = file.digest.first
if @uri
@algorithm, @value = @uri.path.split(":")
@algorithm.upcase!
end
end

end
end
4 changes: 4 additions & 0 deletions lib/active_fedora/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ def metadata
@metadata ||= ActiveFedora::WithMetadata::MetadataNode.new(self)
end

def checksum
ActiveFedora::Checksum.new(self)
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe:

@checksum ||= ActiveFedora::Checksum.new(self)

and then set @checksum = nil under #refresh.

Copy link
Member Author

Choose a reason for hiding this comment

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

Personally I don't think the operation cost warrants the complexity of caching. Or if it does, perhaps it belongs on ActiveFedora::File::Attributes#digest. @jcoyne

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I'd buy that.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, well maybe we merge this and address any caching separately if anybody thinks that's important?

end

def content_changed?
return true if new_record? and !local_or_remote_content(false).blank?
local_or_remote_content(false) != @ds_content
Expand Down
27 changes: 27 additions & 0 deletions spec/unit/checksum_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "spec_helper"

module ActiveFedora
describe Checksum do

subject { described_class.new(file) }

let(:uri) { ::RDF::URI("urn:sha1:bb3200c2ddaee4bd7b9a4dc1ad3e10ed886eaef1") }

describe "when initialized with a file having a digest" do
let(:file) { double(digest: [uri]) }

its(:uri) { is_expected.to eq(uri) }
its(:value) { is_expected.to eq("bb3200c2ddaee4bd7b9a4dc1ad3e10ed886eaef1") }
its(:algorithm) { is_expected.to eq("SHA1") }
end

describe "when initialized with a file not having a digest" do
let(:file) { double(digest: []) }

its(:uri) { is_expected.to be_nil }
its(:value) { is_expected.to be_nil }
its(:algorithm) { is_expected.to be_nil }
end

end
end
14 changes: 14 additions & 0 deletions spec/unit/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,20 @@
end
end

describe "#checksum" do
let(:digest) { RDF::URI.new("urn:sha1:f1d2d2f924e986ac86fdf7b36c94bcdf32beec15") }
before do
allow(subject).to receive(:digest) { [digest] }
end
its(:checksum) { is_expected.to be_a(ActiveFedora::Checksum) }
it "should have the right value" do
expect(subject.checksum.value).to eq("f1d2d2f924e986ac86fdf7b36c94bcdf32beec15")
end
it "should have the right algorithm" do
expect(subject.checksum.algorithm).to eq("SHA1")
end
end

describe "#save" do
let(:file) { described_class.new }
context "when there is nothing to save" do
Expand Down