rdf 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rdf/model/uri.rb CHANGED
@@ -17,12 +17,12 @@ module RDF
17
17
  #
18
18
  # @see http://en.wikipedia.org/wiki/Uniform_Resource_Identifier
19
19
  # @see http://addressable.rubyforge.org/
20
- class URI < Node
20
+ class URI < Resource
21
21
  ##
22
22
  # Creates a new `RDF::URI` instance based on the given `uri` string.
23
23
  #
24
- # This is just an alias for {RDF::URI.new} for compatibity with
25
- # {Addressable::URI].
24
+ # This is just an alias for {#initialize RDF::URI.new} for compatibity
25
+ # with `Addressable::URI.parse`.
26
26
  #
27
27
  # @param [String] uri
28
28
  # @return [URI]
@@ -63,6 +63,20 @@ module RDF
63
63
  false
64
64
  end
65
65
 
66
+ ##
67
+ # Joins several URIs together.
68
+ #
69
+ # @param [Array<String, URI, #to_str>] uris
70
+ # @return [URI]
71
+ def join(*uris)
72
+ result = @uri
73
+ uris.each do |uri|
74
+ result.path += '/' unless result.path[-1] == ?/ # '/'
75
+ result = result.join(uri)
76
+ end
77
+ self.class.new(result)
78
+ end
79
+
66
80
  ##
67
81
  # Returns `true` if this URI's path component is equal to `/`.
68
82
  #
@@ -200,7 +214,11 @@ module RDF
200
214
  # @private
201
215
  def method_missing(symbol, *args, &block)
202
216
  if @uri.respond_to?(symbol)
203
- @uri.send(symbol, *args, &block)
217
+ case result = @uri.send(symbol, *args, &block)
218
+ when Addressable::URI
219
+ self.class.new(result)
220
+ else result
221
+ end
204
222
  else
205
223
  super
206
224
  end
@@ -14,8 +14,8 @@ module RDF::NTriples
14
14
  format RDF::NTriples::Format
15
15
 
16
16
  ##
17
- # @return [Array, nil]
18
- # @see http://www.w3.org/TR/rdf-testcases/#ntrip_grammar
17
+ # @return [Array]
18
+ # @see http://www.w3.org/TR/rdf-testcases/#ntrip_grammar
19
19
  def read_triple
20
20
  loop do
21
21
  readline.strip! # EOFError thrown on end of input
@@ -66,7 +66,7 @@ module RDF::NTriples
66
66
  elsif datatype = match(/^(\^\^)/)
67
67
  RDF::Literal.new(literal, :datatype => read_uriref || fail_object)
68
68
  else
69
- literal # plain string literal
69
+ RDF::Literal.new(literal) # plain string literal
70
70
  end
71
71
  end
72
72
  end
@@ -7,6 +7,8 @@ module RDF::NTriples
7
7
  format RDF::NTriples::Format
8
8
 
9
9
  ##
10
+ # Outputs an N-Triples comment line.
11
+ #
10
12
  # @param [String] text
11
13
  # @return [void]
12
14
  def write_comment(text)
@@ -14,32 +16,40 @@ module RDF::NTriples
14
16
  end
15
17
 
16
18
  ##
17
- # @param [Resource] subject
18
- # @param [URI] predicate
19
- # @param [Value] object
19
+ # Outputs the N-Triples representation of a triple.
20
+ #
21
+ # @param [RDF::Resource] subject
22
+ # @param [RDF::URI] predicate
23
+ # @param [RDF::Value] object
20
24
  # @return [void]
21
25
  def write_triple(subject, predicate, object)
22
26
  puts "%s %s %s ." % [subject, predicate, object].map { |value| format_value(value) }
23
27
  end
24
28
 
25
29
  ##
26
- # @param [URI] value
30
+ # Returns the N-Triples representation of a blank node.
31
+ #
32
+ # @param [RDF::Node] value
27
33
  # @param [Hash{Symbol => Object}] options
28
34
  # @return [String]
