tripod 0.7.26 → 0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,30 +12,45 @@ module Tripod::Finders
12
12
  # Person.find('http://ric')
13
13
  # Person.find(RDF::URI('http://ric'))
14
14
  # @example Find a single resource by uri and graph
15
+ # Person.find('http://ric', :graph_uri => 'http://example.com/people')
16
+ # @example Find a single resource by uri, looking in any graph (i.e. the UNION graph)
17
+ # Person.find('http://ric', :ignore_graph => true)
18
+ # @example Find a single resource by uri and graph (DEPRECATED)
15
19
  # Person.find('http://ric', 'http://example.com/people')
16
20
  # Person.find(RDF::URI('http://ric'), Person.find(RDF::URI('http://example.com/people')))
17
21
  #
18
22
  # @param [ String, RDF::URI ] uri The uri of the resource to find
19
- # @param [ String, RDF::URI ] graph_uri The uri of the graph from which to get the resource
23
+ # @param [ Hash, String, RDF::URI ] opts Either an options hash (see above), or (for backwards compatibility) the uri of the graph from which to get the resource
20
24
  #
21
25
  # @raise [ Tripod::Errors::ResourceNotFound ] If no resource found.
22
26
  #
23
27
  # @return [ Resource ] A single resource
24
- def find(uri, graph_uri=nil)
25
-
26
- unless graph_uri
27
- # do a quick select to see what graph to use.
28
- select_query = "SELECT ?g WHERE { GRAPH ?g {<#{uri.to_s}> ?p ?o } } LIMIT 1"
29
- result = Tripod::SparqlClient::Query.select(select_query)
30
- if result.length > 0
31
- graph_uri = result[0]["g"]["value"]
32
- else
33
- raise Tripod::Errors::ResourceNotFound.new(uri)
34
- end
28
+ def find(uri, opts={})
29
+ if opts.is_a?(String) # backward compatibility hack
30
+ graph_uri = opts
31
+ ignore_graph = false
32
+ else
33
+ graph_uri = opts.fetch(:graph_uri, nil)
34
+ ignore_graph = opts.fetch(:ignore_graph, false)
35
35
  end
36
36
 
37
- # instantiate and hydrate the resource
38
- resource = self.new(uri, graph_uri.to_s)
37
+ resource = nil
38
+ if ignore_graph
39
+ resource = self.new(uri, :ignore_graph => true)
40
+ else
41
+ graph_uri ||= self.get_graph_uri
42
+ unless graph_uri
43
+ # do a quick select to see what graph to use.
44
+ select_query = "SELECT ?g WHERE { GRAPH ?g {<#{uri.to_s}> ?p ?o } } LIMIT 1"
45
+ result = Tripod::SparqlClient::Query.select(select_query)
46
+ if result.length > 0
47
+ graph_uri = result[0]["g"]["value"]
48
+ else
49
+ raise Tripod::Errors::ResourceNotFound.new(uri)
50
+ end
51
+ end
52
+ resource = self.new(uri, :graph_uri => graph_uri.to_s)
53
+ end
39
54
 
40
55
  resource.hydrate!
41
56
  resource.new_record = false
@@ -183,7 +198,8 @@ module Tripod::Finders
183
198
  uris_and_graphs.each_pair do |u,g|
184
199
 
185
200
  # instantiate a new resource
186
- r = self.new(u,g)
201
+ g ||= {}
202
+ r = self.new(u, g)
187
203
 
188
204
  # make a graph of data for this resource's uri
189
205
  data_graph = RDF::Graph.new
@@ -27,19 +27,30 @@ module Tripod::Resource
27
27
  #
28
28
  # @example Instantiate a new Resource
29
29
  # Person.new('http://swirrl.com/ric.rdf#me')
30
+ # @example Instantiate a new Resource in a particular graph
31
+ # Person.new('http://swirrl.com/ric.rdf#me', :graph_uri => 'http://swirrl.com/graph/people')
32
+ # @example Instantiate a new Resource in a particular graph (DEPRECATED)
33
+ # Person.new('http://swirrl.com/ric.rdf#me', 'http://swirrl.com/graph/people')
30
34
  #
31
35
  # @param [ String, RDF::URI ] uri The uri of the resource.
32
- # @param [ String, RDF::URI ] graph_uri The graph_uri where this resource will be saved to. If ommitted, this resource cannot be persisted.
36
+ # @param [ Hash, String, RDF::URI ] opts An options hash (see above), or the graph_uri where this resource will be saved to. If graph URI is ommitted and can't be derived from the object's class, this resource cannot be persisted.
33
37
  #
34
38
  # @return [ Resource ] A new +Resource+
35
- def initialize(uri, graph_uri=nil)
39
+ def initialize(uri, opts={})
40
+ if opts.is_a?(String)
41
+ graph_uri = opts
42
+ else
43
+ graph_uri = opts.fetch(:graph_uri, nil)
44
+ ignore_graph = opts.fetch(:ignore_graph, false)
45
+ end
46
+
36
47
  raise Tripod::Errors::UriNotSet.new('uri missing') unless uri
