Skip to content

Commit

Permalink
Merge pull request #7 from ld4l/upgrade_0.8
Browse files Browse the repository at this point in the history
Upgrade to support ActiveTriples 0.5, 0.6, and 0.8.2
  • Loading branch information
elrayle committed Apr 14, 2016
2 parents 080ccb1 + 282d575 commit 27fa153
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 75 deletions.
16 changes: 14 additions & 2 deletions lib/ld4l/ore_rdf/models/aggregation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,24 @@ def id

def title
titles = list_info.title
titles.kind_of?(Array) && titles.size > 0 ? titles[0] : ""
title = ""
if list_info.respond_to? 'persistence_strategy' # >= ActiveTriples 0.8
title = titles.first if titles.kind_of?(ActiveTriples::Relation) && titles.size > 0
else # < ActiveTriples 0.8
title = titles.first if titles.kind_of?(Array) && titles.size > 0
end
title
end

def description
descriptions = list_info.description
descriptions.kind_of?(Array) && descriptions.size > 0 ? descriptions[0] : ""
description = ""
if list_info.respond_to? 'persistence_strategy' # >= ActiveTriples 0.8
description = descriptions.first if descriptions.kind_of?(ActiveTriples::Relation) && descriptions.size > 0
else # < ActiveTriples 0.8
description = descriptions.first if descriptions.kind_of?(Array) && descriptions.size > 0
end
description
end

def aggregation_resource
Expand Down
6 changes: 5 additions & 1 deletion lib/ld4l/ore_rdf/models/proxy_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ def self.get_range( aggregation, start=0, limit=nil )
results = query.execute(graph)
results.each do |r|
proxy_uri = r.to_hash[:proxy]
proxy = LD4L::OreRDF::ProxyResource.new(proxy_uri)
if aggregation.list_info.respond_to? 'persistence_strategy' # >= ActiveTriples 0.8
proxy = LD4L::OreRDF::ProxyResource.new(proxy_uri,aggregation.list_info)
else # < ActiveTriples 0.8
proxy = LD4L::OreRDF::ProxyResource.new(proxy_uri)
end
proxies << proxy
end
proxies
Expand Down
4 changes: 1 addition & 3 deletions lib/ld4l/ore_rdf/services/aggregation/persist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ def self.call( aggregation )

# TODO Probably shouldn't be ArgumentError
raise ArgumentError, "title is required" unless
aggregation.title && aggregation.title && aggregation.title.kind_of?(String) &&
aggregation.title.size > 0
aggregation.title && aggregation.title.kind_of?(String) && aggregation.title.size > 0

count = 0
agg_persisted = aggregation.aggregation_resource.persist!
Expand All @@ -36,4 +35,3 @@ def self.call( aggregation )
end
end
end

6 changes: 5 additions & 1 deletion lib/ld4l/ore_rdf/services/proxy/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ def self.call( options = {} )
LD4L::OreRDF.configuration.localname_minter )

# create the proxy and set properties
proxy = LD4L::OreRDF::ProxyResource.new(id)
if aggregation.list_info.respond_to? 'persistence_strategy' # >= ActiveTriples 0.8
proxy = LD4L::OreRDF::ProxyResource.new(id,aggregation.list_info)
else # < ActiveTriples 0.8
proxy = LD4L::OreRDF::ProxyResource.new(id)
end
proxy.proxy_for = resource
proxy.proxy_in = aggregation.aggregation_resource
proxy.contributor = options[:contributor] || [] # TODO default to aggregation.owner
Expand Down
6 changes: 5 additions & 1 deletion lib/ld4l/ore_rdf/services/proxy/find.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ def self.call( options={} )
uri = h[:proxy]
if resume
# if resume, return Hash of proxy uri => resumed proxy for each found
proxies[uri] = LD4L::OreRDF::ProxyResource.new(uri)
if aggregation.respond_to? 'persistence_strategy' # >= ActiveTriples 0.8
proxies[uri] = LD4L::OreRDF::ProxyResource.new(uri,aggregation.list_info)
else # < ActiveTriples 0.8
proxies[uri] = LD4L::OreRDF::ProxyResource.new(uri)
end
elsif process_properties
# if properties, return Hash of proxy uri => Hash of property => value for each found
properties = h
Expand Down
4 changes: 4 additions & 0 deletions spec/ld4l/ore_rdf/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
describe 'LD4L::OreRDF' do

describe '#configuration' do
after(:all) do
ActiveTriples::RDFSource.type_registry.keys.each { |k| ActiveTriples::RDFSource.type_registry.delete(k) } if Object.const_defined?("ActiveTriples::RDFSource")
end

describe "base_uri" do
context "when base_uri is not configured" do
before do
Expand Down
89 changes: 47 additions & 42 deletions spec/ld4l/ore_rdf/models/aggregation_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
end

it 'should not be settable' do
expect{ subject.set_subject! RDF::URI('http://example.org/moomin2') }.to raise_error
expect{ subject.set_subject! RDF::URI('http://example.org/moomin2') }.to raise_error(RuntimeError, 'Refusing update URI when one is already assigned!')
end
end
end
Expand Down Expand Up @@ -158,23 +158,24 @@
orig_bib3 = "http://example.org/individual/b3"