29
- def format_uri(value, options = {})
30
- "<%s>" % uri_for(value)
35
+ def format_node(value, options = {})
36
+ "_:%s" % node.id
31
37
  end
32
38
 
33
39
  ##
34
- # @param [Node] value
40
+ # Returns the N-Triples representation of a URI reference.
41
+ #
42
+ # @param [RDF::URI] value
35
43
  # @param [Hash{Symbol => Object}] options
36
44
  # @return [String]
37
- def format_node(value, options = {})
38
- "_:%s" % node.id
45
+ def format_uri(value, options = {})
46
+ "<%s>" % uri_for(value)
39
47
  end
40
48
 
41
49
  ##
42
- # @param [Literal, String, #to_s] value
50
+ # Returns the N-Triples representation of a literal.
51
+ #
52
+ # @param [RDF::Literal, String, #to_s] value
43
53
  # @param [Hash{Symbol => Object}] options
44
54
  # @return [String]
45
55
  def format_literal(value, options = {})
data/lib/rdf/reader.rb CHANGED
@@ -8,9 +8,9 @@ module RDF
8
8
  # @example Obtaining an RDF reader class
9
9
  # RDF::Reader.for(:ntriples) #=> RDF::NTriples::Reader
10
10
  # RDF::Reader.for("spec/data/test.nt")
11
- # RDF::Reader.for(:file_name => "spec/data/test.nt")
11
+ # RDF::Reader.for(:file_name => "spec/data/test.nt")
12
12
  # RDF::Reader.for(:file_extension => "nt")
13
- # RDF::Reader.for(:content_type => "text/plain")
13
+ # RDF::Reader.for(:content_type => "text/plain")
14
14
  #
15
15
  # @example Instantiating an RDF reader class
16
16
  # RDF::Reader.for(:ntriples).new($stdin) { |reader| ... }
@@ -114,9 +114,9 @@ module RDF
114
114
  end
115
115
 
116
116
  ##
117
- # @param [IO, String] input
117
+ # @param [IO, File, String] input
118
118
  # @yield [reader]
119
- # @yieldparam [Reader]
119
+ # @yieldparam [Reader] reader
120
120
  def initialize(input = $stdin, options = {}, &block)
121
121
  @options = options
122
122
  @nodes = {}
@@ -129,17 +129,6 @@ module RDF
129
129
  false
130
130
  end
131
131
 
132
- ##
133
- # Enumerates each RDF statement in this repository.
134
- #
135
- # @yield [statement]
136
- # @yieldparam [Statement]
137
- # @return [Enumerator]
138
- # @see RDF::Enumerable#each_statement
139
- def each(&block)
140
- @data.each(&block)
141
- end
142
-
143
132
  ##
144
133
  # Returns `true` if this repository contains no RDF statements.
145
134
  #
@@ -169,16 +158,27 @@ module RDF
169
158
  end
170
159
 
171
160
  ##
172
- # Inserts an RDF statement into the underlying storage.
161
+ # Enumerates each RDF statement in this repository.
162
+ #
163
+ # @yield [statement]
164
+ # @yieldparam [Statement] statement
165
+ # @return [Enumerator]
166
+ # @see RDF::Enumerable#each_statement
167
+ def each(&block)
168
+ @data.each(&block)
169
+ end
170
+
171
+ ##
172
+ # Inserts the given RDF statement into the underlying storage.
173
173
  #
174
174
  # @param [RDF::Statement] statement
175
175
  # @return [void]
176
176
  def insert_statement(statement)
177
- @data.push(statement) unless @data.include?(statement)
177
+ @data.push(statement.dup) unless @data.include?(statement)
178
178
  end
179
179
 
180
180
  ##
181
- # Deletes an RDF statement from the underlying storage.
181
+ # Deletes the given RDF statement from the underlying storage.
182
182
  #
183
183
  # @param [RDF::Statement] statement
184
184
  # @return [void]
@@ -189,7 +189,7 @@ module RDF
189
189
  ##
190
190
  # Deletes all RDF statements from this repository.
191
191
  #
192
- # @return [Repository]
192
+ # @return [void]
193
193
  # @see RDF::Mutable#clear