37
48
  @uri = RDF::URI(uri.to_s)
38
49
  @repository = RDF::Repository.new
39
50
  @new_record = true
40
51
 
41
52
  run_callbacks :initialize do
42
- graph_uri ||= self.class.get_graph_uri
53
+ graph_uri ||= self.class.get_graph_uri unless ignore_graph
43
54
  @graph_uri = RDF::URI(graph_uri) if graph_uri
44
55
  self.rdf_type = self.class.get_rdf_type if respond_to?(:rdf_type=) && self.class.get_rdf_type
45
56
  end
@@ -1,3 +1,3 @@
1
1
  module Tripod
2
- VERSION = "0.7.26"
2
+ VERSION = "0.8"
3
3
  end
@@ -48,7 +48,31 @@ describe Tripod::Finders do
48
48
  end
49
49
 
50
50
  context 'with graph_uri supplied' do
51
+ let!(:another_person) do
52
+ p = Person.new('http://example.com/anotherperson', :graph_uri => 'http://example.com/graphx')
53
+ p.name = 'a.n.other'
54
+ p.save!
55
+ p
56
+ end
57
+
58
+ context 'when there are triples about the resource in that graph' do
59
+ it 'should use that graph to call new' do
60
+ Person.should_receive(:new).with(another_person.uri, :graph_uri => 'http://example.com/graphx').and_call_original
61
+ Person.find(another_person.uri, :graph_uri => 'http://example.com/graphx')
62
+ end
63
+
64
+ end
65
+
66
+ context 'when there are no triples about the resource in that graph' do
67
+ it 'should raise not found' do
68
+ expect {
69
+ Person.find(another_person.uri, :graph_uri => "http://example.com/graphy")
70
+ }.to raise_error(Tripod::Errors::ResourceNotFound)
71
+ end
72
+ end
73
+ end
51
74
 
75
+ context 'with graph_uri supplied (deprecated)' do
52
76
  let!(:another_person) do
53
77
  p = Person.new('http://example.com/anotherperson', 'http://example.com/graphx')
54
78
  p.name = 'a.n.other'
@@ -57,21 +81,18 @@ describe Tripod::Finders do
57
81
  end
58
82
 
59
83
  context 'when there are triples about the resource in that graph' do
60
-
61
84
  it 'should use that graph to call new' do
62
- Person.should_receive(:new).with(another_person.uri, 'http://example.com/graphx').and_call_original
63
- Person.find(another_person.uri, "http://example.com/graphx")
85
+ Person.should_receive(:new).with(another_person.uri, :graph_uri => 'http://example.com/graphx').and_call_original
86
+ Person.find(another_person.uri, 'http://example.com/graphx')
64
87
  end
65
88
 
66
89
  end
67
90
 
68
91
  context 'when there are no triples about the resource in that graph' do
69
- it 'should raise nto found' do
70
-
71
- lambda {
92
+ it 'should raise not found' do
93
+ expect {
72
94
  Person.find(another_person.uri, "http://example.com/graphy")
73
- }.should raise_error(Tripod::Errors::ResourceNotFound)
74
-
95
+ }.to raise_error(Tripod::Errors::ResourceNotFound)
75
96
  end
76
97
  end
77
98
  end
@@ -79,8 +100,40 @@ describe Tripod::Finders do
79
100
  context 'with no graph_uri supplied' do
80
101
  it 'should look up the graph to call new' do
81
102
  ric # trigger the lazy load
82
- Person.should_receive(:new).with(ric.uri, Person._GRAPH_URI).and_call_original
83
- Person.find(ric.uri, Person._GRAPH_URI)
103
+ Person.should_receive(:new).with(ric.uri, :graph_uri => Person.get_graph_uri).and_call_original
104
+ Person.find(ric.uri)
105
+ end
106
+ end
107
+
108
+ context "looking in any graph" do
109
+ context 'model has no default graph URI' do
110
+ let!(:resource) do
111
+ r = Resource.new('http://example.com/foo', :graph_uri => 'http://example/graph/foo')
112
+ r.label = 'Foo'
113
+ r.save!
114
+ r
115
+ end
116
+
117
+ it 'should find a resource regardless of which graph it is in' do
118
+ Resource.find(resource.uri, :ignore_graph => true).should_not be_nil
119
+ end
120
+ end
121
+
122
+ context 'model has a default graph URI' do
123
+ let!(:another_person) do
124
+ p = Person.new('http://example.com/anotherperson', :graph_uri => 'http://example.com/graphx')
125
+ p.name = 'a.n.other'
126
+ p.save!
127
+ p
128
+ end
129
+
130
+ it 'should override the default graph URI and find the resource regardless' do
131
+ Person.find(another_person.uri, :ignore_graph => true).should_not be_nil
132
+ end
133
+
134
+ it 'should return the resource without a graph URI' do
135
+ Person.find(another_person.uri, :ignore_graph => true).graph_uri.should be_nil
136
+ end
84
137
  end
