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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e5100a6b1c8c88df0960b3777553605ebb2d58e2
4
- data.tar.gz: e5485011985f9d66114103f97e81c43a6b0c34b3
3
+ metadata.gz: 3f4b77f60b44c3d2471ca37daa9608abcd25502b
4
+ data.tar.gz: a76daf16003cd434e172c924db465845dd9b5009
5
5
  SHA512:
6
- metadata.gz: 7571c8035d8b762f9e67f2aee4ad0f93912c9357dafd0a420531df75cc3e74893f0bbc9ef0bf7abc7b3e2e627e92cc39fb58a0c4fca6392c7b8e52985aea856e
7
- data.tar.gz: 727177fc93f5a3925135b2c05ef3e3ed7263d13c24d223636a6bd3bed64e0ae2225af5c734aa933a68cf3c795660820a36be4220cb5a0a3dec118264742b1a2b
6
+ metadata.gz: bd2701dbd4e32689e700a80876a51fb0f93ff907bc73ebe7bcc663b068746b3255422a5fdde3aee845a845fac5e8c7360fe5b207b4f948bda2406d2e7307e850
7
+ data.tar.gz: 778f6876ea0cb79dcdab35d71c93c715786a4b9f95013678a2a5890aae32ca7c060798ec67a849423db30036a5230e074fe6cef8c0eec08cd82adc4701e3a810
data/Gemfile CHANGED
@@ -6,4 +6,6 @@ gemspec
6
6
  group :test do
7
7
  gem "rspec", "~> 2.99.0"
8
8
  gem "webmock"
9
+ gem 'pry', '~> 0.9.12.6'
10
+ gem 'binding_of_caller', '~> 0.7.2'
9
11
  end
@@ -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)
@@ -1,3 +1,3 @@
1
1
  module Tripod
2
- VERSION = "0.9.9"
2
+ VERSION = "0.9.10"
3
3
  end
@@ -2,6 +2,11 @@ class Resource
2
2
 
3
3
  include Tripod::Resource
4
4
 
5
- field :label, RDF::RDFS.label
5
+ field :pref_label, RDF::SKOS.prefLabel
6
+ field :label, RDF::RDFS.label
7
+ field :title, RDF::DC.title
8
+
9
+ end
10
+
11
+
6
12
 
7
- end
data/spec/spec_helper.rb CHANGED
@@ -7,6 +7,8 @@ $LOAD_PATH.unshift(MODELS)
7
7
  require 'tripod'
8
8
  require 'rspec'
9
9
  require 'webmock/rspec'
10
+ require 'binding_of_caller'
11
+ require 'pry'
10
12
 
11
13
  RSpec.configure do |config|
12
14
  config.mock_with :rspec
@@ -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.to_a.length.should == 2
33
-
34
- triples.to_a.sort{|a,b| a.to_s <=> b.to_s }.first.predicate.should == RDF::URI('http://example.com/name-other-pred')
35
- triples.to_a.sort{|a,b| a.to_s <=> b.to_s }.first.object.to_s.should == "hello"
36
-
37
- triples.to_a.sort{|a,b| a.to_s <=> b.to_s }.last.predicate.should == RDF::RDFS.label
38
- triples.to_a.sort{|a,b| a.to_s <=> b.to_s }.last.object.to_s.should == "Name"
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.to_a.length.should == 1
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
- before do
61
- @john.eager_load_object_triples!
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
- it "should add triples to the repository for the all the objects' predicates" do
65
- triples = @john.repository.query([ @peter.uri, :predicate, :object] )
66
- triples.to_a.length.should == 3
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.to_a.sort{|a,b| a.to_s <=> b.to_s }[0].predicate.should == RDF::URI('http://example.com/age')
69
- triples.to_a.sort{|a,b| a.to_s <=> b.to_s }[0].object.to_s.should == "30"
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
- triples.to_a.sort{|a,b| a.to_s <=> b.to_s }[1].predicate.should == RDF::URI('http://example.com/name')
72
- triples.to_a.sort{|a,b| a.to_s <=> b.to_s }[1].object.to_s.should == "Peter"
115
+ end
73
116
 
74
- triples.to_a.sort{|a,b| a.to_s <=> b.to_s }[2].predicate.should == RDF.type
75
- triples.to_a.sort{|a,b| a.to_s <=> b.to_s }[2].object.to_s.should == RDF::URI('http://example.com/person')
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.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-06-27 00:00:00.000000000 Z
13
+ date: 2014-08-21 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rest-client