new_bib1 = "http://example.org/individual/b1_NEW"
new_bib2 = "http://example.org/individual/b2_NEW"
new_bib3 = "http://example.org/individual/b3_NEW"

subject.aggregates = orig_bib1
subject.aggregates << orig_bib2
subject.aggregates << orig_bib3

aggregates = subject.aggregates.dup
if subject.respond_to? 'persistence_strategy' # >= ActiveTriples 0.8
aggregates = subject.aggregates.to_a
else # < ActiveTriples 0.8
aggregates = subject.aggregates.dup
end
aggregates[0] = new_bib1
# aggregates[1] = new_bib2
aggregates[2] = new_bib3
subject.aggregates = aggregates

expect(subject.aggregates[0]).to eq new_bib1
# expect(subject.aggregates[1]).to eq new_bib2
expect(subject.aggregates[1]).to eq orig_bib2
expect(subject.aggregates[2]).to eq new_bib3
expect(subject.aggregates).to include new_bib1
expect(subject.aggregates).to include orig_bib2
expect(subject.aggregates).to include new_bib3
end

it "should be directly changeable for multiple values" do
Expand All @@ -183,21 +184,22 @@
orig_bib3 = "http://example.org/individual/b3"

new_bib1 = "http://example.org/individual/b1_NEW"
new_bib2 = "http://example.org/individual/b2_NEW"
new_bib3 = "http://example.org/individual/b3_NEW"

subject.aggregates = orig_bib1
subject.aggregates << orig_bib2
subject.aggregates << orig_bib3

subject.aggregates[0] = new_bib1
# subject.aggregates[1] = new_bib2
subject.aggregates[2] = new_bib3

expect(subject.aggregates[0]).to eq new_bib1
# expect(subject.aggregates[1]).to eq new_bib2
expect(subject.aggregates[1]).to eq orig_bib2
expect(subject.aggregates[2]).to eq new_bib3
if subject.respond_to? 'persistence_strategy' # >= ActiveTriples 0.8
subject.aggregates.swap(orig_bib1, new_bib1)
subject.aggregates.swap(orig_bib3, new_bib3)
else # < ActiveTriples 0.8
subject.aggregates[0] = new_bib1
subject.aggregates[2] = new_bib3
end
expect(subject.aggregates).to include new_bib1
expect(subject.aggregates).to include orig_bib2
expect(subject.aggregates).to include new_bib3
end
end

Expand Down Expand Up @@ -501,14 +503,18 @@
context "and the item is not a blank node" do

subject {LD4L::OreRDF::AggregationResource.new("123")}
let(:result) { subject.persist! }

before do
# Create inmemory repository
@repo = RDF::Repository.new
allow(subject.class).to receive(:repository).and_return(nil)
allow(subject).to receive(:repository).and_return(@repo)
ActiveTriples::Repositories.repositories[:default] = @repo
subject.title = "bla"
subject.persist!
result
end

it "should return true" do
expect(result).to eq true
end

it "should persist to the repository" do
Expand All @@ -534,7 +540,7 @@
subject << RDF::Statement(RDF::DC.LicenseDocument, RDF::DC.title, 'LICENSE')
end

subject { LD4L::FoafRDF::Person.new('456')}
subject { LD4L::OreRDF::AggregationResource.new('123') }

it 'should return true' do
expect(subject.destroy!).to be true
Expand All @@ -548,22 +554,26 @@

context 'with a parent' do
before do
parent.owner = subject
subject.owner = child
end

let(:parent) do
LD4L::OreRDF::AggregationResource.new('123')
let(:child) do
if subject.respond_to? 'persistence_strategy' # >= ActiveTriples 0.8
LD4L::FoafRDF::Person.new('456',subject)
else # < ActiveTriples 0.8
LD4L::FoafRDF::Person.new('456')
end
end

it 'should empty the graph and remove it from the parent' do
subject.destroy
expect(parent.owner).to be_empty
child.destroy
expect(subject.owner).to be_empty
end

it 'should remove its whole graph from the parent' do
subject.destroy
subject.each_statement do |s|
expect(parent.statements).not_to include s
child.destroy
child.each_statement do |s|
expect(subject.statements).not_to include s
end
end
end
Expand Down Expand Up @@ -667,7 +677,12 @@
end

it "raise an error if the value is not a URI, Node, Literal, RdfResource, or string" do
expect{subject.set_value(RDF::DC.title, Object.new)}.to raise_error
if subject.respond_to? 'persistence_strategy' # >= ActiveTriples 0.8
error_name = ActiveTriples::Relation::ValueError
else # < ActiveTriples 0.8
error_name = RuntimeError
end
expect{subject.set_value(RDF::DC.title, Object.new)}.to raise_error(error_name,/value must be an RDF URI, Node, Literal, or a valid datatype. See RDF::Literal.*/)
end

