tripod 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -6,8 +6,6 @@ ActiveModel-style Ruby ORM for RDF Linked Data. Works with SPARQL 1.1 HTTP endpo
6
6
  * Inspired by [Durran Jordan's](https://github.com/durran) [Mongoid](http://mongoid.org/en/mongoid/) ORM for [MongoDB](http://www.mongodb.org/), and [Ben Lavender's](https://github.com/bhuga) RDF ORM, [Spira](https://github.com/ruby-rdf/spira).
7
7
  * Uses [Ruby-RDF](https://github.com/ruby-rdf/rdf) to manage the data internally.
8
8
 
9
- __Warning: Some features are still experimental.__
10
-
11
9
  ## Quick start, for using in a rails app.
12
10
 
13
11
  Note: Tripod doesn't supply a database. You need to install one. I recommend [Fuseki](http://jena.apache.org/documentation/serving_data/index.html), which runs on port 3030 by default.
@@ -34,6 +32,7 @@ Note: Tripod doesn't supply a database. You need to install one. I recommend [Fu
34
32
  class Person
35
33
  include Tripod::Resource
36
34
 
35
+ # these are the default rdf-type and graph for resources of this class
37
36
  rdf_type 'http://person'
38
37
  graph_uri 'http://people'
39
38
 
@@ -72,6 +71,40 @@ Note: Tripod doesn't supply a database. You need to install one. I recommend [Fu
72
71
  ric = Person.find('http://ric')
73
72
  # => returns a single Person object.
74
73
 
75
- [Full Documentation](http://rubydoc.info/github/Swirrl/tripod/master/frames)
74
+ ## Some Other interesting features
75
+
76
+ ## Eager Loading
77
+
78
+ asa = Person.find('http://asa')
79
+ ric = Person.find('http://ric')
80
+ ric.knows = asa.uri
81
+
82
+ ric.eager_load_predicate_triples! #does a big DESCRIBE statement behind the scenes
83
+ knows = ric.get_related_resource('http://knows', Resource)
84
+ knows.label # this won't cause another database lookup
85
+
86
+ ric.eager_load_object_triples! #does a big DESCRIBE statement behind the scenes
87
+ asa = ric.get_related_resource('http://knows', Person) # returns a fully hydrated Person object for asa, without an extra lookup
88
+
89
+ ## Defining a graph at instantiation-time
90
+
91
+ class Resource
92
+ field :label RDF::RDFS.label
93
+
94
+ # notice also that you don't need to supply an rdf type or graph here!
95
+ end
96
+
97
+ r = Resource.new('http://foo', 'http://mygraph')
98
+
99
+ # if you don't supply a graph at any point, you will get an error when you try to persist the resource.
100
+
101
+ ## Reading and writing arbitrary predicates
102
+
103
+ r.write_predicate(RDF.type, 'http://myresource/type')
104
+ r.read_predicate(RDF.type) # => RDF::URI.new("http://myresource/type")
105
+
106
+
107
+
108
+ [Full Documentation](http://rubydoc.info/gems/tripod/frames)
76
109
 
77
110
  Copyright (c) 2012 [Swirrl IT Limited](http://swirrl.com). Released under MIT License
@@ -20,7 +20,7 @@ module Tripod::EagerLoading
20
20
  def eager_load_object_triples!
21
21
  object_uris = []
22
22
 
23
- self.repository.query( [RDF::URI.new(self.uri), :predicate, :object] ) do |statement|
23
+ self.get_triples_for_this_resource.each_statement do |statement|
24
24
  object_uris << statement.object if statement.object.uri?
25
25
  end
26
26
 
@@ -39,8 +39,7 @@ module Tripod::EagerLoading
39
39
  end
40
40
 
41
41
  if data_graph.empty?
42
- # this means that we've not already looked it up
43
- r = class_of_resource_to_create.find(resource_uri)
42
+ r = nil
44
43
  else
45
44
  # it's in our eager loaded repo
46
45
  r = class_of_resource_to_create.new(resource_uri)
@@ -8,9 +8,11 @@ module Tripod::Predicates
8
8
  # returns a list of predicates (as RDF::URIs) for this resource
9
9
  def predicates
10
10
  pred_uris = []
11
- @repository.statements.each do |s|
12
- pred_uris << s.predicate unless pred_uris.include?(s.predicate)
11
+
12
+ @repository.query( [@uri, :predicate, :object] ) do |statement|
13
+ pred_uris << statement.predicate unless pred_uris.include?(statement.predicate)
13
14
  end
15
+
14
16
  pred_uris
15
17
  end
16
18
 
@@ -26,7 +28,7 @@ module Tripod::Predicates
26
28
  # @return [ Array ] An array of RDF::Terms.
27
29
  def read_predicate(predicate_uri)
28
30
  values = []
29
- @repository.query( [:subject, RDF::URI.new(predicate_uri.to_s), :object] ) do |statement|
31
+ @repository.query( [@uri, RDF::URI.new(predicate_uri.to_s), :object] ) do |statement|
30
32
  values << statement.object
31
33
  end
32
34
  values
@@ -28,7 +28,7 @@ module Tripod::Repository
28
28
 
29
29
  if graph
30
30
  graph.each_statement do |statement|
31
- # only use statements about this resource!
31
+ # only use statements about this resource for hydrating
32
32
  if statement.subject.to_s == @uri.to_s
33
33
  @repository << statement
34
34
  end
@@ -46,6 +46,15 @@ module Tripod::Repository
46
46
 
47
47
  end
48
48
 
49
+ # returns a graph of triples from the underlying repository where this resource's uri is the subject.
50
+ def get_triples_for_this_resource
51
+ triples_graph = RDF::Graph.new
52
+ @repository.query([RDF::URI.new(self.uri), :predicate, :object]) do |stmt|
53
+ triples_graph << stmt
54
+ end
55
+ triples_graph
56
+ end
57
+
49
58
  module ClassMethods
50
59
 
51
60
  # for triples in the graph passed in, add them to the passed in repository obj, and return the repository objects
@@ -6,22 +6,22 @@ module Tripod::Serialization
6
6
 
7
7
  # Serialises this resource's triples to rdf/xml
8
8
  def to_rdf
9
- @repository.dump(:rdfxml)
9
+ get_triples_for_this_resource.dump(:rdfxml)
10
10
  end
11
11
 
12
12
  # Serialises this resource's triples to turtle
13
13
  def to_ttl
14
- @repository.dump(:n3)
14
+ get_triples_for_this_resource.dump(:n3)
15
15
  end
16
16
 
17
17
  # Serialises this resource's triples to n-triples
18
18
  def to_nt
19
- @repository.dump(:ntriples)
19
+ get_triples_for_this_resource.dump(:ntriples)
20
20
  end
21
21
 
22
22
  # Serialises this resource's triples to JSON-LD
23
23
  def to_json(opts={})
24
- @repository.dump(:jsonld)
24
+ get_triples_for_this_resource.dump(:jsonld)
25
25
  end
26
26
 
27
27
  end
@@ -1,3 +1,3 @@
1
1
  module Tripod
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -55,10 +55,20 @@ describe Tripod::EagerLoading do
55
55
  describe "#get_related_resource" do
56
56
 
57
57
  context "when eager load not called" do
58
- it "should do a find on the right class to get the right instance of the resource class passed in" do
59
- Person.should_receive(:find).with( @peter.uri ).and_call_original
60
- res = @john.get_related_resource(@peter.uri, Person)
61
- res.should == @peter
58
+
59
+ context "and related resource exists" do
60
+ it "should return nil" do
61
+ res = @john.get_related_resource(@peter.uri, Person)
62
+ res.should == nil
63
+ end
64
+ end
65
+
66
+ context "and related resource doesn't exist" do
67
+
68
+ it "should return nil" do
69
+ res = @john.get_related_resource(RDF::URI.new('http://nonexistent/person'), Person)
70
+ res.should be_nil
71
+ end
62
72
  end
63
73
  end
64
74
 
@@ -98,6 +108,7 @@ describe Tripod::EagerLoading do
98
108
  end
99
109
  end
100
110
 
111
+
101
112
  end
102
113
 
103
114
  end
@@ -23,6 +23,13 @@ describe Tripod::Predicates do
23
23
  stmt3.predicate = RDF::URI.new('http://name')
24
24
  stmt3.object = "ric"
25
25
  @graph << stmt3
26
+
27
+ # throw a random other statement (about name) in the mix!
28
+ stmt4 = RDF::Statement.new
29
+ stmt4.subject = RDF::URI.new('http://name')
30
+ stmt4.predicate = RDF::RDFS.label
31
+ stmt4.object = "name"
32
+ @graph << stmt4
26
33
  end
27
34
 
28
35
  let(:person) do
@@ -77,6 +84,8 @@ describe Tripod::Predicates do
77
84
  person.predicates.length.should == 2
78
85
  person.predicates.should == [RDF::URI('http://blog'), RDF::URI('http://name')]
79
86
  end
87
+
88
+
80
89
  end
81
90
 
82
91
  end
@@ -3,34 +3,79 @@ require "spec_helper"
3
3
  describe Tripod::Serialization do
4
4
 
5
5
  let(:person) do
6
+
7
+ p2 = Person.new('http://fred')
8
+ p2.name = "fred"
9
+ p2.save!
10
+
6
11
  p = Person.new('http://garry')
7
12
  p.name = 'Garry'
8
13
  p.age = 30
14
+ p.knows = p2.uri
9
15
  p
10
16
  end
11
17
 
12
- describe "#to_rdf" do
13
- it "should dump the contents of the repository as rdfxml" do
14
- person.to_rdf.should == person.repository.dump(:rdfxml)
18
+ context "where no eager loading has happened" do
19
+
20
+ describe "#to_rdf" do
21
+ it "should dump the contents of the repository as rdfxml" do
22
+ person.to_rdf.should == person.repository.dump(:rdfxml)
23
+ end
15
24
  end
16
- end
17
25
 
18
- describe "#to_ttl" do
19
- it "should dump the contents of the repository with the n3 serializer" do
20
- person.to_ttl.should == person.repository.dump(:n3)
26
+ describe "#to_ttl" do
27
+ it "should dump the contents of the repository with the n3 serializer" do
28
+ person.to_ttl.should == person.repository.dump(:n3)
29
+ end
21
30
  end
22
- end
23
31
 
24
- describe "#to_nt" do
25
- it "should dump the contents of the repository as ntriples" do
26
- person.to_nt.should == person.repository.dump(:ntriples)
32
+ describe "#to_nt" do
33
+ it "should dump the contents of the repository as ntriples" do
34
+ person.to_nt.should == person.repository.dump(:ntriples)
35
+ end
27
36
  end
37
+
38
+ describe "#to_json" do
39
+ it "should dump the contents of the repository as ntriples" do
40
+ person.to_json.should == person.repository.dump(:jsonld)
41
+ end
42
+ end
43
+
28
44
  end
29
45
 
30
- describe "#to_json" do
31
- it "should dump the contents of the repository as ntriples" do
32
- person.to_json.should == person.repository.dump(:jsonld)
46
+ context "where eager loading has happened" do
47
+
48
+ before do
49
+ person.eager_load_predicate_triples!
50
+ person.eager_load_object_triples!
51
+ end
52
+
53
+ describe "#to_rdf" do
54
+ it "should dump the triples for this resource only as rdfxml" do
55
+ person.to_rdf.should == person.get_triples_for_this_resource.dump(:rdfxml)
56
+ end
57
+ end
58
+
59
+ describe "#to_ttl" do
60
+ it "should dump the triples for this resource only with the n3 serializer" do
61
+ person.to_ttl.should == person.get_triples_for_this_resource.dump(:n3)
62
+ end
63
+ end
64
+
65
+ describe "#to_nt" do
66
+ it "should dump the triples for this resource only as ntriples" do
67
+ person.to_nt.should == person.get_triples_for_this_resource.dump(:ntriples)
68
+ end
33
69
  end
70
+
71
+ describe "#to_json" do
72
+ it "should dump the triples for this resource only as ntriples" do
73
+ person.to_json.should == person.get_triples_for_this_resource.dump(:jsonld)
74
+ end
75
+ end
76
+
77
+
78
+
34
79
  end
35
80
 
36
81
  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.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -15,7 +15,7 @@ date: 2013-02-06 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rest-client
18
- requirement: &70274473451860 !ruby/object:Gem::Requirement
18
+ requirement: &70191292757280 !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: *70274473451860
26
+ version_requirements: *70191292757280
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activemodel
29
- requirement: &70274473450860 !ruby/object:Gem::Requirement
29
+ requirement: &70191292756400 !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: *70274473450860
37
+ version_requirements: *70191292756400
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: equivalent-xml
40
- requirement: &70274473466560 !ruby/object:Gem::Requirement
40
+ requirement: &70191292771960 !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: *70274473466560
48
+ version_requirements: *70191292771960
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: rdf
51
- requirement: &70274473465400 !ruby/object:Gem::Requirement
51
+ requirement: &70191292770960 !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: *70274473465400
59
+ version_requirements: *70191292770960
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: rdf-rdfxml
62
- requirement: &70274473464180 !ruby/object:Gem::Requirement
62
+ requirement: &70191292769560 !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: *70274473464180
70
+ version_requirements: *70191292769560
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: rdf-n3
73
- requirement: &70274473463580 !ruby/object:Gem::Requirement
73
+ requirement: &70191292768980 !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: *70274473463580
81
+ version_requirements: *70191292768980
82
82
  - !ruby/object:Gem::Dependency
83
83
  name: rdf-json
84
- requirement: &70274473462900 !ruby/object:Gem::Requirement
84
+ requirement: &70191292768400 !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: *70274473462900
92
+ version_requirements: *70191292768400
93
93
  - !ruby/object:Gem::Dependency
94
94
  name: json-ld
95
- requirement: &70274473462160 !ruby/object:Gem::Requirement
95
+ requirement: &70191292767540 !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: *70274473462160
103
+ version_requirements: *70191292767540
104
104
  - !ruby/object:Gem::Dependency
105
105
  name: guid
106
- requirement: &70274473461300 !ruby/object:Gem::Requirement
106
+ requirement: &70191292766780 !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: *70274473461300
114
+ version_requirements: *70191292766780
115
115
  description: RDF ruby ORM
116
116
  email:
117
117
  - ric@swirrl.com