iq_rdf 0.1.16 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 985cc8b02831cc489803bb8ab8bf17ad1ea0bec3
4
- data.tar.gz: 61fbb6c02a8a8a8a13d1151ee2bbef74a2e8e784
2
+ SHA256:
3
+ metadata.gz: 21e26c36ccb3dfca97f5c1224df1b95dec59c9672d837d2c3f4259a02acbbb36
4
+ data.tar.gz: 7f235328b11b0b864f3a283a715f86d498e6babf4d2a7e28457146b296fac78b
5
5
  SHA512:
6
- metadata.gz: '096d438f95afe8be17afdc28b9517d3cd3ea82fb2766c9667c597c8c1f55c70c83caa63f8391da86977d835eac5357840f92c2da57148ef9b29a44e901a07349'
7
- data.tar.gz: '034199d8c6b497b5b1944e63798cfa49f38de8e2abb9d4394f01498fb972851bd47cadf0111317e463675a8fae3dc968a1810489711a57a03197e0401523b2c3'
6
+ metadata.gz: ae3b1524338f266611e3d8976a20ba7ca5b6e8646cbc31c1ced591543714547f89cdeddcab4899ab92eff0109d13d46141394146b16e6b148a03cedf6b2ac432
7
+ data.tar.gz: 24f87a96f0995040ed7b8473b7102bd1e6bf490462384b196db0bbc85d5904ce63ecab75c4556bd9c7a25118957e657ccf5919796ee8bdb2050959625b3e207c
data/README.md CHANGED
@@ -1,4 +1,7 @@
1
- # IqRdf - RDF Renderering for Ruby and Rails [![build status](https://secure.travis-ci.org/innoq/iq_rdf.png)](http://travis-ci.org/innoq/iq_rdf)
1
+ # IqRdf - RDF Renderering for Ruby and Rails
2
+
3
+ ![CI](https://github.com/innoq/iq_rdf/workflows/CI/badge.svg)
4
+
2
5
  IqRdf is a RDF renderer for Ruby and Rails. You can use it in any Ruby
3
6
  environment to render Trurtle-, N-Triple- (not implemented yet) or XML-RDF.
4
7
 
@@ -26,14 +26,24 @@ module IqRdf
26
26
 
27
27
  end
28
28
 
29
- def to_s(parent_lang = nil)
30
- "[\n#{(nodes.map{|pred| "#{pred} #{pred.nodes.map{|o| o.to_s(pred.lang || parent_lang)}.join(", ")}"}.join(";\n")).gsub(/^/, " ")}\n]"
29
+ def to_s(options = {})
30
+ base_indent = options[:indent] || ""
31
+ predicate_indent = base_indent + "".ljust(4)
32
+
33
+ predicates = nodes.map { |p| format_predicate(p, indent: predicate_indent, lang: options[:lang]) }
34
+ .join(";\n")
35
+
36
+ "[\n#{predicates}\n#{base_indent} ]"
31
37
  end
32
38
 
33
39
  def build_predicate(*args, &block)
34
40
  IqRdf::PredicateNamespace.new(self, IqRdf::Default).build_predicate(*args, &block)
35
41
  end
36
42
 
43
+ def build_predicate_with_ns(namespace, *args, &block)
44
+ IqRdf::PredicateNamespace.new(self, namespace).build_predicate(*args, &block)
45
+ end
46
+
37
47
  def build_xml(xml, &block)
38
48
  block.call({}, lambda {
39
49
  attrs = {}
@@ -46,5 +56,15 @@ module IqRdf
46
56
  })
47
57
  end
48
58
 
59
+ private
60
+
61
+ def format_predicate(predicate, options = {})
62
+ subjects = predicate.nodes
63
+ .map { |object| object.to_s(indent: options[:indent], lang: predicate.lang || options[:lang]) }
64
+ .join(", ")
65
+
66
+ "#{options[:indent]} #{predicate} #{subjects}"
67
+ end
68
+
49
69
  end
50
70
  end
@@ -25,8 +25,8 @@ module IqRdf
25
25
  end
26
26
  end
27
27
 
28
- def to_s(lang = nil)
29
- "(#{@elements.map{|e| e.to_s(lang)}.join(" ")})"
28
+ def to_s(options = {})
29
+ "(#{@elements.map{|e| e.to_s(options)}.join(" ")})"
30
30
  end
31
31
 
32
32
  def build_xml(xml, elements = nil, &block)
@@ -25,6 +25,7 @@ module IqRdf
25
25
  @document_language = options[:lang]
26
26
 
27
27
  @nodes = []
28
+ @config = {}
28
29
  end
29
30
 
30
31
  def namespaces(namespaces)
@@ -39,6 +40,10 @@ module IqRdf
39
40
  self
40
41
  end
41
42
 
43
+ def config(config = {})
44
+ @config = config
45
+ end
46
+
42
47
  def <<(node)
43
48
  return if node.nil?
44
49
  raise ArgumentError, "Node must be an IqRdf::Uri and a Subject!" unless node.is_a?(IqRdf::Uri) and node.is_subject?
@@ -143,17 +148,29 @@ module IqRdf
143
148
  s << "@prefix #{namespace.turtle_token}: <#{namespace.uri_prefix}>.\n"
144
149
  end
145
150
  s << "\n"
146
- @nodes.each do |node|
147
- pref = "#{node.to_s(@document_language)}"
148
- if node.rdf_type
149
- s << "#{pref} a #{node.rdf_type}"
150
- pref = ";\n" + "".ljust(node.to_s(@document_language).length)
151
+ @nodes.each do |subject|
152
+ pref = subject.to_s(@document_language)
153
+ indent = "".ljust(pref.length)
154
+
155
+ # Render subject, if it is defined as a RDF-type.
156
+ if subject.rdf_type
157
+ s << "#{pref} a #{subject.rdf_type}"
158
+ pref = ";\n" + indent
151
159
  end
152
- node.nodes.each do |predicate|
153
- s << "#{pref} #{predicate.to_s} #{predicate.nodes.map{|o| o.to_s(predicate.lang || node.lang || @document_language)}.join(", ")}"
154
- pref = ";\n" + "".ljust(node.to_s(@document_language).length)
160
+
161
+ # Render all predicates and their associated objects
162
+ subject.nodes.each do |predicate|
163
+ objects = predicate.nodes.map { |object| object.to_s(indent: indent, lang: predicate.lang || subject.lang || @document_language) }
164
+ .join(", ")
165
+
166
+ s << "#{pref} #{predicate.to_s} #{objects}"
167
+
168
+ pref = ";\n" + indent
155
169
  end
156
170
  s << ".\n"
171
+ if @config[:empty_line_between_triples]
172
+ s << "\n"
173
+ end
157
174
  end
158
175
  s
159
176
  end
@@ -6,7 +6,7 @@ module IqRdf
6
6
  super(!!b, nil, ::URI.parse("http://www.w3.org/2001/XMLSchema#boolean"))
7
7
  end
8
8
 
9
- def to_s(parent_lang = nil)
9
+ def to_s(options = {})
10
10
  @obj.to_s
11
11
  end
12
12
 
@@ -7,7 +7,7 @@ module IqRdf
7
7
  super(num, nil, ::URI.parse(num.is_a?(Integer) ? "http://www.w3.org/2001/XMLSchema#integer" : "http://www.w3.org/2001/XMLSchema#decimal"))
8
8
  end
9
9
 
10
- def to_s(parent_lang = nil)
10
+ def to_s(options = {})
11
11
  @obj.to_s
12
12
  end
13
13
 
@@ -7,7 +7,7 @@ module IqRdf
7
7
  super(uri)
8
8
  end
9
9
 
10
- def to_s(parent_lang = nil)
10
+ def to_s(options = {})
11
11
  "<#{@obj.to_s}>"
12
12
  end
13
13
 
@@ -34,8 +34,8 @@ module IqRdf
34
34
  end
35
35
  end
36
36
 
37
- def to_s(parent_lang = nil)
38
- lang = @lang || parent_lang # Use the Literals lang when given
37
+ def to_s(options = {})
38
+ lang = @lang || options[:lang] # Use the Literals lang when given
39
39
  lang = (lang && lang != :none) ? "@#{lang}" : ""
40
40
  quote = @obj.to_s.include?("\n") ? '"""' : '"'
41
41
  datatype = if @datatype.is_a?(::URI)
@@ -12,10 +12,12 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- def IqRdf.rails_template(template)
15
+ def IqRdf.rails_template(template, source = nil)
16
+ source ||= template.source
17
+
16
18
  <<-EOV
17
19
  document = IqRdf::Document.new()
18
- #{template.source}
20
+ #{source}
19
21
  if params[:format].to_s == "ttl"
20
22
  controller.response.headers["Content-Type"] ||= 'text/turtle;charset=utf-8'
21
23
  document.to_turtle
@@ -34,7 +36,16 @@ end
34
36
 
35
37
  module ActionView
36
38
 
37
- if Rails.version >= "3"
39
+ if Rails.version >= "6"
40
+ class Template::Handlers::IqRdf
41
+ # see https://github.com/rails/rails/commit/28f88e0074f473f58c2d3fd54cb3daff81027c12
42
+ def self.call(template, source)
43
+ IqRdf.rails_template(template, source)
44
+ end
45
+ end
46
+ ActionView::Template.register_template_handler('iqrdf', ActionView::Template::Handlers::IqRdf)
47
+
48
+ elsif Rails.version >= "3"
38
49
 
39
50
  class Template::Handlers::IqRdf
40
51
  def self.call(template)
data/lib/iq_rdf/uri.rb CHANGED
@@ -31,7 +31,7 @@ module IqRdf
31
31
  "#{self.namespace.uri_prefix.to_s}#{self.uri_postfix.to_s}"
32
32
  end
33
33
 
34
- def to_s(parent_lang = nil)
34
+ def to_s(options = {})
35
35
  if namespace.token # There is a dummy_empty_namespace without token => postfix is a full uri!
36
36
  "#{namespace.turtle_token}:#{self.uri_postfix.to_s}"
37
37
  else
@@ -39,7 +39,7 @@ module IqRdf
39
39
  end
40
40
  end
41
41
 
42
- def is_subject?()
42
+ def is_subject?
43
43
  if (nodes.size > 0) # walk through the nodes: a blank node is an object (but has nodes)
44
44
  nodes.each do |node|
45
45
  return true unless node.is_a?(BlankNode)
@@ -65,5 +65,31 @@ module IqRdf
65
65
  end
66
66
  end
67
67
 
68
+ # Method to add a blank node for the given predicate to the current subject
69
+ # In contrast to the "method_missing" implementation, this method allows one
70
+ # to choose a different namespace for the predicate.
71
+ #
72
+ # Usage:
73
+ # document << IqRdf::Eg.build_uri('test', IqRdf::Eg.build_uri('testType')) do |uri|
74
+ # uri.add_blank_node(IqRdf::Eg, :component) do |blank_node|
75
+ # blank_node.build_predicate_with_ns(IqRdf::Eg, :order, 1)
76
+ # end
77
+ # end
78
+ #
79
+ # Resulting in the following turtle syntax:
80
+ # eg:test a eg:testType
81
+ # eg:component [
82
+ # eg:order 1
83
+ # ]
84
+ def add_blank_node(namespace, postfix, &block)
85
+ predicate = IqRdf::Predicate.new(namespace, postfix, self)
86
+ blank_node = IqRdf::BlankNode.new
87
+
88
+ block.call(blank_node)
89
+
90
+ predicate << blank_node
91
+ self << predicate
92
+ end
93
+
68
94
  end
69
95
  end
@@ -13,5 +13,5 @@
13
13
  # limitations under the License.
14
14
 
15
15
  module IqRdf
16
- VERSION = "0.1.16"
16
+ VERSION = "0.2.0"
17
17
  end
@@ -206,18 +206,18 @@ _:b2 <http://www.umweltprobenbank.de/title> "blubb"@de .
206
206
  over two lines"""@de .
207
207
  <http://www.test.de/testemann> <http://www.test.de/some_literal> "text"@de .
208
208
  rdf
209
- end
210
-
211
- def test_supress_if_empty_option
212
- document = IqRdf::Document.new('http://www.test.de/')
213
- document.namespaces :foaf => 'http://xmlns.com/foaf/0.1/'
209
+ end
214
210
 
215
- document << IqRdf::testemann.Foaf::knows(:suppress_if_empty => true)
216
- document << IqRdf::testemann.Foaf::knows(nil, :suppress_if_empty => true)
217
- document << IqRdf::testemann.Foaf::knows("", :suppress_if_empty => true)
218
- document << IqRdf::testemann.Foaf::knows([], :suppress_if_empty => true)
211
+ def test_supress_if_empty_option
212
+ document = IqRdf::Document.new('http://www.test.de/')
213
+ document.namespaces :foaf => 'http://xmlns.com/foaf/0.1/'
219
214
 
220
- assert_equal("", document.to_ntriples.strip)
221
- end
215
+ document << IqRdf::testemann.Foaf::knows(:suppress_if_empty => true)
216
+ document << IqRdf::testemann.Foaf::knows(nil, :suppress_if_empty => true)
217
+ document << IqRdf::testemann.Foaf::knows("", :suppress_if_empty => true)
218
+ document << IqRdf::testemann.Foaf::knows([], :suppress_if_empty => true)
222
219
 
220
+ assert_equal("", document.to_ntriples.strip)
223
221
  end
222
+
223
+ end
data/test/turtle_test.rb CHANGED
@@ -117,11 +117,11 @@ upb:xkfkrl a skos:Concept;
117
117
  :testIt (:hello :goodbye "bla"@de), "blubb"@de;
118
118
  :anotherTest <http://www.test.de/foo>.
119
119
  skos:testnode :test32 [
120
- :title "dies ist ein test"@de;
121
- :sub [
122
- :title "blubb"@de
123
- ]
124
- ].
120
+ :title "dies ist ein test"@de;
121
+ :sub [
122
+ :title "blubb"@de
123
+ ]
124
+ ].
125
125
  rdf
126
126
  end
127
127
 
@@ -207,12 +207,58 @@ rdf
207
207
  @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
208
208
 
209
209
  :testnode :test32 [
210
- :title "dies ist ein test";
211
- :test "Another test";
212
- :sub [
213
- :title "blubb"
214
- ]
215
- ].
210
+ :title "dies ist ein test";
211
+ :test "Another test";
212
+ :sub [
213
+ :title "blubb"
214
+ ]
215
+ ].
216
+ rdf
217
+ end
218
+
219
+ def test_blank_nodes_with_different_ns
220
+ document = IqRdf::Document.new('http://www.test.de/')
221
+ document.namespaces(qb: 'http://purl.org/linked-data/cube#')
222
+ document.namespaces(eg: 'http://www.example.com')
223
+
224
+ document << IqRdf::Eg.build_uri('dsd', IqRdf::Qb.build_uri('DataStructureDefinition')) do |ds|
225
+ ds.add_blank_node(IqRdf::Qb, :component) do |blank_node|
226
+ blank_node.build_predicate_with_ns(IqRdf::Qb, :dimension, IqRdf::Eg.build_uri('refArea'))
227
+ blank_node.build_predicate_with_ns(IqRdf::Qb, :order, 1)
228
+ blank_node.build_predicate_with_ns(IqRdf::Eg, :cost, 2.25)
229
+ end
230
+ end
231
+
232
+ assert_equal(<<rdf, document.to_turtle)
233
+ @prefix : <http://www.test.de/>.
234
+ @prefix eg: <http://www.example.com>.
235
+ @prefix qb: <http://purl.org/linked-data/cube#>.
236
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
237
+
238
+ eg:dsd a qb:DataStructureDefinition;
239
+ qb:component [
240
+ qb:dimension eg:refArea;
241
+ qb:order 1;
242
+ eg:cost 2.25
243
+ ].
244
+ rdf
245
+ end
246
+
247
+ def test_config
248
+ document = IqRdf::Document.new('http://www.test.de/')
249
+ document.config(empty_line_between_triples: true)
250
+
251
+ document << IqRdf::testemann.myCustomNote("This is an example", :lang => :en)
252
+ document << IqRdf::testemann.myCustomNote("Zweites Beispiel", :lang => :de)
253
+
254
+ assert_equal(<<rdf, document.to_turtle)
255
+ @prefix : <http://www.test.de/>.
256
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
257
+
258
+ :testemann :myCustomNote "This is an example"@en.
259
+
260
+ :testemann :myCustomNote "Zweites Beispiel"@de.
261
+
216
262
  rdf
217
263
  end
218
264
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iq_rdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.16
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Till Schulte-Coerne
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-26 00:00:00.000000000 Z
11
+ date: 2024-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -104,7 +104,7 @@ files:
104
104
  homepage: http://github.com/innoq/iq_rdf
105
105
  licenses: []
106
106
  metadata: {}
107
- post_install_message:
107
+ post_install_message:
108
108
  rdoc_options: []
109
109
  require_paths:
110
110
  - lib
@@ -119,9 +119,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  - !ruby/object:Gem::Version
120
120
  version: '0'
121
121
  requirements: []
122
- rubyforge_project:
123
- rubygems_version: 2.6.11
124
- signing_key:
122
+ rubygems_version: 3.4.10
123
+ signing_key:
125
124
  specification_version: 4
126
125
  summary: IqRdf - A builder like rdf library for ruby and rails
127
126
  test_files: