tripod 0.3.4 → 0.3.5

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.
data/lib/tripod.rb CHANGED
@@ -68,6 +68,7 @@ end
68
68
  require "tripod/extensions"
69
69
  require "tripod/sparql_client"
70
70
  require "tripod/sparql_query"
71
+ require "tripod/resource_collection"
71
72
 
72
73
  require "tripod/predicates"
73
74
  require "tripod/attributes"
@@ -6,9 +6,10 @@ module Tripod
6
6
 
7
7
  extend ActiveSupport::Concern
8
8
 
9
- # Execute the query and return an array of all hydrated resources
9
+ # Execute the query and return a +ResourceCollection+ of all hydrated resources
10
+ # +ResourceCollection+ is an +Enumerable+, Array-like object.
10
11
  def resources
11
- resources_from_sparql(build_select_query)
12
+ Tripod::ResourceCollection.new(resources_from_sparql(build_select_query))
12
13
  end
13
14
 
14
15
  # Execute the query and return the first result as a hydrated resource
@@ -39,11 +40,10 @@ module Tripod
39
40
 
40
41
  def build_select_query
41
42
 
42
-
43
43
  if graph_uri
44
- select_query = "SELECT ?uri (<#{graph_uri}> as ?graph) WHERE { GRAPH <#{graph_uri}> "
44
+ select_query = "SELECT DISTINCT ?uri (<#{graph_uri}> as ?graph) WHERE { GRAPH <#{graph_uri}> "
45
45
  else
46
- select_query = "SELECT ?uri ?graph WHERE { GRAPH ?graph "
46
+ select_query = "SELECT DISTINCT ?uri ?graph WHERE { GRAPH ?graph "
47
47
  end
48
48
 
49
49
  select_query += "{ "
@@ -0,0 +1,68 @@
1
+ # encoding: utf-8
2
+
3
+ module Tripod
4
+
5
+ # class that wraps a collection of resources, and allows them to be serialized
6
+ class ResourceCollection
7
+
8
+ include Enumerable
9
+
10
+ attr_reader :resources
11
+
12
+ def initialize(resources)
13
+ @resources = resources
14
+ end
15
+
16
+ def length
17
+ self.resources.length
18
+ end
19
+
20
+ def each
21
+ self.resources.each { |e| yield(e) }
22
+ end
23
+
24
+ # return the underlying array
25
+ def to_a
26
+ resources
27
+ end
28
+
29
+ # allow index operator to act on underlying array of resources.
30
+ def [](*args)
31
+ resources[*args]
32
+ end
33
+
34
+ # for n-triples we can just concatenate them
35
+ def to_nt
36
+ nt = ""
37
+ resources.each do |resource|
38
+ nt += resource.to_nt
39
+ end
40
+ nt
41
+ end
42
+
43
+ def to_json(opts={})
44
+ get_graph.dump(:jsonld)
45
+ end
46
+
47
+ def to_rdf
48
+ get_graph.dump(:rdf)
49
+ end
50
+
51
+ def to_ttl
52
+ get_graph.dump(:n3)
53
+ end
54
+
55
+ private
56
+
57
+ def get_graph
58
+ graph = RDF::Graph.new
59
+ RDF::Reader.for(:ntriples).new(self.to_nt) do |reader|
60
+ reader.each_statement do |statement|
61
+ graph << statement
62
+ end
63
+ end
64
+ end
65
+
66
+ end
67
+
68
+ end
@@ -1,3 +1,3 @@
1
1
  module Tripod
2
- VERSION = "0.3.4"
2
+ VERSION = "0.3.5"
3
3
  end
@@ -28,14 +28,14 @@ describe Tripod::Criteria do
28
28
 
29
29
  context "for a class with an rdf_type and graph" do
30
30
  it "should return a SELECT query based with an rdf type restriction" do