194
194
  def clear_statements
195
195
  @data.clear
data/lib/rdf/version.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  module RDF
2
2
  module VERSION
3
3
  MAJOR = 0
4
- MINOR = 0
5
- TINY = 9
4
+ MINOR = 1
5
+ TINY = 0
6
6
  EXTRA = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
@@ -15,5 +15,9 @@ module RDF
15
15
  ##
16
16
  # @return [String]
17
17
  def self.to_str() STRING end
18
+
19
+ ##
20
+ # @return [Array(Integer, Integer, Integer)]
21
+ def self.to_a() [MAJOR, MINOR, TINY] end
18
22
  end
19
23
  end
data/lib/rdf/vocab.rb CHANGED
@@ -8,6 +8,7 @@ module RDF
8
8
  #
9
9
  # * {RDF::CC} - Creative Commons (CC)
10
10
  # * {RDF::DC} - Dublin Core (DC)
11
+ # * {RDF::DC11} - Dublin Core 1.1 (DC11) _deprecated_
11
12
  # * {RDF::DOAP} - Description of a Project (DOAP)
12
13
  # * {RDF::EXIF} - Exchangeable Image File Format (EXIF)
13
14
  # * {RDF::FOAF} - Friend of a Friend (FOAF)
@@ -23,6 +24,7 @@ module RDF
23
24
  #
24
25
  # @example Using pre-defined RDF vocabularies
25
26
  # include RDF
27
+ #
26
28
  # DC.title #=> RDF::URI("http://purl.org/dc/terms/title")
27
29
  # FOAF.knows #=> RDF::URI("http://xmlns.com/foaf/0.1/knows")
28
30
  # RDFS.seeAlso #=> RDF::URI("http://www.w3.org/2000/01/rdf-schema#seeAlso")
@@ -52,7 +54,7 @@ module RDF
52
54
  # This is needed since all vocabulary classes are defined using
53
55
  # Ruby's autoloading facility, meaning that `@@subclasses` will
54
56
  # be empty until each subclass has been touched or require'd.
55
- %w(cc dc doap exif foaf http owl rdfs rss sioc skos wot xhtml xsd).each do |prefix|
57
+ %w(cc dc dc11 doap exif foaf http owl rdfs rss sioc skos wot xhtml xsd).each do |prefix|
56
58
  require "rdf/vocab/#{prefix}"
57
59
  end
58
60
  @@subclasses.each(&block)
@@ -0,0 +1,23 @@
1
+ module RDF
2
+ ##
3
+ # Dublin Core (DC) legacy vocabulary.
4
+ #
5
+ # @see http://dublincore.org/schemas/rdfs/
6
+ class DC11 < Vocabulary("http://purl.org/dc/elements/1.1/")
7
+ property :contributor
8
+ property :coverage
9
+ property :creator
10
+ property :date
11
+ property :description
12
+ property :format
13
+ property :identifier
14
+ property :language
15
+ property :publisher
16
+ property :relation
17
+ property :rights
18
+ property :source
19
+ property :subject
20
+ property :title
21
+ property :type
22
+ end
23
+ end
data/lib/rdf/writer.rb CHANGED
@@ -8,21 +8,21 @@ module RDF
8
8
  # @example Obtaining an RDF writer class
9
9
  # RDF::Writer.for(:ntriples) #=> RDF::NTriples::Writer
10
10
  # RDF::Writer.for("spec/data/output.nt")
11
- # RDF::Writer.for(:file_name => "spec/data/output.nt")
11
+ # RDF::Writer.for(:file_name => "spec/data/output.nt")
12
12
  # RDF::Writer.for(:file_extension => "nt")
13
- # RDF::Writer.for(:content_type => "text/plain")
13
+ # RDF::Writer.for(:content_type => "text/plain")
14
14
  #
15
15
  # @example Instantiating an RDF writer class
16
16
  # RDF::Writer.for(:ntriples).new($stdout) { |writer| ... }
17
17
  #
