tripod 0.6.1 → 0.7.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/lib/tripod.rb CHANGED
@@ -84,6 +84,7 @@ require "tripod/resource_collection"
84
84
 
85
85
  require "tripod/predicates"
86
86
  require "tripod/attributes"
87
+ require "tripod/callbacks"
87
88
  require "tripod/errors"
88
89
  require "tripod/repository"
89
90
  require "tripod/fields"
@@ -24,6 +24,7 @@ module Tripod::Attributes
24
24
  raise Tripod::Errors::FieldNotPresent.new unless field
25
25
 
26
26
  attr_values = read_predicate(field.predicate)
27
+ attr_values.map! { |v| read_value_for_field(v, field) }
27
28
 
28
29
  # If the field is multivalued, return an array of the results
29
30
  # If it's not multivalued, return the first (should be only) result.
@@ -56,16 +57,38 @@ module Tripod::Attributes
56
57
  if field.multivalued
57
58
  new_val = []
58
59
  value.each do |v|
59
- new_val << self.class.new_value_for_field(v, field)
60
+ new_val << write_value_for_field(v, field)
60
61
  end
61
62
  else
62
- new_val = self.class.new_value_for_field(value.first, field)
63
+ new_val = write_value_for_field(value.first, field)
63
64
  end
64
65
  else
65
- new_val = self.class.new_value_for_field(value, field)
66
+ new_val = write_value_for_field(value, field)
66
67
  end
67
68
 
68
69
  write_predicate(field.predicate, new_val)
69
70
  end
70
71
  alias :[]= :write_attribute
72
+
73
+ private
74
+
75
+ def read_value_for_field(value, field)
76
+ if field.is_uri?
77
+ value
78
+ else
79
+ value.object
80
+ end
81
+ end
82
+
83
+ def write_value_for_field(value, field)
84
+ return unless value
85
+
86
+ if field.is_uri?
87
+ RDF::URI.new(value.to_s)
88
+ elsif field.datatype
89
+ RDF::Literal.new(value, :datatype => field.datatype)
90
+ else
91
+ value
92
+ end
93
+ end
71
94
  end
@@ -0,0 +1,8 @@
1
+ module Tripod::Callbacks
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ extend ActiveModel::Callbacks
6
+ define_model_callbacks :initialize, :save, :destroy
7
+ end
8
+ end
@@ -15,6 +15,7 @@ module Tripod::Components
15
15
 
16
16
  include Tripod::Predicates
17
17
  include Tripod::Attributes
18
+ include Tripod::Callbacks
18
19
  include Tripod::Persistence
19
20
  include Tripod::Fields
20
21
  include Tripod::Finders
@@ -8,23 +8,28 @@ module Tripod
8
8
 
9
9
  # Execute the query and return a +ResourceCollection+ of all hydrated resources
10
10
  # +ResourceCollection+ is an +Enumerable+, Array-like object.
11
- def resources
11
+ # options:
12
+ # :return_graph (default true) # indicates whether to return the graph as one of the variables.
13
+ def resources(opts={})
12
14
  Tripod::ResourceCollection.new(
13
- resources_from_sparql(build_select_query)
15
+ resources_from_sparql(build_select_query(opts))
14
16
  )
15
17
  end
16
18
 
17
19
  # Execute the query and return the first result as a hydrated resource
18
-
19
- def first
20
- sq = Tripod::SparqlQuery.new(build_select_query)
20
+ # options:
21
+ # :return_graph (default true) # indicates whether to return the graph as one of the variables.
22
+ def first(opts={})
23
+ sq = Tripod::SparqlQuery.new(build_select_query(opts))
21
24
  first_sparql = sq.as_first_query_str
22
25
  resources_from_sparql(first_sparql).first
23
26
  end
24
27
 
25
28
  # Return how many records the current criteria would return
26
- def count
27
- sq = Tripod::SparqlQuery.new(build_select_query)
29
+ # options:
30
+ # :return_graph (default true) # indicates whether to return the graph as one of the variables.
31
+ def count(opts={})
32
+ sq = Tripod::SparqlQuery.new(build_select_query(opts))
28
33
  count_sparql = sq.as_count_query_str
29
34
  result = Tripod::SparqlClient::Query.select(count_sparql)