31
- person_criteria.send(:build_select_query).should == "SELECT ?uri (<http://graph> as ?graph) WHERE { GRAPH <http://graph> { ?uri a <http://person> } }"
31
+ person_criteria.send(:build_select_query).should == "SELECT DISTINCT ?uri (<http://graph> as ?graph) WHERE { GRAPH <http://graph> { ?uri a <http://person> } }"
32
32
  end
33
33
 
34
34
  context "and extra restrictions" do
35
35
  before { person_criteria.where("[pattern]") }
36
36
 
37
37
  it "should return a SELECT query with the extra restriction" do
38
- person_criteria.send(:build_select_query).should == "SELECT ?uri (<http://graph> as ?graph) WHERE { GRAPH <http://graph> { ?uri a <http://person> . [pattern] } }"
38
+ person_criteria.send(:build_select_query).should == "SELECT DISTINCT ?uri (<http://graph> as ?graph) WHERE { GRAPH <http://graph> { ?uri a <http://person> . [pattern] } }"
39
39
  end
40
40
  end
41
41
 
@@ -43,21 +43,21 @@ describe Tripod::Criteria do
43
43
  before { person_criteria.graph("http://anothergraph") }
44
44
 
45
45
  it "should override the graph in the query" do
46
- person_criteria.send(:build_select_query).should == "SELECT ?uri (<http://anothergraph> as ?graph) WHERE { GRAPH <http://anothergraph> { ?uri a <http://person> } }"
46
+ person_criteria.send(:build_select_query).should == "SELECT DISTINCT ?uri (<http://anothergraph> as ?graph) WHERE { GRAPH <http://anothergraph> { ?uri a <http://person> } }"
47
47
  end
48
48
  end
49
49
  end
50
50
 
51
51
  context "for a class without an rdf_type and graph" do
52
52
  it "should return a SELECT query without an rdf_type restriction" do
53
- resource_criteria.send(:build_select_query).should == "SELECT ?uri ?graph WHERE { GRAPH ?graph { ?uri ?p ?o } }"
53
+ resource_criteria.send(:build_select_query).should == "SELECT DISTINCT ?uri ?graph WHERE { GRAPH ?graph { ?uri ?p ?o } }"
54
54
  end
55
55
 
56
56
  context "and extra restrictions" do
57
57
  before { resource_criteria.where("[pattern]") }
58
58
 
59
59
  it "should return a SELECT query with the extra restrictions" do
60
- resource_criteria.send(:build_select_query).should == "SELECT ?uri ?graph WHERE { GRAPH ?graph { ?uri ?p ?o . [pattern] } }"
60
+ resource_criteria.send(:build_select_query).should == "SELECT DISTINCT ?uri ?graph WHERE { GRAPH ?graph { ?uri ?p ?o . [pattern] } }"
61
61
  end
62
62
  end
63
63
 
@@ -65,7 +65,7 @@ describe Tripod::Criteria do
65
65
  before { resource_criteria.graph("http://graphy") }
66
66
 
67
67
  it "should override the graph in the query" do
68
- resource_criteria.send(:build_select_query).should == "SELECT ?uri (<http://graphy> as ?graph) WHERE { GRAPH <http://graphy> { ?uri ?p ?o } }"
68
+ resource_criteria.send(:build_select_query).should == "SELECT DISTINCT ?uri (<http://graphy> as ?graph) WHERE { GRAPH <http://graphy> { ?uri ?p ?o } }"
69
69
  end
70
70
  end
71
71
  end
@@ -75,7 +75,7 @@ describe Tripod::Criteria do
75
75
  before { resource_criteria.where("[pattern]").extras("LIMIT 10").extras("OFFSET 20") }
76
76
 
77
77
  it "should add the extras on the end" do
78
- resource_criteria.send(:build_select_query).should == "SELECT ?uri ?graph WHERE { GRAPH ?graph { ?uri ?p ?o . [pattern] } } LIMIT 10 OFFSET 20"
78
+ resource_criteria.send(:build_select_query).should == "SELECT DISTINCT ?uri ?graph WHERE { GRAPH ?graph { ?uri ?p ?o . [pattern] } } LIMIT 10 OFFSET 20"
79
79
  end