18
- # @example Serializing RDF statements to a file
18
+ # @example Serializing RDF statements into a file
19
19
  # RDF::Writer.open("spec/data/output.nt") do |writer|
20
20
  # graph.each_statement do |statement|
21
21
  # writer << statement
22
22
  # end
23
23
  # end
24
24
  #
25
- # @example Serializing RDF statements to a string
25
+ # @example Serializing RDF statements into a string
26
26
  # RDF::Writer.for(:ntriples).buffer do |writer|
27
27
  # graph.each_statement do |statement|
28
28
  # writer << statement
@@ -96,6 +96,22 @@ module RDF
96
96
  alias_method :format_class, :format
97
97
  end
98
98
 
99
+ def self.dump(data, io = nil, options = {})
100
+ if io
101
+ new(io) do |writer|
102
+ data.each_statement do |statement|
103
+ writer << statement
104
+ end
105
+ end
106
+ else
107
+ buffer do |writer|
108
+ data.each_statement do |statement|
109
+ writer << statement
110
+ end
111
+ end
112
+ end
113
+ end
114
+
99
115
  def self.buffer(*args, &block)
100
116
  require 'stringio' unless defined?(StringIO)
101
117
 
@@ -204,7 +220,7 @@ module RDF
204
220
  # @return [String]
205
221
  def format_value(value, options = {})
206
222
  case value
207
- when String then format_literal(value, options)
223
+ when String then format_literal(value, options) # FIXME
208
224
  when RDF::Literal then format_literal(value, options)
209
225
  when RDF::URI then format_uri(value, options)
210
226
  when RDF::Node then format_node(value, options)
@@ -279,8 +295,8 @@ module RDF
279
295
  # @param [String] string
280
296
  # @return [String]
281
297
  def escaped(string)
282
- string.gsub("\\", "\\\\").gsub("\t", "\\\t").
283
- gsub("\n", "\\\n").gsub("\r", "\\\r").gsub("\"", "\\\"")
298
+ string.gsub('\\', '\\\\').gsub("\t", '\\t').
299
+ gsub("\n", '\\n').gsub("\r", '\\r').gsub('"', '\\"')
284
300
  end
285
301
 
286
302
  ##
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arto Bendiken
@@ -10,9 +10,19 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2010-02-01 00:00:00 +01:00
13
+ date: 2010-03-10 00:00:00 +01:00
14
14
  default_executable: rdf
15
15
  dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rdf-spec
18
+ type: :development
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.9
25
+ version:
16
26
  - !ruby/object:Gem::Dependency
17
27
  name: rspec
18
28
  type: :development
@@ -31,7 +41,7 @@ dependencies:
31
41
  requirements:
32
42
  - - ">="
33
43
  - !ruby/object:Gem::Version
34
- version: 0.5.2
44
+ version: 0.5.3
35
45
  version:
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: addressable
@@ -85,12 +95,10 @@ files:
85
95
  - lib/rdf/query.rb
86
96
  - lib/rdf/reader.rb
87
97
  - lib/rdf/repository.rb
88
- - lib/rdf/spec/enumerable.rb
89
- - lib/rdf/spec/repository.rb
90
- - lib/rdf/spec.rb
91
98
  - lib/rdf/version.rb
92
99
  - lib/rdf/vocab/cc.rb
93
100
  - lib/rdf/vocab/dc.rb
101
+ - lib/rdf/vocab/dc11.rb
94
102
  - lib/rdf/vocab/doap.rb
95
103
  - lib/rdf/vocab/exif.rb
96
104
  - lib/rdf/vocab/foaf.rb
