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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e8b2231c1e29b1c0505ed07246fed4759caec36
4
- data.tar.gz: 7f11adc9aeb2eb8da2cc0bdc9cdc7e5c6aed0375
3
+ metadata.gz: 860ab59bda222badcfcec33c7581cb38c9689438
4
+ data.tar.gz: f3b261284e25ca436da10d9645069566ee5502ef
5
5
  SHA512:
6
- metadata.gz: 631ba8493a054ed194a00582d6da8bb834d5e30493a82f3097ff5f0c6219637d7dec3049ec6fad916c280e2b9cd2327caed7500ee4d87c2c987c2f223207f4ae
7
- data.tar.gz: 8504919b265ce0582f2d0d1a60795214bd119867a02ec32892d7be22d40a48ebe6228d5ab2e38c1f582a1dcd561658757236b3192e8ba3791a2d1f7c91c8cdcf
6
+ metadata.gz: 76536a9ac3b635dcc105d200b46ed62ff18e62aef98448ff2f025dd032348d4a5775fba2106a5b612777f1671de4bc804e658fde6a7ab48c17d894fa4c7dc582
7
+ data.tar.gz: bc4b45d80af1433da52118bf8c0fc9ebbcf62f2f194c36073983b83ef34a775b4d96806c304bc805f81dc80721b85dfc6400778d1a4af9157a3cec2420de1e42
@@ -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 htis query to the graph uri passed in
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 [ Stirng, RDF::URI ] The graph uri
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
- self.graph_uri = graph_uri.to_s
97
- self
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 return_graph
66
- # if we are returing the graph, select it as a variable, and include either the <graph> or ?graph in the where clause
67
- if graph_uri
68
- select_query += "(<#{graph_uri}> as ?graph) WHERE { GRAPH <#{graph_uri}> { "
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 += "?graph WHERE { GRAPH ?graph { "
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
- # if we're not returning the graph, only restrict by the <graph> if there's one set at class level
75
- select_query += "GRAPH <#{graph_uri}> { " if graph_uri
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(" ")
@@ -1,3 +1,3 @@
1
1
  module Tripod
2
- VERSION = "0.11.2"
2
+ VERSION = "0.12.0"
3
3
  end
@@ -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.11.2
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: 2015-11-17 00:00:00.000000000 Z
13
+ date: 2016-03-04 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rest-client