30
35
  result[0]["c"]["value"].to_i
@@ -41,17 +46,30 @@ module Tripod
41
46
  self.resource_class._create_and_hydrate_resources(uris_and_graphs)
42
47
  end
43
48
 
44
- def build_select_query
49
+ # options:
50
+ # :return_graph (default true) # indicates whether to return the graph as one of the variables.
51
+ def build_select_query(opts={})
52
+
53
+ return_graph = opts.has_key?(:return_graph) ? opts[:return_graph] : true
54
+
55
+ select_query = "SELECT DISTINCT ?uri "
45
56
 
46
- if graph_uri
47
- select_query = "SELECT DISTINCT ?uri (<#{graph_uri}> as ?graph) WHERE { GRAPH <#{graph_uri}> "
57
+ if return_graph
58
+ # if we are returing the graph, select it as a variable, and include either the <graph> or ?graph in the where clause
59
+ if graph_uri
60
+ select_query += "(<#{graph_uri}> as ?graph) WHERE { GRAPH <#{graph_uri}> { "
61
+ else
62
+ select_query += "?graph WHERE { GRAPH ?graph { "
63
+ end
48
64
  else
49
- select_query = "SELECT DISTINCT ?uri ?graph WHERE { GRAPH ?graph "
65
+ select_query += "WHERE { "
66
+ # if we're not returning the graph, only restrict by the <graph> if there's one set at class level
67
+ select_query += "GRAPH <#{graph_uri}> { " if graph_uri
50
68
  end
51
69
 
52
- select_query += "{ "
53
70
  select_query += self.where_clauses.join(" . ")
54
- select_query += " } } "
71
+ select_query += " } "
72
+ select_query += "} " if return_graph || graph_uri # close the graph clause
55
73
  select_query += self.extra_clauses.join(" ")
56
74
 
57
75
  select_query += [order_clause, limit_clause, offset_clause].join(" ")
data/lib/tripod/fields.rb CHANGED
@@ -32,18 +32,6 @@ module Tripod::Fields
32
32
  add_field(name, predicate, options)
33
33
  end
34
34
 
35
-
36
- def new_value_for_field(value, field)
37
- return unless value
38
- if field.is_uri?
39
- RDF::URI.new(value.to_s)
40
- elsif field.datatype
41
- RDF::Literal.new(value, :datatype => field.datatype)
42
- else
43
- value
44
- end
45
- end
46
-
47
35
  protected
48
36
 
49
37
  # Define a field attribute for the +Resource+.
@@ -154,7 +154,8 @@ module Tripod::Finders
154
154
  select_results.each do |r|
155
155
  uri_variable = opts[:uri_variable] || 'uri'
156
156
  graph_variable = opts[:graph_variable] || 'graph'
157
- uris_and_graphs[ r[uri_variable]["value"] ] = r[graph_variable]["value"]
157
+ graph_value = r[graph_variable]["value"] if r[graph_variable]
158
+ uris_and_graphs[ r[uri_variable]["value"] ] = graph_value || nil
158
159
  end
159
160
 
160
161
  uris_and_graphs
@@ -4,11 +4,6 @@
4
4
  module Tripod::Persistence
5
5
  extend ActiveSupport::Concern
6
6
 
7
- included do
8
- extend ActiveModel::Callbacks
9
- define_model_callbacks :save, :destroy
10
- end
11
-
12
7
  class Tripod::Persistence::Transaction
13
8
 
14
9
  def initialize
@@ -33,12 +33,14 @@ module Tripod::Resource
33
33
  def initialize(uri, graph_uri=nil)
34
34
  raise Tripod::Errors::UriNotSet.new('uri missing') unless uri
35
35
  @uri = RDF::URI(uri.to_s)
36
-
37
- graph_uri ||= self.class._GRAPH_URI if self.class._GRAPH_URI
38
- @graph_uri = RDF::URI(graph_uri) if graph_uri
39
36
  @repository = RDF::Repository.new
40
37
  @new_record = true
