tripod 0.11.2 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/tripod/criteria.rb +18 -5
- data/lib/tripod/criteria/execution.rb +28 -10
- data/lib/tripod/version.rb +1 -1
- data/spec/tripod/criteria_execution_spec.rb +16 -0
- data/spec/tripod/criteria_spec.rb +16 -0
- 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: 860ab59bda222badcfcec33c7581cb38c9689438
|
4
|
+
data.tar.gz: f3b261284e25ca436da10d9645069566ee5502ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 76536a9ac3b635dcc105d200b46ed62ff18e62aef98448ff2f025dd032348d4a5775fba2106a5b612777f1671de4bc804e658fde6a7ab48c17d894fa4c7dc582
|
7
|
+
data.tar.gz: bc4b45d80af1433da52118bf8c0fc9ebbcf62f2f194c36073983b83ef34a775b4d96806c304bc805f81dc80721b85dfc6400778d1a4af9157a3cec2420de1e42
|
data/lib/tripod/criteria.rb
CHANGED
@@ -18,11 +18,13 @@ module Tripod
|
|
18
18
|
attr_accessor :order_clause
|
19
19
|
attr_accessor :offset_clause
|
20
20
|
attr_accessor :graph_uri
|
21
|
+
attr_accessor :graph_lambdas
|
21
22
|
|
22
23
|
def initialize(resource_class)
|
23
24
|
self.resource_class = resource_class
|
24
25
|
self.where_clauses = []
|
25
26
|
self.extra_clauses = []
|
27
|
+
self.graph_lambdas = []
|
26
28
|
|
27
29
|
if resource_class._RDF_TYPE
|
28
30
|
self.where("?uri a <#{resource_class._RDF_TYPE.to_s}>")
|
@@ -84,17 +86,28 @@ module Tripod
|
|
84
86
|
self
|
85
87
|
end
|
86
88
|
|
87
|
-
# Restrict
|
89
|
+
# Restrict this query to the graph uri passed in
|
90
|
+
# You may also pass a block to an unbound graph, ?g
|
91
|
+
# then chain a where clause to the criteria returned to bind ?g
|
88
92
|
#
|
89
93
|
# @example .graph(RDF::URI.new('http://graphoid')
|
90
94
|
# @example .graph('http://graphoid')
|
95
|
+
# @example .graph(nil) { "?s ?p ?o" }.where("?uri ?p ?g")
|
91
96
|
#
|
92
|
-
# @param [
|
97
|
+
# @param [ String, RDF::URI ] The graph uri
|
98
|
+
# @param [ Block ] A string to be executed within an unbound graph, ?g
|
93
99
|
#
|
94
100
|
# @return [ Tripod::Criteria ] A criteria object
|
95
|
-
def graph(graph_uri)
|
96
|
-
|
97
|
-
|
101
|
+
def graph(graph_uri, &block)
|
102
|
+
|
103
|
+
if block_given?
|
104
|
+
self.graph_lambdas ||= []
|
105
|
+
self.graph_lambdas << block
|
106
|
+
self
|
107
|
+
else
|
108
|
+
self.graph_uri = graph_uri.to_s
|
109
|
+
self
|
110
|
+
end
|
98
111
|
end
|
99
112
|
end
|
100
113
|
end
|
@@ -62,22 +62,40 @@ module Tripod
|
|
62
62
|
|
63
63
|
select_query = "SELECT DISTINCT ?uri "
|
64
64
|
|
65
|
-
if
|
66
|
-
|
67
|
-
if
|
68
|
-
|
65
|
+
if graph_lambdas.empty?
|
66
|
+
|
67
|
+
if return_graph
|
68
|
+
# if we are returning the graph, select it as a variable, and include either the <graph> or ?graph in the where clause
|
69
|
+
if graph_uri
|
70
|
+
select_query += "(<#{graph_uri}> as ?graph) WHERE { GRAPH <#{graph_uri}> { "
|
71
|
+
else
|
72
|
+
select_query += "?graph WHERE { GRAPH ?graph { "
|
73
|
+
end
|
69
74
|
else
|
70
|
-
select_query += "
|
75
|
+
select_query += "WHERE { "
|
76
|
+
# if we're not returning the graph, only restrict by the <graph> if there's one set at class level
|
77
|
+
select_query += "GRAPH <#{graph_uri}> { " if graph_uri
|
71
78
|
end
|
79
|
+
|
80
|
+
select_query += self.query_where_clauses.join(" . ")
|
81
|
+
select_query += " } "
|
82
|
+
select_query += "} " if return_graph || graph_uri # close the graph clause
|
83
|
+
|
72
84
|
else
|
85
|
+
# whip through the graph lambdas and add into the query
|
86
|
+
# we have multiple graphs so the above does not apply
|
73
87
|
select_query += "WHERE { "
|
74
|
-
|
75
|
-
|
88
|
+
|
89
|
+
graph_lambdas.each do |lambda_item|
|
90
|
+
select_query += "GRAPH ?g { "
|
91
|
+
select_query += lambda_item.call
|
92
|
+
select_query += " } "
|
93
|
+
end
|
94
|
+
|
95
|
+
select_query += self.query_where_clauses.join(" . ")
|
96
|
+
select_query += " } "
|
76
97
|
end
|
77
98
|
|
78
|
-
select_query += self.query_where_clauses.join(" . ")
|
79
|
-
select_query += " } "
|
80
|
-
select_query += "} " if return_graph || graph_uri # close the graph clause
|
81
99
|
select_query += self.extra_clauses.join(" ")
|
82
100
|
|
83
101
|
select_query += [order_clause, limit_clause, offset_clause].join(" ")
|
data/lib/tripod/version.rb
CHANGED
@@ -26,6 +26,22 @@ describe Tripod::Criteria do
|
|
26
26
|
|
27
27
|
describe "#as_query" do
|
28
28
|
|
29
|
+
context "when graph_lambdas exist" do
|
30
|
+
it "should return the contents of the block inside a graph statement with unbound ?g parameter" do
|
31
|
+
resource_criteria.graph(nil) do
|
32
|
+
"?uri ?p ?o"
|
33
|
+
end
|
34
|
+
resource_criteria.as_query.should == "SELECT DISTINCT ?uri WHERE { GRAPH ?g { ?uri ?p ?o } ?uri ?p ?o }"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should be possible to bind to the ?g paramter on the criteria after supplying a block" do
|
38
|
+
resource_criteria.graph(nil) do
|
39
|
+
"?s ?p ?o"
|
40
|
+
end.where("?uri ?p ?g")
|
41
|
+
resource_criteria.as_query.should == "SELECT DISTINCT ?uri WHERE { GRAPH ?g { ?s ?p ?o } ?uri ?p ?g }"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
29
45
|
context "for a class with an rdf_type and graph" do
|
30
46
|
it "should return a SELECT query based with an rdf type restriction" do
|
31
47
|
person_criteria.as_query.should == "SELECT DISTINCT ?uri (<http://example.com/graph> as ?graph) WHERE { GRAPH <http://example.com/graph> { ?uri a <http://example.com/person> } }"
|
@@ -16,6 +16,10 @@ describe Tripod::Criteria do
|
|
16
16
|
person_criteria.extra_clauses.should == []
|
17
17
|
end
|
18
18
|
|
19
|
+
it "should initialize the graph lambdas to a blank array" do
|
20
|
+
person_criteria.graph_lambdas.should == []
|
21
|
+
end
|
22
|
+
|
19
23
|
context "with rdf_type set on the class" do
|
20
24
|
it "should initialize the where clauses to include a type restriction" do
|
21
25
|
person_criteria.where_clauses.should == ["?uri a <http://example.com/person>"]
|
@@ -133,6 +137,18 @@ describe Tripod::Criteria do
|
|
133
137
|
resource_criteria.graph(RDF::URI("http://example.com/foobar"))
|
134
138
|
resource_criteria.graph_uri.should == "http://example.com/foobar"
|
135
139
|
end
|
140
|
+
|
141
|
+
context "with a block" do
|
142
|
+
|
143
|
+
it "will convert each criteria in the block to a query" do
|
144
|
+
resource_criteria.graph(nil) do
|
145
|
+
"?uri ?p ?o"
|
146
|
+
end.where("?uri ?p ?g")
|
147
|
+
|
148
|
+
resource_criteria.graph_lambdas.should_not be_empty
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
136
152
|
end
|
137
153
|
|
138
154
|
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.
|
4
|
+
version: 0.12.0
|
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:
|
13
|
+
date: 2016-03-04 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rest-client
|