Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract SolrHit class to wrap Solr response documents
- Loading branch information
Showing
13 changed files
with
209 additions
and
74 deletions.
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
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,71 @@ | ||
module ActiveFedora | ||
class SolrHit < Delegator | ||
def self.for(hit) | ||
return hit if hit.is_a? ActiveFedora::SolrHit | ||
|
||
SolrHit.new(hit) | ||
end | ||
|
||
delegate :models, to: :classifier | ||
|
||
def __getobj__ | ||
@document # return object we are delegating to, required | ||
end | ||
|
||
alias static_config __getobj__ | ||
|
||
def __setobj__(obj) | ||
@document = obj | ||
end | ||
|
||
attr_reader :document | ||
|
||
def initialize(document) | ||
super | ||
@document = document | ||
end | ||
|
||
def id | ||
document[ActiveFedora.id_field] | ||
end | ||
|
||
def rdf_uri | ||
::RDF::URI.new(ActiveFedora::Base.id_to_uri(id)) | ||
end | ||
|
||
def model(opts = {}) | ||
best_model_match = classifier.best_model(opts) | ||
ActiveFedora::Base.logger.warn "Could not find a model for #{id}, defaulting to ActiveFedora::Base" if ActiveFedora::Base.logger && best_model_match == ActiveFedora::Base | ||
best_model_match | ||
end | ||
|
||
def model?(model_to_check) | ||
models.any? do |model| | ||
model_to_check >= model | ||
end | ||
end | ||
|
||
def instantiate_with_json | ||
model.allocate.init_with_json(profile_json) do |allocated_object| | ||
create_key = allocated_object.indexing_service.class.create_time_solr_name | ||
modified_key = allocated_object.indexing_service.class.modified_time_solr_name | ||
allocated_object.resource.set_value(:create_date, DateTime.parse(document[create_key])) if document[create_key] | ||
allocated_object.resource.set_value(:modified_date, DateTime.parse(document[modified_key])) if document[modified_key] | ||
end | ||
end | ||
|
||
def reify(opts = {}) | ||
model(opts).find(id, cast: true) | ||
end | ||
|
||
private | ||
|
||
def classifier | ||
ActiveFedora.model_mapper.classifier(document) | ||
end | ||
|
||
def profile_json | ||
Array(document[ActiveFedora::IndexingService.profile_solr_name]).first | ||
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
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,52 @@ | ||
require 'spec_helper' | ||
|
||
describe ActiveFedora::SolrHit do | ||
before do | ||
class Foo < ActiveFedora::Base | ||
has_metadata 'descMetadata', type: ActiveFedora::SimpleDatastream do |m| | ||
m.field "foo", :text | ||
m.field "bar", :text | ||
end | ||
Deprecation.silence(ActiveFedora::Attributes) do | ||
has_attributes :foo, datastream: 'descMetadata', multiple: true | ||
has_attributes :bar, datastream: 'descMetadata', multiple: false | ||
end | ||
property :title, predicate: ::RDF::Vocab::DC.title, multiple: false | ||
end | ||
end | ||
|
||
let(:another) { Foo.create } | ||
|
||
let!(:obj) { Foo.create!( | ||
id: 'test-123', | ||
foo: ["baz"], | ||
bar: 'quix', | ||
title: 'My Title' | ||
) } | ||
|
||
let(:profile) { { "foo" => ["baz"], "bar" => "quix", "title" => "My Title" }.to_json } | ||
let(:doc) { { 'id' => 'test-123', 'has_model_ssim' => ['Foo'], 'object_profile_ssm' => profile } } | ||
let(:solr_hit) { described_class.new(doc) } | ||
|
||
after do | ||
Object.send(:remove_const, :Foo) | ||
end | ||
|
||
describe "#reify" do | ||
subject { solr_hit.reify } | ||
|
||
it "finds the document in solr" do | ||
expect(subject).to be_instance_of Foo | ||
expect(subject.title).to eq 'My Title' | ||
end | ||
end | ||
|
||
describe "#instantiate_with_json" do | ||
subject { solr_hit.instantiate_with_json } | ||
|
||
it "finds the document in solr" do | ||
expect(subject).to be_instance_of Foo | ||
expect(subject.title).to eq 'My Title' | ||
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
Oops, something went wrong.