41
- self.rdf_type = self.class._RDF_TYPE if respond_to?(:rdf_type=) && self.class._RDF_TYPE
38
+
39
+ run_callbacks :initialize do
40
+ graph_uri ||= self.class._GRAPH_URI if self.class._GRAPH_URI
41
+ @graph_uri = RDF::URI(graph_uri) if graph_uri
42
+ self.rdf_type = self.class._RDF_TYPE if respond_to?(:rdf_type=) && self.class._RDF_TYPE
43
+ end
42
44
  end
43
45
 
44
46
  # default comparison is via the uri
@@ -96,7 +98,7 @@ module Tripod::Resource
96
98
  # and sets a class level _RDF_TYPE variable with the rdf_type passed in.
97
99
  def rdf_type(new_rdf_type)
98
100
  self._RDF_TYPE = RDF::URI.new(new_rdf_type.to_s)
99
- field :rdf_type, RDF.type, :multivalued => true # things can have more than 1 type and often do
101
+ field :rdf_type, RDF.type, :multivalued => true, :is_uri => true # things can have more than 1 type and often do
100
102
  end
101
103
 
102
104
  def graph_uri(new_graph_uri)
@@ -30,7 +30,7 @@ module Tripod::SparqlClient
30
30
  end
31
31
 
32
32
  response = Tripod::Streaming.get_data(request_url, streaming_opts)
33
- Rails.logger.debug "TRIPOD: response: #{response}" if defined?(Rails)
33
+ # Rails.logger.debug "TRIPOD: response: #{response}" if defined?(Rails)
34
34
  response
35
35
  }
36
36
 
@@ -1,3 +1,3 @@
1
1
  module Tripod
2
- VERSION = "0.6.1"
2
+ VERSION = "0.7.1"
3
3
  end
@@ -7,7 +7,7 @@ class Person
7
7
 
8
8
  field :name, 'http://example.com/name'
9
9
  field :father, 'http://example.com/father', :is_uri => true
10
- field :knows, 'http://example.com/knows', :multivalued => true
10
+ field :knows, 'http://example.com/knows', :multivalued => true, :is_uri => true
11
11
  field :aliases, 'http://exmample.com/alias', :multivalued => true
12
12
  field :age, 'http://example.com/age', :datatype => RDF::XSD.integer
13
13
  field :important_dates, 'http://example.com/importantdates', :datatype => RDF::XSD.date, :multivalued => true
@@ -17,8 +17,8 @@ describe Tripod::Attributes do
17
17
  end
18
18
 
19
19
  context "for a literal" do
20
- it "should return an RDF::Literal" do
21
- person[:name].class.should == RDF::Literal
20
+ it "should return the native type" do
21
+ person[:name].class.should == String
22
22
  end
23
23
  it "should read the given attribute" do
24
24
  person[:name].should == 'Barry'
@@ -48,32 +48,23 @@ describe Tripod::Attributes do
48
48
  context "where field is given and single-valued" do
49
49
  let(:field) { Person.send(:field_for, :hat_type, 'http://example.com/hat', {}) }
50
50
  before do
51
- person.stub(:read_predicate).with('http://example.com/hat').and_return(['fez'])
51
+ person.stub(:read_predicate).with('http://example.com/hat').and_return([RDF::Literal.new('fez')])
52
52
  end
53
53
 
54
54
  it "should use the predicate name from the given field" do
55
- person.should_receive(:read_predicate).with('http://example.com/hat').and_return(['fez'])
55
+ person.should_receive(:read_predicate).with('http://example.com/hat').and_return([RDF::Literal.new('fez')])
56
56
  person.read_attribute(:hat_type, field)
57
57
  end
58
58
 
59
59
  it "should return a single value" do
60
60
  person.read_attribute(:hat_type, field).should == 'fez'
61
61
  end
62
-
63
- context "and the value is a URI" do
64
- let(:field) { Person.send(:field_for, :hat_type, 'http://example.com/hat', {is_uri: true}) }
65
- before do
66
- person.stub(:read_predicate).with('http://example.com/hat').and_return(['fez'])
67
- end
68
-
69
-
70
- end
71
62
  end
72
63
 
73
64
  context "where field is given and is multi-valued" do
74
65
  let(:field) { Person.send(:field_for, :hat_types, 'http://example.com/hat', {multivalued: true}) }
75
66
  before do
