-
Notifications
You must be signed in to change notification settings - Fork 63
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
Extracting object from .load_instance_from_solr #51
Merged
jcoyne
merged 1 commit into
samvera:master
from
jeremyf:feature-extract-solr-instance-loading
Mar 26, 2013
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
module ActiveFedora | ||
class FedoraSolrMismatchError < ActiveFedora::ObjectNotFoundError | ||
def initialize(pid, solr_document_id) | ||
super("Solr record id and pid do not match; Solr ID=#{solr_document_id}, PID=#{pid}") | ||
end | ||
end | ||
|
||
# Responsible for loading an ActiveFedora::Base proxy from a Solr document. | ||
class SolrInstanceLoader | ||
attr_reader :context, :pid | ||
private :context, :pid | ||
def initialize(context, pid, solr_doc = nil) | ||
@context = context | ||
@pid = pid | ||
@solr_doc = solr_doc | ||
validate_solr_doc_and_pid!(@solr_doc) | ||
end | ||
|
||
def object | ||
return @object if @object | ||
@object = allocate_object | ||
@object.rels_ext | ||
load_object_datastreams(@object) | ||
@object.inner_object.freeze | ||
@object | ||
end | ||
|
||
private | ||
|
||
def allocate_object | ||
active_fedora_class.allocate.init_with(solr_digital_object) | ||
end | ||
|
||
def solr_digital_object | ||
SolrDigitalObject.new(solr_doc, profile_hash, active_fedora_class) | ||
end | ||
|
||
|
||
def load_object_datastreams(obj) | ||
obj.datastreams.each_value do |ds| | ||
if ds.respond_to?(:profile_from_hash) and (ds_prof = profile_hash['datastreams'][ds.dsid]) | ||
ds.profile_from_hash(ds_prof) | ||
end | ||
ds.from_solr(solr_doc) if ds.respond_to?(:from_solr) | ||
end | ||
end | ||
|
||
def solr_doc | ||
@solr_doc ||= begin | ||
result = context.find_with_conditions(:id=>pid) | ||
if result.empty? | ||
raise ActiveFedora::ObjectNotFoundError, "Object #{pid} not found in solr" | ||
end | ||
@solr_doc = result.first | ||
validate_solr_doc_and_pid!(@solr_doc) | ||
@solr_doc | ||
end | ||
end | ||
|
||
def validate_solr_doc_and_pid!(document) | ||
return true if document.nil? | ||
solr_id = document[SOLR_DOCUMENT_ID] | ||
if pid != solr_id | ||
raise ActiveFedora::FedoraSolrMismatchError.new(pid, solr_id) | ||
end | ||
end | ||
|
||
def active_fedora_class | ||
@active_fedora_class ||= begin | ||
if solr_doc[ActiveFedora::SolrService.solr_name('has_model', :symbol)] | ||
ActiveFedora::SolrService.class_from_solr_document(solr_doc) | ||
else | ||
ActiveFedora::Base | ||
end | ||
end | ||
end | ||
|
||
def profile_json | ||
@profile_json ||= begin | ||
profile_json = Array(solr_doc[ActiveFedora::Base.profile_solr_name]).first | ||
unless profile_json.present? | ||
raise ActiveFedora::ObjectNotFoundError, "Object #{pid} does not contain a solrized profile" | ||
end | ||
profile_json | ||
end | ||
end | ||
|
||
def profile_hash | ||
@profile_hash ||= ActiveSupport::JSON.decode(profile_json) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'spec_helper' | ||
|
||
require 'active_fedora' | ||
|
||
describe ActiveFedora::SolrInstanceLoader do | ||
let(:context) { ActiveFedora::Base } | ||
let(:pid) { nil } | ||
let(:solr_doc) { nil } | ||
let(:active_fedora_object) { ActiveFedora::Base.find(pid, :cast => true) } | ||
subject { ActiveFedora::SolrInstanceLoader.new(context, pid, solr_doc) } | ||
|
||
describe 'existing pid' do | ||
let(:pid) { 'hydrangea:fixture_mods_article1' } | ||
describe 'without a solr document' do | ||
it 'it finds the SOLR document and casts into an AF::Base object' do | ||
expect(subject.object).to eq(active_fedora_object) | ||
end | ||
end | ||
describe 'with matching solr document' do | ||
let(:solr_doc) { ActiveFedora::Base.find_with_conditions(:id=>pid).first } | ||
it 'it casts the SOLR document and casts into an AF::Base object' do | ||
expect(subject.object).to eq(active_fedora_object) | ||
end | ||
end | ||
describe 'with a mismatching solr document' do | ||
let(:mismatching_pid) { 'hydrangea:fixture_mods_article2' } | ||
let(:solr_doc) { ActiveFedora::Base.find_with_conditions(:id=>mismatching_pid).first } | ||
it 'it raise ObjectNotFoundError' do | ||
expect { | ||
subject | ||
}.to raise_error(ActiveFedora::ObjectNotFoundError) | ||
end | ||
end | ||
end | ||
describe 'missing pid' do | ||
let(:pid) { 'hydrangea:fixture_mods_article8675309' } | ||
describe 'without a solr document' do | ||
it 'it raise ObjectNotFoundError' do | ||
expect { | ||
subject.object | ||
}.to raise_error(ActiveFedora::ObjectNotFoundError) | ||
end | ||
end | ||
describe 'with matching solr document' do | ||
let(:solr_doc) { ActiveFedora::Base.find_with_conditions(:id=>pid).first } | ||
it 'it raise ObjectNotFoundError' do | ||
expect { | ||
subject.object | ||
}.to raise_error(ActiveFedora::ObjectNotFoundError) | ||
end | ||
end | ||
describe 'with a mismatching solr document' do | ||
let(:mismatching_pid) { 'hydrangea:fixture_mods_article2' } | ||
let(:solr_doc) { ActiveFedora::Base.find_with_conditions(:id=>mismatching_pid).first } | ||
it 'it raise ObjectNotFoundError' do | ||
expect { | ||
subject | ||
}.to raise_error(ActiveFedora::ObjectNotFoundError) | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future, don't commit debugger to git. It's not necessary that everyone have this