85
138
  end
86
139
  end
@@ -34,12 +34,22 @@ describe Tripod::Resource do
34
34
 
35
35
  context 'with a URI and a graph URI' do
36
36
  let(:person) do
37
- Person.new('http://example.com/foobar', 'http://example.com/foobar/graph')
37
+ Person.new('http://example.com/foobar', :graph_uri => 'http://example.com/foobar/graph')
38
38
  end
39
39
 
40
40
  it "overrides the default graph URI with what's given" do
41
41
  person.graph_uri.should == RDF::URI.new('http://example.com/foobar/graph')
42
42
  end
43
43
  end
44
+
45
+ context 'with a URI, ignoring the graph URI' do
46
+ let(:person) do
47
+ Person.new('http://example.com/foobar', :ignore_graph => true)
48
+ end
49
+
50
+ it "should ignore the class-level graph URI" do
51
+ person.graph_uri.should be_nil
52
+ end
53
+ end
44
54
  end
45
55
  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.7.26
4
+ version: '0.8'
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-10-31 00:00:00.000000000 Z
14
+ date: 2013-11-08 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rest-client
18
- requirement: &70123156202840 !ruby/object:Gem::Requirement
18
+ requirement: &70287154647860 !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: *70123156202840
26
+ version_requirements: *70287154647860
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activemodel
29
- requirement: &70123156218380 !ruby/object:Gem::Requirement
29
+ requirement: &70287154647280 !ruby/object:Gem::Requirement
30
30
  none: false
31
31
  requirements:
32
32
  - - ~>
@@ -34,10 +34,10 @@ dependencies:
34
34
  version: '3.2'
35
35
  type: :runtime
36
36
  prerelease: false
37
- version_requirements: *70123156218380
37
+ version_requirements: *70287154647280
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: equivalent-xml
40
- requirement: &70123156217780 !ruby/object:Gem::Requirement
40
+ requirement: &70287154646820 !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: *70123156217780
48
+ version_requirements: *70287154646820
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: rdf
51
- requirement: &70123156216920 !ruby/object:Gem::Requirement
51
+ requirement: &70287154646240 !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: *70123156216920
59
+ version_requirements: *70287154646240
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: rdf-rdfxml
62
- requirement: &70123156216500 !ruby/object:Gem::Requirement
62
+ requirement: &70287154645760 !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: *70123156216500
70
+ version_requirements: *70287154645760
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: rdf-turtle
73
- requirement: &70123156216040 !ruby/object:Gem::Requirement
73
+ requirement: &70287154645080 !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: *70123156216040
81
+ version_requirements: *70287154645080
82
82
  - !ruby/object:Gem::Dependency
83
83
  name: rdf-json
84
- requirement: &70123156215580 !ruby/object:Gem::Requirement
84
+ requirement: &70287154644500 !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: *70123156215580
92
+ version_requirements: *70287154644500
93
93
  - !ruby/object:Gem::Dependency
94
94
  name: json-ld
95
- requirement: &70123156215040 !ruby/object:Gem::Requirement
95
+ requirement: &70287154643580 !ruby/object:Gem::Requirement
96
96
  none: false
97
97
  requirements:
98
98
  - - ~>
@@ -100,10 +100,10 @@ dependencies:
100
100
  version: 0.9.1
101
101
  type: :runtime
102
102
  prerelease: false
103
- version_requirements: *70123156215040
103
+ version_requirements: *70287154643580
104
104
  - !ruby/object:Gem::Dependency
105
105
  name: guid
106
- requirement: &70123156214380 !ruby/object:Gem::Requirement
106
+ requirement: &70287154643100 !ruby/object:Gem::Requirement
107
107
  none: false
108
108
  requirements:
109
109
  - - ! '>='
@@ -111,10 +111,10 @@ dependencies:
111
111
  version: '0'
112
112
  type: :runtime
113
113
  prerelease: false
114
- version_requirements: *70123156214380
114
+ version_requirements: *70287154643100
115
115
  - !ruby/object:Gem::Dependency
116
116
  name: dalli
117
- requirement: &70123156213660 !ruby/object:Gem::Requirement
117
+ requirement: &70287154642500 !ruby/object:Gem::Requirement
118
118
  none: false
119
119
  requirements:
120
120
  - - ~>
@@ -122,7 +122,7 @@ dependencies:
122
122
  version: '2.6'
123
123
  type: :runtime
124
124
  prerelease: false
125
- version_requirements: *70123156213660
125
+ version_requirements: *70287154642500
126
126
  description: RDF ruby ORM
127
127
  email:
128
128
  - ric@swirrl.com