data/lib/rdf/spec.rb DELETED
@@ -1,138 +0,0 @@
1
- require 'spec'
2
-
3
- module RDF
4
- ##
5
- # RDF extensions for RSpec.
6
- #
7
- # @see http://rspec.info/
8
- module Spec
9
- ##
10
- # RDF matchers for RSpec.
11
- #
12
- # @see http://rspec.rubyforge.org/rspec/1.2.9/classes/Spec/Matchers.html
13
- module Matchers
14
- ##
15
- # Defines a new RSpec matcher.
16
- #
17
- # @param [Symbol] name
18
- # @return [void]
19
- def self.define(name, &declarations)
20
- define_method name do |*expected|
21
- ::Spec::Matchers::Matcher.new(name, *expected, &declarations)
22
- end
23
- end
24
-
25
- define :be_a_statement do
26
- match do |statement|
27
- statement.should be_instance_of(RDF::Statement)
28
- statement.subject.should be_a_kind_of(RDF::Resource)
29
- statement.predicate.should be_a_kind_of(RDF::URI)
30
- statement.object.should be_a_kind_of(RDF::Value) unless statement.object.is_a?(String) # FIXME
31
- true
32
- end
33
- end
34
-
35
- define :be_a_triple do
36
- match do |triple|
37
- triple.should be_instance_of(Array)
38
- triple.size.should == 3
39
- triple[0].should be_a_kind_of(RDF::Resource)
40
- triple[1].should be_a_kind_of(RDF::URI)
41
- triple[2].should be_a_kind_of(RDF::Value) unless triple[2].is_a?(String) # FIXME
42
- true
43
- end
44
- end
45
-
46
- define :be_a_quad do
47
- match do |quad|
48
- quad.should be_instance_of(Array)
49
- quad.size.should == 4
50
- quad[0].should be_a_kind_of(RDF::Resource)
51
- quad[1].should be_a_kind_of(RDF::URI)
52
- quad[2].should be_a_kind_of(RDF::Value) unless quad[2].is_a?(String) # FIXME
53
- quad[3].should be_a_kind_of(RDF::Resource) unless quad[3].nil?
54
- true
55
- end
56
- end
57
-
58
- define :be_a_resource do
59
- match do |value|
60
- value.should be_a_kind_of(RDF::Resource)
61
- true
62
- end
63
- end
64
-
65
- define :be_a_uri do
66
- match do |value|
67
- value.should be_a_kind_of(RDF::URI)
68
- true
69
- end
70
- end
71
-
72
- define :be_a_value do
73
- match do |value|
74
- value.should be_a_kind_of(RDF::Value) unless value.is_a?(String) # FIXME
75
- true
76
- end
77
- end
78
-
79
- define :be_a_vocabulary do |base_uri|
80
- match do |vocabulary|
81
- vocabulary.should be_a_kind_of(Module)
82
- vocabulary.should respond_to(:to_uri)
83
- vocabulary.to_uri.to_s.should == base_uri
84
- vocabulary.should respond_to(:[])
85
- true
86
- end
87
- end
88
-
89
- define :have_properties do |base_uri, properties|
90
- match do |vocabulary|
91
- properties.map(&:to_sym).each do |property|
92
- vocabulary[property].should be_a_uri
93
- vocabulary[property].to_s.should == "#{base_uri}#{property}"
94
- #vocabulary.should respond_to(property) # FIXME
95
- lambda { vocabulary.send(property) }.should_not raise_error
96
- vocabulary.send(property).should be_a_uri
97
- vocabulary.send(property.to_s).should be_a_uri
98
- vocabulary.send(property).to_s.should == "#{base_uri}#{property}"
99
- end
100
- true
101
- end
102
- end
103
-
104
- define :have_subclasses do |base_uri, klasses|
105
- match do |vocabulary|
106
- klasses.map(&:to_sym).each do |klass|
107
- # TODO
108
- end
109
- true
110
- end
111
- end
112
-
113
- define :be_a_repository do
114
- match do |repository|
115
- repository.should be_a_kind_of(RDF::Repository)
116
- true
117
- end
118
- end
119
-
120
- define :be_a_repository_of_size do |size|
121
- match do |repository|
122
- repository.should be_a_repository
123
- repository.size == size
124
- end
125
- end
126
-
127
- define :have_predicate do |predicate, count|
128
- match do |queryable|
129
- if count.nil?
130
- queryable.has_predicate?(predicate)
131
- else
132
- queryable.query([nil, predicate, nil]).size == count
133
- end
134
- end
135
- end
136
- end # module Matchers
137
- end # module Spec
138
- end # module RDF