tripod 0.9.9 → 0.9.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/lib/tripod/eager_loading.rb +46 -0
- data/lib/tripod/version.rb +1 -1
- data/spec/app/models/resource.rb +7 -2
- data/spec/spec_helper.rb +2 -0
- data/spec/tripod/eager_loading_spec.rb +75 -21
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f4b77f60b44c3d2471ca37daa9608abcd25502b
|
4
|
+
data.tar.gz: a76daf16003cd434e172c924db465845dd9b5009
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd2701dbd4e32689e700a80876a51fb0f93ff907bc73ebe7bcc663b068746b3255422a5fdde3aee845a845fac5e8c7360fe5b207b4f948bda2406d2e7307e850
|
7
|
+
data.tar.gz: 778f6876ea0cb79dcdab35d71c93c715786a4b9f95013678a2a5890aae32ca7c060798ec67a849423db30036a5230e074fe6cef8c0eec08cd82adc4701e3a810
|
data/Gemfile
CHANGED
data/lib/tripod/eager_loading.rb
CHANGED
@@ -11,10 +11,13 @@ module Tripod::EagerLoading
|
|
11
11
|
# get all the triples in the db where the predicate uri is their subject
|
12
12
|
# stick the results in this resource's repo
|
13
13
|
# options: labels_only (default false)
|
14
|
+
# options: predicates (default nil) array of predicaets (as URIs or strings representing URIs) for fieldnames to fetch
|
14
15
|
def eager_load_predicate_triples!(opts={})
|
15
16
|
|
16
17
|
if opts[:labels_only]
|
17
18
|
construct_query = "CONSTRUCT { ?p <#{RDF::RDFS.label}> ?pred_label } WHERE { <#{self.uri.to_s}> ?p ?o . ?p <#{RDF::RDFS.label}> ?pred_label }"
|
19
|
+
elsif (opts[:predicates] && opts[:predicates].length > 0)
|
20
|
+
construct_query = build_multifield_query(opts[:predicates],"?p")
|
18
21
|
else
|
19
22
|
construct_query = "CONSTRUCT { ?p ?pred_pred ?pred_label } WHERE { <#{self.uri.to_s}> ?p ?o . ?p ?pred_pred ?pred_label }"
|
20
23
|
end
|
@@ -26,11 +29,14 @@ module Tripod::EagerLoading
|
|
26
29
|
# get all the triples in the db where the object uri is their subject
|
27
30
|
# stick the results in this resource's repo
|
28
31
|
# options: labels_only (default false)
|
32
|
+
# options: predicates (default nil) array of predicaets (as URIs or strings representing URIs) for fieldnames to fetch
|
29
33
|
def eager_load_object_triples!(opts={})
|
30
34
|
object_uris = []
|
31
35
|
|
32
36
|
if opts[:labels_only]
|
33
37
|
construct_query = "CONSTRUCT { ?o <#{RDF::RDFS.label}> ?obj_label } WHERE { <#{self.uri.to_s}> ?p ?o . ?o <#{RDF::RDFS.label}> ?obj_label }"
|
38
|
+
elsif (opts[:predicates] && opts[:predicates].length > 0)
|
39
|
+
construct_query = build_multifield_query(opts[:predicates],"?o")
|
34
40
|
else
|
35
41
|
construct_query = "CONSTRUCT { ?o ?obj_pred ?obj_label } WHERE { <#{self.uri.to_s}> ?p ?o . ?o ?obj_pred ?obj_label }"
|
36
42
|
end
|
@@ -39,6 +45,46 @@ module Tripod::EagerLoading
|
|
39
45
|
self.class.add_data_to_repository(extra_triples, self.repository)
|
40
46
|
end
|
41
47
|
|
48
|
+
# build a list of optional predicates
|
49
|
+
#
|
50
|
+
# for the input
|
51
|
+
# build_multifield_query(RDF::SKOS.prefLabel, RDF::RDFS.label, RDF::DC.title],"?p")
|
52
|
+
#
|
53
|
+
# the follwing query is generated
|
54
|
+
#
|
55
|
+
# CONSTRUCT {
|
56
|
+
# ?p <http://www.w3.org/2004/02/skos/core#prefLabel> ?pref_label .
|
57
|
+
# ?p <http://www.w3.org/2000/01/rdf-schema#label> ?label .
|
58
|
+
# ?p <http://purl.org/dc/elements/1.1/title> ?title . }
|
59
|
+
# WHERE {
|
60
|
+
# <http://data.hampshirehub.net/data/miscelleanous/2011-census-ks101ew-usual-resident-population-key-statistics-ks> ?p ?o .
|
61
|
+
# OPTIONAL { ?p <http://www.w3.org/2004/02/skos/core#prefLabel> ?pref_label . }
|
62
|
+
# OPTIONAL { ?p <http://www.w3.org/2000/01/rdf-schema#label> ?label . }
|
63
|
+
# OPTIONAL { ?p <http://purl.org/dc/elements/1.1/title> ?title . }
|
64
|
+
# }
|
65
|
+
#
|
66
|
+
def build_multifield_query(predicates,subject_position_as)
|
67
|
+
clauses = []
|
68
|
+
|
69
|
+
iter = 0
|
70
|
+
predicates.each do |p|
|
71
|
+
variable_name = "var#{iter.to_s}"
|
72
|
+
clauses << "#{subject_position_as} <#{p.to_s}> ?#{variable_name} . "
|
73
|
+
iter +=1
|
74
|
+
end
|
75
|
+
|
76
|
+
construct_query = "CONSTRUCT { "
|
77
|
+
clauses.each do |c|
|
78
|
+
construct_query += c
|
79
|
+
end
|
80
|
+
|
81
|
+
construct_query += " } WHERE { <#{self.uri.to_s}> ?p ?o . "
|
82
|
+
clauses.each do |c|
|
83
|
+
construct_query += " OPTIONAL { #{c} } "
|
84
|
+
end
|
85
|
+
construct_query += " }" # close WHERE
|
86
|
+
end
|
87
|
+
|
42
88
|
# get the resource that represents a particular uri. If there's triples in our repo where that uri
|
43
89
|
# is the subject, use that to hydrate a resource, otherwise justdo a find against the db.
|
44
90
|
def get_related_resource(resource_uri, class_of_resource_to_create)
|
data/lib/tripod/version.rb
CHANGED
data/spec/app/models/resource.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -5,7 +5,9 @@ describe Tripod::EagerLoading do
|
|
5
5
|
before do
|
6
6
|
|
7
7
|
@name = Resource.new('http://example.com/name', 'http://example.com/names')
|
8
|
+
@name.pref_label = "Nom"
|
8
9
|
@name.label = "Name"
|
10
|
+
@name.title = "Soubriquet"
|
9
11
|
@name.write_predicate('http://example.com/name-other-pred', 'hello') # another predicate
|
10
12
|
@name.save!
|
11
13
|
|
@@ -18,6 +20,7 @@ describe Tripod::EagerLoading do
|
|
18
20
|
@john.name = "john"
|
19
21
|
@john.knows = @peter.uri
|
20
22
|
@john.save!
|
23
|
+
|
21
24
|
end
|
22
25
|
|
23
26
|
describe "#eager_load_predicate_triples!" do
|
@@ -28,14 +31,16 @@ describe Tripod::EagerLoading do
|
|
28
31
|
end
|
29
32
|
|
30
33
|
it "should add triples to the repository all for the predicates' predicates" do
|
31
|
-
triples = @peter.repository.query([ RDF::URI.new('http://example.com/name'), :predicate, :object] )
|
32
|
-
triples.
|
33
|
-
|
34
|
-
triples.
|
35
|
-
triples.
|
36
|
-
|
37
|
-
triples.
|
38
|
-
triples.
|
34
|
+
triples = @peter.repository.query([ RDF::URI.new('http://example.com/name'), :predicate, :object] ).to_a.sort{|a,b| a.to_s <=> b.to_s }
|
35
|
+
triples.length.should == 4
|
36
|
+
triples[0].predicate.should == RDF::URI('http://example.com/name-other-pred')
|
37
|
+
triples[0].object.to_s.should == "hello"
|
38
|
+
triples[1].predicate.should == RDF::DC.title
|
39
|
+
triples[1].object.to_s.should == "Soubriquet"
|
40
|
+
triples[2].predicate.should == RDF::RDFS.label
|
41
|
+
triples[2].object.to_s.should == "Name"
|
42
|
+
triples[3].predicate.should == RDF::SKOS.prefLabel
|
43
|
+
triples[3].object.to_s.should == "Nom"
|
39
44
|
end
|
40
45
|
end
|
41
46
|
|
@@ -45,35 +50,84 @@ describe Tripod::EagerLoading do
|
|
45
50
|
end
|
46
51
|
|
47
52
|
it "should add triples to the repository all for the predicates labels only" do
|
48
|
-
triples = @peter.repository.query([ RDF::URI.new('http://example.com/name'), :predicate, :object] )
|
49
|
-
triples.
|
53
|
+
triples = @peter.repository.query([ RDF::URI.new('http://example.com/name'), :predicate, :object] ).to_a
|
54
|
+
triples.length.should == 1
|
50
55
|
triples.first.predicate.should == RDF::RDFS.label
|
51
56
|
triples.first.object.to_s.should == "Name"
|
52
57
|
end
|
53
58
|
|
54
59
|
end
|
55
60
|
|
61
|
+
context "with array of fields" do
|
62
|
+
before do
|
63
|
+
@peter.eager_load_predicate_triples!(:predicates => [RDF::SKOS.prefLabel, RDF::DC.title])
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should add triples to the repository all for the given fields of the predicate" do
|
67
|
+
triples = @peter.repository.query([ RDF::URI.new('http://example.com/name'), :predicate, :object] ).to_a.sort{|a,b| a.to_s <=> b.to_s }
|
68
|
+
triples.length.should == 2
|
69
|
+
|
70
|
+
triples.first.predicate.should == RDF::DC.title
|
71
|
+
triples.first.object.to_s.should == "Soubriquet"
|
72
|
+
|
73
|
+
triples.last.predicate.should == RDF::SKOS.prefLabel
|
74
|
+
triples.last.object.to_s.should == "Nom"
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
56
80
|
end
|
57
81
|
|
58
82
|
describe "#eager_load_object_triples!" do
|
59
83
|
|
60
|
-
|
61
|
-
|
84
|
+
context "with no options passed" do
|
85
|
+
before do
|
86
|
+
@john.eager_load_object_triples!
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should add triples to the repository for the all the objects' predicates" do
|
90
|
+
triples = @john.repository.query([ @peter.uri, :predicate, :object] )
|
91
|
+
triples.to_a.length.should == 3
|
92
|
+
|
93
|
+
triples.to_a.sort{|a,b| a.to_s <=> b.to_s }[0].predicate.should == RDF::URI('http://example.com/age')
|
94
|
+
triples.to_a.sort{|a,b| a.to_s <=> b.to_s }[0].object.to_s.should == "30"
|
95
|
+
|
96
|
+
triples.to_a.sort{|a,b| a.to_s <=> b.to_s }[1].predicate.should == RDF::URI('http://example.com/name')
|
97
|
+
triples.to_a.sort{|a,b| a.to_s <=> b.to_s }[1].object.to_s.should == "Peter"
|
98
|
+
|
99
|
+
triples.to_a.sort{|a,b| a.to_s <=> b.to_s }[2].predicate.should == RDF.type
|
100
|
+
triples.to_a.sort{|a,b| a.to_s <=> b.to_s }[2].object.to_s.should == RDF::URI('http://example.com/person')
|
101
|
+
|
102
|
+
end
|
62
103
|
end
|
63
104
|
|
64
|
-
|
65
|
-
|
66
|
-
|
105
|
+
context "with labels_only option" do
|
106
|
+
before do
|
107
|
+
@john.eager_load_object_triples!(:labels_only => true)
|
108
|
+
end
|
67
109
|
|
68
|
-
triples
|
69
|
-
|
110
|
+
it "should add triples to the repository all for the object labels only" do
|
111
|
+
triples = @john.repository.query([ @peter.uri, :predicate, :object] ).to_a
|
112
|
+
triples.length.should == 0 # people don't have labels
|
113
|
+
end
|
70
114
|
|
71
|
-
|
72
|
-
triples.to_a.sort{|a,b| a.to_s <=> b.to_s }[1].object.to_s.should == "Peter"
|
115
|
+
end
|
73
116
|
|
74
|
-
|
75
|
-
|
117
|
+
context "with array of fields" do
|
118
|
+
before do
|
119
|
+
@john.eager_load_object_triples!(:predicates => ['http://example.com/name'])
|
120
|
+
end
|
76
121
|
|
122
|
+
it "should add triples to the repository all for the given fields of the object" do
|
123
|
+
triples = @john.repository.query([ @peter.uri, :predicate, :object] ).to_a.sort{|a,b| a.to_s <=> b.to_s }
|
124
|
+
|
125
|
+
triples.length.should == 1
|
126
|
+
|
127
|
+
triples.first.predicate.should == 'http://example.com/name'
|
128
|
+
triples.first.object.to_s.should == "Peter"
|
129
|
+
|
130
|
+
end
|
77
131
|
end
|
78
132
|
|
79
133
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tripod
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ric Roberts
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2014-
|
13
|
+
date: 2014-08-21 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rest-client
|