76
- person.stub(:read_predicate).with('http://example.com/hat').and_return(['fez', 'bowler'])
67
+ person.stub(:read_predicate).with('http://example.com/hat').and_return([RDF::Literal.new('fez'), RDF::Literal.new('bowler')])
77
68
  end
78
69
 
79
70
  it "should return an array of values" do
@@ -31,6 +31,12 @@ describe Tripod::Criteria do
31
31
  person_criteria.send(:build_select_query).should == "SELECT DISTINCT ?uri (<http://example.com/graph> as ?graph) WHERE { GRAPH <http://example.com/graph> { ?uri a <http://example.com/person> . ?uri ?p ?o } }"
32
32
  end
33
33
 
34
+ context "with include_graph option set to false" do
35
+ it "should not select graphs, but restrict to graph" do
36
+ person_criteria.send(:build_select_query, :return_graph => false).should == "SELECT DISTINCT ?uri WHERE { GRAPH <http://example.com/graph> { ?uri a <http://example.com/person> . ?uri ?p ?o } }"
37
+ end
38
+ end
39
+
34
40
  context "and extra restrictions" do
35
41
  before { person_criteria.where("[pattern]") }
36
42
 
@@ -53,6 +59,12 @@ describe Tripod::Criteria do
53
59
  resource_criteria.send(:build_select_query).should == "SELECT DISTINCT ?uri ?graph WHERE { GRAPH ?graph { ?uri ?p ?o } }"
54
60
  end
55
61
 
62
+ context "with include_graph option set to false" do
63
+ it "should not select graphs or restrict to graph" do
64
+ resource_criteria.send(:build_select_query, :return_graph => false).should == "SELECT DISTINCT ?uri WHERE { ?uri ?p ?o }"
65
+ end
66
+ end
67
+
56
68
  context "and extra restrictions" do
57
69
  before { resource_criteria.where("[pattern]") }
58
70
 
@@ -82,7 +94,12 @@ describe Tripod::Criteria do
82
94
 
83
95
  describe "#resources" do
84
96
 
85
-
97
+ context "with options passed" do
98
+ 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)
101
+ end
102
+ end
86
103
 
87
104
  context "with no extra restrictions" do
88
105
  it "should return a set of hydrated objects for the type" do
@@ -91,18 +108,40 @@ describe Tripod::Criteria do
91
108
  end
92
109
 
93
110
  context "with extra restrictions" do
94
-
95
111
  before { person_criteria.where("?uri <http://example.com/name> 'John'") }
96
112
 
97
113
  it "should return a set of hydrated objects for the type and restrictions" do
98
- person_criteria.resources.to_a.should == [john]
114
+ person_criteria.resources.to_a.should == [john]
115
+ end
116
+ end
117
+
118
+ context "with return_graph option set to false" do
119
+
120
+ context "where the class has a graph_uri set" do
121
+ it "should set the graph_uri on the hydrated objects" do
122
+ person_criteria.resources(:return_graph => false).first.graph_uri.should_not be_nil
123
+ end
124
+ end
125
+
126
+ context "where the class does not have a graph_uri set" do
127
+ it "should not set the graph_uri on the hydrated objects" do
128
+ resource_criteria.resources(:return_graph => false).first.graph_uri.should be_nil
129
+ end
99
130
  end
131
+
100
132
  end
101
133
 
102
134
  end
103
135
 
104
136
  describe "#first" do
105
137
 
138
+ context "with options passed" do
139
+ it "should pass the options to build_select_query" do
140
+ person_criteria.should_receive(:build_select_query).with(:return_graph => false).and_call_original
141
+ person_criteria.first(:return_graph => false)
142
+ end
143
+ end
144
+
106
145
  it "should return the first resource for the criteria" do
107
146
  person_criteria.first.should == john
108
147
  end
@@ -112,10 +151,33 @@ describe Tripod::Criteria do
112
151
  Tripod::SparqlClient::Query.should_receive(:select).with(sparql).and_call_original
113
152
  person_criteria.first
114
153
  end
