tripod 0.3.0 → 0.3.1

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/README.md CHANGED
@@ -73,7 +73,7 @@ Note: Tripod doesn't supply a database. You need to install one. I recommend [Fu
73
73
  knows.label # this won't cause another database lookup
74
74
 
75
75
  ric.eager_load_object_triples! #does a big DESCRIBE statement behind the scenes
76
- asa = ric.get_related_resource('http://knows', Person) # returns a fully hydrated Person object for asa, without an extra lookup
76
+ asa = ric.get_related_resource('http://asa', Person) # returns a fully hydrated Person object for asa, without an extra lookup
77
77
 
78
78
  ## Defining a graph at instantiation-time
79
79
 
@@ -88,32 +88,39 @@ Note: Tripod doesn't supply a database. You need to install one. I recommend [Fu
88
88
  r.label = "example"
89
89
  r.save
90
90
 
91
- # Note: if you don't supply a graph at any point (i.e. class or instance level), you will get an error when you try to persist the resource.
91
+ # Note: Tripod assumes you want to store all resources in named graphs.
92
+ # So if you don't supply a graph at any point (i.e. class or instance level),
93
+ # you will get an error when you try to persist the resource.
92
94
 
93
95
  ## Reading and writing arbitrary predicates
94
96
 
95
97
  r.write_predicate(RDF.type, 'http://myresource/type')
96
- r.read_predicate(RDF.type) # => RDF::URI.new("http://myresource/type")
98
+ r.read_predicate(RDF.type) #=> [RDF::URI.new("http://myresource/type")]
97
99
 
98
100
  ## Finders and criteria
99
101
 
100
- Person.all #=> returns a Tripod::Criteria which selets all resources of rdf_type http://person
102
+ # A Tripod::Criteria object defines a set of constraints for a SPARQL query.
103
+ # It doesn't actually do anything against the DB until you run resources, first, or count on it.
104
+ # (from Tripod::CriteriaExecution)
101
105
 
102
- Resource.all #=> returns all resources in the database (as no rdf_type specified at class level)
106
+ Person.all #=> returns a Tripod::Criteria object which selects all resources of rdf_type http://person
103
107
 
104
- Person.all.resources #=> returns all the actual resources for the criteria, as an array
108
+ Resource.all #=> returns a criteria object to return resources in the database (as no rdf_type specified at class level)
105
109
 
106
- Person.first #=> returns the first person
110
+ Person.all.resources #=> returns all the actual resources for the criteria object, as an array
107
111
 
108
- Person.count #=> returns the count of all people
112
+ Person.first #=> returns the first person (by crafting a sparql query under the covers that only returns 1 result)
113
+
114
+ Person.count #=> returns the count of all people (by crafting a count query under the covers that only returns a count)
109
115
 
110
116
  # note that you need to use ?uri as the variable for the subject.
111
- Person.where("?uri <http://name> 'Joe'") #=> returns a Tripod::Criteria
117
+ Person.where("?uri <http://name> 'Joe'") #=> returns a Tripod::Criteria object
112
118
 
113
119
  ## Chainable criteria
114
120
 