80
80
  end
81
81
  end
@@ -86,7 +86,7 @@ describe Tripod::Criteria do
86
86
 
87
87
  context "with no extra restrictions" do
88
88
  it "should return a set of hydrated objects for the type" do
89
- person_criteria.resources.should == [john, barry]
89
+ person_criteria.resources.to_a.should == [john, barry]
90
90
  end
91
91
  end
92
92
 
@@ -95,7 +95,7 @@ describe Tripod::Criteria do
95
95
  before { person_criteria.where("?uri <http://name> 'John'") }
96
96
 
97
97
  it "should return a set of hydrated objects for the type and restrictions" do
98
- person_criteria.resources.should == [john]
98
+ person_criteria.resources.to_a.should == [john]
99
99
  end
100
100
  end
101
101
 
@@ -129,7 +129,7 @@ describe Tripod::Criteria do
129
129
  end
130
130
 
131
131
  it "should execute the right Sparql" do
132
- sparql = "SELECT COUNT(*) { SELECT ?uri (<http://graph> as ?graph) WHERE { GRAPH <http://graph> { ?uri a <http://person> } } LIMIT 10 OFFSET 20 }"
132
+ sparql = "SELECT COUNT(*) { SELECT DISTINCT ?uri (<http://graph> as ?graph) WHERE { GRAPH <http://graph> { ?uri a <http://person> } } LIMIT 10 OFFSET 20 }"
133
133
  Tripod::SparqlClient::Query.should_receive(:select).with(sparql).and_call_original
134
134
  Person.all.limit(10).offset(20).count
135
135
  end
@@ -141,13 +141,13 @@ describe Tripod::Criteria do
141
141
  let(:chained_criteria) { Person.where("?uri <http://name> ?name").limit(1).offset(0).order("DESC(?name)") }
142
142
 
143
143
  it "should run the right Sparql" do
144
- sparql = "SELECT ?uri (<http://graph> as ?graph) WHERE { GRAPH <http://graph> { ?uri a <http://person> . ?uri <http://name> ?name } } ORDER BY DESC(?name) LIMIT 1 OFFSET 0"
144
+ sparql = "SELECT DISTINCT ?uri (<http://graph> as ?graph) WHERE { GRAPH <http://graph> { ?uri a <http://person> . ?uri <http://name> ?name } } ORDER BY DESC(?name) LIMIT 1 OFFSET 0"
145
145
  Tripod::SparqlClient::Query.should_receive(:select).with(sparql).and_call_original
146
146
  chained_criteria.resources
147
147
  end
148
148
 
149
149
  it "should return the right resources" do
150
- chained_criteria.resources.should == [john]
150
+ chained_criteria.resources.to_a.should == [john]
151
151
  end
152
152
 
153
153
  it "should return the right number of resources" do
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.3.4
4
+ version: 0.3.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,11 +11,11 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-02-21 00:00:00.000000000 Z
14
+ date: 2013-02-25 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rest-client
18
- requirement: &70190726102800 !ruby/object:Gem::Requirement
18
+ requirement: &70333194336060 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
21
  - - ! '>='
@@ -23,10 +23,10 @@ dependencies:
23
23
  version: '0'
24
24
  type: :runtime
25
25
  prerelease: false
26
- version_requirements: *70190726102800
26
+ version_requirements: *70333194336060
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activemodel
29
- requirement: &70190726101660 !ruby/object:Gem::Requirement
29
+ requirement: &70333194335020 !ruby/object:Gem::Requirement
30
30
  none: false
31
31
  requirements:
32
32
  - - ~>
@@ -34,10 +34,10 @@ dependencies:
34
34
  version: '3.1'
35
35
  type: :runtime
36
36
  prerelease: false
37
- version_requirements: *70190726101660
37
+ version_requirements: *70333194335020
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: equivalent-xml
40
- requirement: &70190726100840 !ruby/object:Gem::Requirement
40
+ requirement: &70333194334540 !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
43
  - - ! '>='
