tripod 0.7.8 → 0.7.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,11 +10,13 @@ module Tripod
10
10
  # +ResourceCollection+ is an +Enumerable+, Array-like object.
11
11
  #
12
12
  # @option options [ String ] return_graph Indicates whether to return the graph as one of the variables.
13
+ # @option options [ Boolean ] only_set_fields Indicates whether to set only the declared fields on the resources. (default false).
13
14
  def resources(opts={})
14
15
  Tripod::ResourceCollection.new(
15
- self.resource_class._resources_from_sparql(build_select_query(opts)),
16
+ self.resource_class._resources_from_sparql(build_select_query(opts), :only_set_fields => opts[:only_set_fields]),
16
17
  # pass in the criteria that was used to generate this collection, as well as whether the user specified return graph
17
18
  :return_graph => (opts.has_key?(:return_graph) ? opts[:return_graph] : true),
19
+ :only_set_fields => opts[:only_set_fields],
18
20
  :criteria => self
19
21
  )
20
22
  end
@@ -31,6 +33,7 @@ module Tripod
31
33
  # Execute the query and return the first result as a hydrated resource
32
34
  #
33
35
  # @option options [ String ] return_graph Indicates whether to return the graph as one of the variables.
36
+ # @option options [ Boolean ] only_set_fields Indicates whether to set only the declared fields on the resources. (default false).
34
37
  def first(opts={})
35
38
  sq = Tripod::SparqlQuery.new(build_select_query(opts))
36
39
  first_sparql = sq.as_first_query_str
@@ -59,6 +59,7 @@ module Tripod::Finders
59
59
  #
60
60
  # @option options [ String ] uri_variable The name of the uri variable in thh query, if not 'uri'
61
61
  # @option options [ String ] graph_variable The name of the uri variable in thh query, if not 'graph'
62
+ # @option options [ Boolean ] only_set_fields Indicates whether to set only the declared fields on the resources. (default false).
62
63
  #
63
64
  # @return [ Array ] An array of hydrated resources of this class's type.
64
65
  def find_by_sparql(sparql_query, opts={})
@@ -115,7 +116,8 @@ module Tripod::Finders
115
116
  # given a sparql select query, create and hydrate some resources
116
117
  #
117
118
  # @option options [ String ] uri_variable The name of the uri variable in the query, if not 'uri'
118
- # @option options [ String ] graph_variable The name of the uri variable in thh query, if not 'graph'
119
+ # @option options [ String ] graph_variable The name of the uri variable in the query, if not 'graph'
120
+ # @option options [ Boolean ] only_set_fields Indicates whether to set only the declared fields on the resources. (default false).
119
121
  def _resources_from_sparql(select_sparql, opts={})
120
122
  _create_and_hydrate_resources_from_sparql(select_sparql, opts)
121
123
  end
@@ -143,12 +145,13 @@ module Tripod::Finders
143
145
  #
144
146
  # @option options [ String ] uri_variable The name of the uri variable in the query, if not 'uri'
145
147
  # @option options [ String ] graph_variable The name of the uri variable in the query, if not 'graph'
148
+ # @option options [ Boolean ] only_set_fields Indicates whether to set only the declared fields on the resources. (default false).
146
149
  def _create_and_hydrate_resources_from_sparql(select_sparql, opts={})
147
150
  # TODO: Optimization?: if return_graph option is false, then don't do this next line?
148
151
  uris_and_graphs = _select_uris_and_graphs(select_sparql, :uri_variable => opts[:uri_variable], :graph_variable => opts[:graph_variable])
149
152
  ntriples_string = _raw_describe_select_results(select_sparql, :uri_variable => opts[:uri_variable]) # this defaults to ntriples
150
153
  graph = _rdf_graph_from_ntriples_string(ntriples_string, graph=nil)
151
- _resources_from_graph(graph, uris_and_graphs)
154
+ _resources_from_graph(graph, uris_and_graphs, :only_set_fields => opts[:only_set_fields])
152
155
  end
153
156
 
154
157
  # For a select query, generate a query which DESCRIBES all the results
@@ -169,10 +172,12 @@ module Tripod::Finders
169
172
  Tripod::SparqlClient::Query.query(query, accept_header)
170
173
  end
171
174
 
172
- given a graph of data, and a hash of uris=>graphs, create and hydrate some resources.
175
+ Given a graph of data, and a hash of uris=>graphs, create and hydrate some resources.
173
176
  # Note: if any of the graphs are not set in the hash,
174
177
  # those resources can still be constructed, but not persisted back to DB.
175
- def _resources_from_graph(graph, uris_and_graphs)
178
+ #
179
+ # @option options [ Boolean ] only_set_fields Indicates whether to set only the declared fields on the resources. (default false).
180
+ def _resources_from_graph(graph, uris_and_graphs, opts={})
176
181
  repo = add_data_to_repository(graph)
177
182
  resources = []
178
183
 