115
121
  Person.all.where("?uri <http://name> 'Ric'").where("?uri <http://knows> <http://asa>).first
116
122
 
123
+ Person.where("?uri <http://name> ?name").limit(1).offset(0).order("DESC(?name)")
117
124
 
118
125
 
119
126
  [Full Documentation](http://rubydoc.info/gems/tripod/frames)
@@ -11,12 +11,13 @@ module Tripod
11
11
  # the resource class that this criteria is for.
12
12
  attr_accessor :resource_class
13
13
 
14
- # array of all the where clauses in this criteria
15
14
  attr_accessor :where_clauses
16
-
17
- # array of all the extra clauses in this criteria
18
15
  attr_accessor :extra_clauses
19
16
 
17
+ attr_accessor :limit_clause
18
+ attr_accessor :order_clause
19
+ attr_accessor :offset_clause
20
+
20
21
  def initialize(resource_class)
21
22
  self.resource_class = resource_class
22
23
  self.where_clauses = []
@@ -46,11 +47,31 @@ module Tripod
46
47
  end
47
48
 
48
49
  # takes a string and adds an extra clause to this criteria.
50
+ # e.g. my_criteria.extras("LIMIT 10 OFFSET 20").extrass
51
+ #
49
52
  # TODO: make it also take a hash?
50
53
  def extras(sparql_snippet)
51
54
  extra_clauses << sparql_snippet
52
55
  self
53
56
  end
54
57
 
58
+ # replaces this criteria's limit clause
59
+ def limit(the_limit)
60
+ self.limit_clause = "LIMIT #{the_limit.to_s}"
61
+ self
62
+ end
63
+
64
+ # replaces this criteria's offset clause
65
+ def offset(the_offset)
66
+ self.offset_clause = "OFFSET #{the_offset.to_s}"
67
+ self
68
+ end
69
+
70
+ # replaces this criteria's order clause
71
+ def order(param)
72
+ self.order_clause = "ORDER BY #{param}"
73
+ self
74
+ end
75
+
55
76
  end
56
77
  end
@@ -38,10 +38,18 @@ module Tripod
38
38
  end
39
39
 
40
40
  def build_select_query
41
+
42
+ # convert the order, limit and offset to extras in the right order
43
+ extras(order_clause)
44
+ extras(limit_clause)
45
+ extras(offset_clause)
46
+
47
+ # build the query.
41
48
  select_query = "SELECT ?uri ?graph WHERE { GRAPH ?graph { "
42
49
  select_query += self.where_clauses.join(" . ")
43
- # TODO: Deal with extras.
44
- select_query += " } }"
50
+ select_query += " } } "
51
+ select_query += self.extra_clauses.join(" ")
52
+ select_query.strip
45
53
  end
46
54
 
47
55
  # create and hydrate the resources identified in uris_and_graphs.
@@ -1,3 +1,3 @@
1
1
  module Tripod
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -51,6 +51,15 @@ describe Tripod::Criteria do
51
51
  end
52
52
  end
53
53
  end
54
+
55
+ context "with extras" do
56
+
57
+ before { resource_criteria.where("[pattern]").extras("LIMIT 10").extras("OFFSET 20") }
58
+
59
+ it "should add the extras on the end" do
60
+ resource_criteria.send(:build_select_query).should == "SELECT ?uri ?graph WHERE { GRAPH ?graph { ?uri ?p ?o . [pattern] } } LIMIT 10 OFFSET 20"
61
+ end
62
+ end
54
63
  end
55
64
 
56
65
  describe "#resources" do
@@ -115,4 +124,28 @@ describe Tripod::Criteria do
115
124
 
116
125
  end
117
126
 
127
+ describe "exeuting a chained criteria" do
128
+
129
+ before do
130
+ john.save!
131
+ barry.save!
132
+ end
133
+
134
+ let(:chained_criteria) { Person.where("?uri <http://name> ?name").limit(1).offset(0).order("DESC(?name)") }
135
+
136
+ it "should run the right Sparql" do
137
+ sparql = "SELECT ?uri ?graph WHERE { GRAPH ?graph { ?uri a <http://person> . ?uri <http://name> ?name } } ORDER BY DESC(?name) LIMIT 1 OFFSET 0"
138
+ Tripod::SparqlClient::Query.should_receive(:select).with(sparql).and_call_original
139
+ chained_criteria.resources
140
+ end
141
+
142
+ it "should return the right resources" do
143
+ chained_criteria.resources.should == [john]
144
+ end
145
+
146
+ it "should return the right number of resources" do
147
+ chained_criteria.count.should == 1
148
+ end
149
+ end
150
+
118
151
  end
@@ -62,4 +62,49 @@ describe Tripod::Criteria do
62
62
  end
63
63
  end
64
64
 
65
+ describe "#limit" do
66
+ it "calls extras with the right limit clause" do
67
+ resource_criteria.limit(10)
68
+ resource_criteria.limit_clause.should == "LIMIT 10"
69
+ end
70
+
71
+ context 'calling it twice' do
72
+ it 'should overwrite the previous version' do
73
+ resource_criteria.limit(10)
74
+ resource_criteria.limit(20)
75
+ resource_criteria.limit_clause.should == "LIMIT 20"
76
+ end
77
+ end
78
+ end
79
+
80
+ describe "#offset" do
81
+ it "calls extras with the right limit clause" do
82
+ resource_criteria.offset(10)
83
+ resource_criteria.offset_clause.should == "OFFSET 10"
84
+ end
85
+
86
+ context 'calling it twice' do
87
+ it 'should overwrite the previous version' do
88
+ resource_criteria.offset(10)
89
+ resource_criteria.offset(30)
90
+ resource_criteria.offset_clause.should == "OFFSET 30"
91
+ end
92
+ end
93
+ end
94
+
95
+ describe "#order" do
96
+ it "calls extras with the right limit clause" do
97
+ resource_criteria.order("DESC(?label)")
98
+ resource_criteria.order_clause.should == "ORDER BY DESC(?label)"
99
+ end
100
+
101
+ context 'calling it twice' do
102
+ it 'should overwrite the previous version' do
103
+ resource_criteria.order("DESC(?label)")
104
+ resource_criteria.order("ASC(?label)")
105
+ resource_criteria.order_clause.should == "ORDER BY ASC(?label)"
106
+ end
107
+ end
108
+ end
109
+
65
110
  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.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -15,7 +15,7 @@ date: 2013-02-20 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rest-client
18
- requirement: &70158128550280 !ruby/object:Gem::Requirement
18
+ requirement: &70281327925380 !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: *70158128550280
26
+ version_requirements: *70281327925380
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activemodel
29
- requirement: &70158128549680 !ruby/object:Gem::Requirement
29
+ requirement: &70281327924780 !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: *70158128549680
37
+ version_requirements: *70281327924780
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: equivalent-xml
40
- requirement: &70158128549220 !ruby/object:Gem::Requirement
40
+ requirement: &70281327924320 !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: *70158128549220
48
+ version_requirements: *70281327924320
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: rdf
51
- requirement: &70158128548580 !ruby/object:Gem::Requirement
51
+ requirement: &70281327923680 !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: *70158128548580
59
+ version_requirements: *70281327923680
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: rdf-rdfxml
62
- requirement: &70158128548020 !ruby/object:Gem::Requirement
62
+ requirement: &70281327923100 !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: *70158128548020
70
+ version_requirements: *70281327923100
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: rdf-n3
73
- requirement: &70158128547360 !ruby/object:Gem::Requirement
73
+ requirement: &70281327922480 !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: *70158128547360
81
+ version_requirements: *70281327922480
82
82
  - !ruby/object:Gem::Dependency
83
83
  name: rdf-json
84
- requirement: &70158128546600 !ruby/object:Gem::Requirement
84
+ requirement: &70281327921720 !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: *70158128546600
92
+ version_requirements: *70281327921720
93
93
  - !ruby/object:Gem::Dependency
94
94
  name: json-ld
95
- requirement: &70158128545840 !ruby/object:Gem::Requirement
95
+ requirement: &70281327920960 !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: *70158128545840
103
+ version_requirements: *70281327920960
104
104
  - !ruby/object:Gem::Dependency
105
105
  name: guid
106
- requirement: &70158128545340 !ruby/object:Gem::Requirement
106
+ requirement: &70281327920460 !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: *70158128545340
114
+ version_requirements: *70281327920460
115
115
  description: RDF ruby ORM
116
116
  email:
117
117
  - ric@swirrl.com