acts_as_rdf 0.1.2 → 0.1.3
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/VERSION +1 -1
- data/lib/acts_as_rdf.rb +17 -30
- data/test/test_to_rdf.rb +9 -23
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/lib/acts_as_rdf.rb
CHANGED
@@ -39,36 +39,27 @@ module ActsAsRdf
|
|
39
39
|
# All the methods available to a record that has had <tt>acts_as_rdf</tt> specified.
|
40
40
|
module InstanceMethods
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
def to_ttl
|
48
|
-
graph = to_graph()
|
49
|
-
return to_output(graph, :turtle)
|
50
|
-
end
|
51
|
-
|
52
|
-
def to_ntriples
|
53
|
-
graph = to_graph()
|
54
|
-
return to_output(graph, :ntriples)
|
42
|
+
# current tested formats are rdfxml, turtle, ntriples
|
43
|
+
def to_rdf(host = "http://localhost", format = :rdfxml)
|
44
|
+
graph = to_graph(host)
|
45
|
+
return to_output(graph, host, format)
|
55
46
|
end
|
56
47
|
|
57
48
|
protected
|
58
49
|
|
59
|
-
def to_graph(graph = RDF::Graph.new)
|
50
|
+
def to_graph(host, graph = RDF::Graph.new)
|
60
51
|
|
61
52
|
if 'Array'.eql?(self.class.name) then
|
62
53
|
self.each do |element|
|
63
|
-
graph = self.to_graph(graph)
|
54
|
+
graph = self.to_graph(host, graph)
|
64
55
|
end
|
65
56
|
else
|
66
|
-
graph << [to_uri(self), RDF.type, to_uri(self.class)]
|
57
|
+
graph << [to_uri(self, host), RDF.type, to_uri(self.class, host)]
|
67
58
|
|
68
59
|
self.attributes.each do |attr, value|
|
69
|
-
graph << [to_uri(self), to_uri(attr), RDF::Literal.new(value)]
|
60
|
+
graph << [to_uri(self, host), to_uri(attr, host), RDF::Literal.new(value)]
|
70
61
|
if ('name'.eql?(attr)) then
|
71
|
-
graph << [to_uri(self), RDF::RDFS.label, RDF::Literal.new(value)]
|
62
|
+
graph << [to_uri(self, host), RDF::RDFS.label, RDF::Literal.new(value)]
|
72
63
|
end
|
73
64
|
end
|
74
65
|
|
@@ -80,10 +71,10 @@ module ActsAsRdf
|
|
80
71
|
if association_objects != nil then
|
81
72
|
if 'Array'.eql?(association_objects.class.name) then
|
82
73
|
association_objects.each do |obj|
|
83
|
-
graph << [to_uri(self), to_uri(soc.class_name), to_uri(obj)]
|
74
|
+
graph << [to_uri(self, host), to_uri(soc.class_name, host), to_uri(obj, host)]
|
84
75
|
end
|
85
76
|
else
|
86
|
-
graph << [to_uri(self), to_uri(soc.class_name), to_uri(association_objects)]
|
77
|
+
graph << [to_uri(self, host), to_uri(soc.class_name, host), to_uri(association_objects, host)]
|
87
78
|
end
|
88
79
|
end
|
89
80
|
end
|
@@ -94,8 +85,8 @@ module ActsAsRdf
|
|
94
85
|
return graph
|
95
86
|
end
|
96
87
|
|
97
|
-
def to_output(graph, format)
|
98
|
-
base_uri = RDF::URI.new(
|
88
|
+
def to_output(graph, host, format)
|
89
|
+
base_uri = RDF::URI.new(host)
|
99
90
|
|
100
91
|
return RDF::Writer.for(format).buffer(:base_uri => base_uri, :default_namespace => base_uri, :prefixes => get_prefixes()) { |writer|
|
101
92
|
graph.each_statement do |statement|
|
@@ -104,19 +95,15 @@ module ActsAsRdf
|
|
104
95
|
}
|
105
96
|
end
|
106
97
|
|
107
|
-
def to_uri(thing)
|
98
|
+
def to_uri(thing, host)
|
108
99
|
if (thing.class.name != nil)
|
109
100
|
if ('String'.eql?(thing.class.name) || 'Class'.eql?(thing.class.name)) then
|
110
|
-
return RDF::URI.new("#{
|
101
|
+
return RDF::URI.new("#{host}/#{thing.to_s}")
|
111
102
|
end
|
112
|
-
return RDF::URI.new("#{
|
103
|
+
return RDF::URI.new("#{host}/#{thing.class.name.tableize}/#{thing.id}")
|
113
104
|
end
|
114
105
|
|
115
|
-
return RDF::URI.new("#{
|
116
|
-
end
|
117
|
-
|
118
|
-
def get_host
|
119
|
-
"http://#{self.class.respond_to?('request') ? "#{request.env['HTTP_HOST']}" : "localhost"}"
|
106
|
+
return RDF::URI.new("#{host}##{thing.to_s}")
|
120
107
|
end
|
121
108
|
|
122
109
|
end
|
data/test/test_to_rdf.rb
CHANGED
@@ -13,29 +13,15 @@ class RdfTest < Test::Unit::TestCase
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_to_rdf
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
Mixin.find(:all).each do |o|
|
26
|
-
puts o.to_ttl
|
27
|
-
end
|
28
|
-
MixinRelation.find(:all).each do |o|
|
29
|
-
puts o.to_ttl
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_to_ntriples
|
34
|
-
Mixin.find(:all).each do |o|
|
35
|
-
puts o.to_ntriples
|
36
|
-
end
|
37
|
-
MixinRelation.find(:all).each do |o|
|
38
|
-
puts o.to_ntriples
|
16
|
+
host = 'http://localhost'
|
17
|
+
formats = [:rdfxml, :turtle, :ntriples]
|
18
|
+
formats.each do |format|
|
19
|
+
Mixin.find(:all).each do |o|
|
20
|
+
puts o.to_rdf(host, format)
|
21
|
+
end
|
22
|
+
MixinRelation.find(:all).each do |o|
|
23
|
+
puts o.to_rdf(host, format)
|
24
|
+
end
|
39
25
|
end
|
40
26
|
end
|
41
27
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_rdf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 3
|
10
|
+
version: 0.1.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Andrew Sodt
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-10-
|
18
|
+
date: 2010-10-26 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|