@@ -45,10 +45,10 @@ dependencies:
45
45
  version: '0'
46
46
  type: :runtime
47
47
  prerelease: false
48
- version_requirements: *70190726100840
48
+ version_requirements: *70333194334540
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: rdf
51
- requirement: &70190730540020 !ruby/object:Gem::Requirement
51
+ requirement: &70333194333460 !ruby/object:Gem::Requirement
52
52
  none: false
53
53
  requirements:
54
54
  - - ~>
@@ -56,10 +56,10 @@ dependencies:
56
56
  version: '1.0'
57
57
  type: :runtime
58
58
  prerelease: false
59
- version_requirements: *70190730540020
59
+ version_requirements: *70333194333460
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: rdf-rdfxml
62
- requirement: &70190730539320 !ruby/object:Gem::Requirement
62
+ requirement: &70333194332500 !ruby/object:Gem::Requirement
63
63
  none: false
64
64
  requirements:
65
65
  - - ! '>='
@@ -67,10 +67,10 @@ dependencies:
67
67
  version: '0'
68
68
  type: :runtime
69
69
  prerelease: false
70
- version_requirements: *70190730539320
70
+ version_requirements: *70333194332500
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: rdf-n3
73
- requirement: &70190730538300 !ruby/object:Gem::Requirement
73
+ requirement: &70333194331640 !ruby/object:Gem::Requirement
74
74
  none: false
75
75
  requirements:
76
76
  - - ! '>='
@@ -78,10 +78,10 @@ dependencies:
78
78
  version: '0'
79
79
  type: :runtime
80
80
  prerelease: false
81
- version_requirements: *70190730538300
81
+ version_requirements: *70333194331640
82
82
  - !ruby/object:Gem::Dependency
83
83
  name: rdf-json
84
- requirement: &70190730537520 !ruby/object:Gem::Requirement
84
+ requirement: &70333194331040 !ruby/object:Gem::Requirement
85
85
  none: false
86
86
  requirements:
87
87
  - - ! '>='
@@ -89,10 +89,10 @@ dependencies:
89
89
  version: '0'
90
90
  type: :runtime
91
91
  prerelease: false
92
- version_requirements: *70190730537520
92
+ version_requirements: *70333194331040
93
93
  - !ruby/object:Gem::Dependency
94
94
  name: json-ld
95
- requirement: &70190730536780 !ruby/object:Gem::Requirement
95
+ requirement: &70333194330380 !ruby/object:Gem::Requirement
96
96
  none: false
97
97
  requirements:
98
98
  - - ! '>='
@@ -100,10 +100,10 @@ dependencies:
100
100
  version: '0'
101
101
  type: :runtime
102
102
  prerelease: false
103
- version_requirements: *70190730536780
103
+ version_requirements: *70333194330380
104
104
  - !ruby/object:Gem::Dependency
105
105
  name: guid
106
- requirement: &70190730536300 !ruby/object:Gem::Requirement
106
+ requirement: &70333194329820 !ruby/object:Gem::Requirement
107
107
  none: false
108
108
  requirements:
109
109
  - - ! '>='
@@ -111,7 +111,7 @@ dependencies:
111
111
  version: '0'
112
112
  type: :runtime
113
113
  prerelease: false
114
- version_requirements: *70190730536300
114
+ version_requirements: *70333194329820
115
115
  description: RDF ruby ORM
116
116
  email:
117
117
  - ric@swirrl.com
@@ -151,6 +151,7 @@ files:
151
151
  - lib/tripod/predicates.rb
152
152
  - lib/tripod/repository.rb
153
153
  - lib/tripod/resource.rb
154
+ - lib/tripod/resource_collection.rb
154
155
  - lib/tripod/serialization.rb
155
156
  - lib/tripod/sparql_client.rb
156
157
  - lib/tripod/sparql_query.rb