154
+
155
+ context "with return_graph option set to false" do
156
+
157
+ context "where the class has a graph_uri set" do
158
+ it "should set the graph_uri on the hydrated object" do
159
+ person_criteria.first(:return_graph => false).graph_uri.should_not be_nil
160
+ end
161
+ end
162
+
163
+ context "where the class does not have a graph_uri set" do
164
+ it "should not set the graph_uri on the hydrated object" do
165
+ resource_criteria.first(:return_graph => false).graph_uri.should be_nil
166
+ end
167
+ end
168
+
169
+ end
115
170
  end
116
171
 
117
172
  describe "#count" do
118
173
 
174
+ context "with options passed" do
175
+ it "should pass the options to build_select_query" do
176
+ person_criteria.should_receive(:build_select_query).with(:return_graph => false).and_call_original
177
+ person_criteria.count(:return_graph => false)
178
+ end
179
+ end
180
+
119
181
  it "should return a set of hydrated objects for the criteria" do
120
182
  person_criteria.count.should == 2
121
183
  person_criteria.where("?uri <http://example.com/name> 'John'").count.should ==1
@@ -23,7 +23,7 @@ describe Tripod::Resource do
23
23
 
24
24
  context "with rdf_type specified at class level" do
25
25
  it "sets the rdf type from the class" do
26
- person.rdf_type.should == ['http://example.com/person']
26
+ person.rdf_type.should == [RDF::URI.new('http://example.com/person')]
27
27
  end
28
28
  end
29
29
 
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.6.1
4
+ version: 0.7.1
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-03-27 00:00:00.000000000 Z
14
+ date: 2013-03-28 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rest-client
18
- requirement: &70363921370680 !ruby/object:Gem::Requirement
18
+ requirement: &70200922668480 !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: *70363921370680
26
+ version_requirements: *70200922668480
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activemodel
29
- requirement: &70363921385640 !ruby/object:Gem::Requirement
29
+ requirement: &70200922664960 !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: *70363921385640
37
+ version_requirements: *70200922664960
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: equivalent-xml
40
- requirement: &70363921384140 !ruby/object:Gem::Requirement
40
+ requirement: &70200922663180 !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: *70363921384140
48
+ version_requirements: *70200922663180
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: rdf
51
- requirement: &70363921383140 !ruby/object:Gem::Requirement
51
+ requirement: &70200922662000 !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: *70363921383140
59
+ version_requirements: *70200922662000
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: rdf-rdfxml
62
- requirement: &70363921381980 !ruby/object:Gem::Requirement
62
+ requirement: &70200922660520 !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: *70363921381980
70
+ version_requirements: *70200922660520
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: rdf-n3
73
- requirement: &70363921380580 !ruby/object:Gem::Requirement
73
+ requirement: &70200922674900 !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: *70363921380580
81
+ version_requirements: *70200922674900
82
82
  - !ruby/object:Gem::Dependency
83
83
  name: rdf-json
84
- requirement: &70363921379380 !ruby/object:Gem::Requirement
84
+ requirement: &70200922673580 !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: *70363921379380
92
+ version_requirements: *70200922673580
93
93
  - !ruby/object:Gem::Dependency
94
94
  name: json-ld
95
- requirement: &70363921378320 !ruby/object:Gem::Requirement
95
+ requirement: &70200922670740 !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: *70363921378320
103
+ version_requirements: *70200922670740
104
104
  - !ruby/object:Gem::Dependency
105
105
  name: guid
106
- requirement: &70363921393360 !ruby/object:Gem::Requirement
106
+ requirement: &70200922693900 !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: *70363921393360
114
+ version_requirements: *70200922693900
115
115
  - !ruby/object:Gem::Dependency
116
116
  name: dalli
117
- requirement: &70363921391420 !ruby/object:Gem::Requirement
117
+ requirement: &70200922692800 !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: *70363921391420
125
+ version_requirements: *70200922692800
126
126
  description: RDF ruby ORM
127
127
  email:
128
128
  - ric@swirrl.com
@@ -140,6 +140,7 @@ files:
140
140
  - lib/tripod.rb
141
141
  - lib/tripod/attributes.rb
142
142
  - lib/tripod/cache_stores/memcached_cache_store.rb
143
+ - lib/tripod/callbacks.rb
143
144
  - lib/tripod/components.rb
144
145
  - lib/tripod/criteria.rb
145
146
  - lib/tripod/criteria/execution.rb