@@ -187,8 +192,11 @@ module Tripod::Finders
187
192
 
188
193
  # make a graph of data for this resource's uri
189
194
  data_graph = RDF::Graph.new
195
+
190
196
  repo.query( [RDF::URI.new(u), :predicate, :object] ) do |statement|
191
- data_graph << statement
197
+ if !opts[:only_set_fields] || self.fields.values.map{ |v| v.predicate.to_s }.include?(statement.predicate.to_s)
198
+ data_graph << statement
199
+ end
192
200
  end
193
201
 
194
202
  # use it to hydrate this resource
@@ -1,3 +1,3 @@
1
1
  module Tripod
2
- VERSION = "0.7.8"
2
+ VERSION = "0.7.9"
3
3
  end
@@ -12,6 +12,7 @@ describe Tripod::Criteria do
12
12
 
13
13
  let!(:john) do
14
14
  p = Person.new('http://example.com/id/john')
15
+ p.write_predicate('http://example.com/this-is-not-declared-as-a-field', 'blah')
15
16
  p.name = "John"
16
17
  p.save!
17
18
  p
@@ -96,8 +97,8 @@ describe Tripod::Criteria do
96
97
 
97
98
  context "with options passed" do
98
99
  it "should pass the options to build_select_query" do
99
- person_criteria.should_receive(:build_select_query).with(:return_graph => false).and_call_original
100
- person_criteria.resources(:return_graph => false)
100
+ person_criteria.should_receive(:build_select_query).with(:return_graph => false, :only_set_fields => true).and_call_original
101
+ person_criteria.resources(:return_graph => false, :only_set_fields => true)
101
102
  end
102
103
  end
103
104
 
@@ -131,6 +132,18 @@ describe Tripod::Criteria do
131
132
 
132
133
  end
133
134
 
135
+ context "with the only_set_fields option set to true" do
136
+ it "should only populate predicates for fields that have been declared at class level" do
137
+ person_criteria.resources(:only_set_fields => true).first.read_predicate('http://example.com/this-is-not-declared-as-a-field').should be_empty
138
+ end
139
+ end
140
+
141
+ context "without setting the only_set_fields option (i.e. false)" do
142
+ it "should only populate all preciates that have been declared at class level" do
143
+ person_criteria.resources.first.read_predicate('http://example.com/this-is-not-declared-as-a-field').should_not be_empty
144
+ end
145
+ end
146
+
134
147
  end
135
148
 
136
149
  describe "#first" 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.7.8
4
+ version: 0.7.9
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-04-24 00:00:00.000000000 Z
14
+ date: 2013-05-07 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rest-client
18
- requirement: &70109553817240 !ruby/object:Gem::Requirement
18
+ requirement: &70100686885260 !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: *70109553817240
26
+ version_requirements: *70100686885260
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activemodel
29
- requirement: &70109553816700 !ruby/object:Gem::Requirement
29
+ requirement: &70100686884720 !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: *70109553816700
37
+ version_requirements: *70100686884720
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: equivalent-xml
40
- requirement: &70109553816280 !ruby/object:Gem::Requirement
40
+ requirement: &70100686884300 !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: *70109553816280
48
+ version_requirements: *70100686884300
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: rdf
51
- requirement: &70109553815740 !ruby/object:Gem::Requirement
51
+ requirement: &70100686883760 !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: *70109553815740
59
+ version_requirements: *70100686883760
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: rdf-rdfxml
62
- requirement: &70109553815320 !ruby/object:Gem::Requirement
62
+ requirement: &70100686883340 !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: *70109553815320
70
+ version_requirements: *70100686883340
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: rdf-n3
73
- requirement: &70109553814860 !ruby/object:Gem::Requirement
73
+ requirement: &70100686882880 !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: *70109553814860
81
+ version_requirements: *70100686882880
82
82
  - !ruby/object:Gem::Dependency
83
83
  name: rdf-json
84
- requirement: &70109553814440 !ruby/object:Gem::Requirement
84
+ requirement: &70100686882460 !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: *70109553814440
92
+ version_requirements: *70100686882460
93
93
  - !ruby/object:Gem::Dependency
94
94
  name: json-ld
95
- requirement: &70109553814020 !ruby/object:Gem::Requirement
95
+ requirement: &70100686882040 !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: *70109553814020
103
+ version_requirements: *70100686882040
104
104
  - !ruby/object:Gem::Dependency
105
105
  name: guid
106
- requirement: &70109553813600 !ruby/object:Gem::Requirement
106
+ requirement: &70100686881620 !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: *70109553813600
114
+ version_requirements: *70100686881620
115
115
  - !ruby/object:Gem::Dependency
116
116
  name: dalli
117
- requirement: &70109553829460 !ruby/object:Gem::Requirement
117
+ requirement: &70100686881120 !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: *70109553829460
125
+ version_requirements: *70100686881120
126
126
  description: RDF ruby ORM
127
127
  email:
128
128
  - ric@swirrl.com