it "should be able to accept a subject" do
Expand Down Expand Up @@ -697,7 +712,8 @@

describe '#type' do
it 'should return the type configured on the parent class' do
expect(subject.type).to eq [LD4L::OreRDF::AggregationResource.type]
expected_result = LD4L::OreRDF::AggregationResource.type.kind_of?(Array) ? LD4L::OreRDF::AggregationResource.type : [LD4L::OreRDF::AggregationResource.type]
expect(subject.type).to eq expected_result
end

it 'should set the type' do
Expand Down Expand Up @@ -731,17 +747,6 @@
end
end

describe '#solrize' do
it 'should return a label for bnodes' do
expect(subject.solrize).to eq subject.rdf_label
end

it 'should return a string of the resource uri' do
subject.set_subject! 'http://example.org/moomin'
expect(subject.solrize).to eq 'http://example.org/moomin'
end
end

describe 'editing the graph' do
it 'should write properties when statements are added' do
subject << RDF::Statement.new(subject.rdf_subject, RDF::DC.title, 'Comet in Moominland')
Expand Down
51 changes: 26 additions & 25 deletions spec/ld4l/ore_rdf/models/proxy_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
end

it 'should not be settable' do
expect{ subject.set_subject! RDF::URI('http://example.org/moomin2') }.to raise_error
expect{ subject.set_subject! RDF::URI('http://example.org/moomin2') }.to raise_error(RuntimeError, 'Refusing update URI when one is already assigned!')
end
end
end
Expand Down Expand Up @@ -381,17 +381,21 @@
context "and the item is not a blank node" do

subject {LD4L::OreRDF::ProxyResource.new("123")}
let(:result) { subject.persist! }

before do
# Create inmemory repository
@repo = RDF::Repository.new
allow(subject.class).to receive(:repository).and_return(nil)
allow(subject).to receive(:repository).and_return(@repo)
ActiveTriples::Repositories.repositories[:default] = @repo
subject.contributor = "John Smith"
an_aggregation = LD4L::OreRDF::AggregationResource.new('1')
subject.proxy_in = an_aggregation
subject.proxy_for = "http://example.org/b1"
subject.persist!
result
end

it "should return true" do
expect(result).to eq true
end

it "should persist to the repository" do
Expand All @@ -408,7 +412,11 @@
subject.persist!
subject.reload
expect(subject.contributor).to eq []
expect(@repo.statements.to_a.length).to eq 1 # Only the type statement
if subject.respond_to? 'persistence_strategy' # >= ActiveTriples 0.8
expect(@repo.statements.to_a.length).to eq 2 # Only the type statements for aggregation and proxy
else # < ActiveTriples 0.8
expect(@repo.statements.to_a.length).to eq 1 # Only the type statements for proxy
end
end
end

Expand Down Expand Up @@ -453,7 +461,7 @@
subject << RDF::Statement(RDF::DC.LicenseDocument, RDF::DC.title, 'LICENSE')
end

subject { LD4L::FoafRDF::Person.new('456')}
subject { LD4L::OreRDF::ProxyResource.new('123') }

it 'should return true' do
expect(subject.destroy!).to be true
Expand All @@ -467,22 +475,26 @@

context 'with a parent' do
before do
parent.contributor = subject
subject.contributor = child
end

let(:parent) do
LD4L::OreRDF::ProxyResource.new('123')
let(:child) do
if subject.respond_to? 'persistence_strategy' # >= ActiveTriples 0.8
LD4L::FoafRDF::Person.new('456',subject)
else # < ActiveTriples 0.8
LD4L::FoafRDF::Person.new('456')
end
end

it 'should empty the graph and remove it from the parent' do
subject.destroy
expect(parent.contributor).to be_empty
child.destroy
expect(subject.contributor).to be_empty
end

it 'should remove its whole graph from the parent' do
subject.destroy
subject.each_statement do |s|
expect(parent.statements).not_to include s
child.destroy
child.each_statement do |s|
expect(subject.statements).not_to include s
end
end
end
Expand Down Expand Up @@ -591,17 +603,6 @@
end
end

describe '#solrize' do
it 'should return a label for bnodes' do
expect(subject.solrize).to eq subject.rdf_label
end

it 'should return a string of the resource uri' do
subject.set_subject! 'http://example.org/moomin'
expect(subject.solrize).to eq 'http://example.org/moomin'
end
end

describe 'editing the graph' do
it 'should write properties when statements are added' do
subject << RDF::Statement.new(subject.rdf_subject, RDFVocabularies::ORE.proxyFor, 'Comet in Moominland')
Expand Down
4 changes: 4 additions & 0 deletions spec/ld4l/ore_rdf_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require "spec_helper"

describe "LD4L::OreRDF" do
after(:all) do
ActiveTriples::RDFSource.type_registry.keys.each { |k| ActiveTriples::RDFSource.type_registry.delete(k) } if Object.const_defined?("ActiveTriples::RDFSource")
end

describe "#configure" do

before do
Expand Down

0 comments on commit 27fa153

Please sign in to comment.