rdf 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,5 +1,5 @@
1
- RDF.rb: RDF for Ruby
2
- ====================
1
+ RDF.rb: Linked Data for Ruby
2
+ ============================
3
3
 
4
4
  This is a pure-Ruby library for working with [Resource Description Framework
5
5
  (RDF)][RDF] data.
@@ -147,8 +147,8 @@ See Also
147
147
  * [RDFcache](http://rdfcache.rubyforge.org/)
148
148
  * [Trinity](http://trinity.datagraph.org/)
149
149
 
150
- Author
151
- ------
150
+ Authors
151
+ -------
152
152
 
153
153
  * [Arto Bendiken](mailto:arto.bendiken@gmail.com) - <http://ar.to/>
154
154
  * [Ben Lavender](mailto:blavender@gmail.com) - <http://bhuga.net/>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
data/lib/rdf.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'open-uri'
1
2
  require 'rdf/version'
2
3
 
3
4
  module RDF
@@ -39,6 +39,7 @@ module RDF
39
39
  count = 0
40
40
  Reader.open(filename, options) do |reader|
41
41
  reader.each_statement do |statement|
42
+ statement.context = options[:context] if options[:context]
42
43
  insert_statement(statement)
43
44
  count += 1
44
45
  end
@@ -13,10 +13,14 @@ module RDF
13
13
  ##
14
14
  # Queries `self` for RDF statements matching the given pattern.
15
15
  #
16
- # @param [Query, Statement, Array(Value)] pattern
16
+ # @example
17
+ # queryable.query([nil, RDF::DOAP.developer, nil])
18
+ # queryable.query(:predicate => RDF::DOAP.developer)
19
+ #
20
+ # @param [Query, Statement, Array(Value), Hash] pattern
17
21
  # @yield [statement]
18
22
  # @yieldparam [Statement]
19
- # @return [Array<Statement>, nil]
23
+ # @return [Array<Statement>]
20
24
  def query(pattern, &block)
21
25
  raise TypeError.new("#{self} is not readable") if respond_to?(:readable) && !readable?
22
26
 
@@ -25,6 +29,8 @@ module RDF
25
29
  pattern.execute(self, &block)
26
30
  when Array
27
31
  query(Statement.new(*pattern), &block)
32
+ when Hash
33
+ query(Statement.new(pattern), &block)
28
34
  when Statement
29
35
  if block_given?
30
36
  find_all { |statement| pattern === statement }.each(&block)
@@ -32,7 +38,7 @@ module RDF
32
38
  find_all { |statement| pattern === statement }.extend(RDF::Enumerable, RDF::Queryable)
33
39
  end
34
40
  else
35
- raise ArgumentError.new("expected RDF::Query or RDF::Pattern, got #{pattern.inspect}")
41
+ raise ArgumentError.new("expected RDF::Query, RDF::Pattern, Array or Hash, but got #{pattern.inspect}")
36
42
  end
37
43
  end
38
44
  end
@@ -4,7 +4,20 @@ module RDF
4
4
  #
5
5
  # @abstract
6
6
  class Resource < Value
7
- # Prevent instantiation of this class.
8
- private_class_method :new
7
+ ##
8
+ # Instantiates an {RDF::Node} or an {RDF::URI}, depending on the given
9
+ # argument.
10
+ #
11
+ # @return [RDF::Resource]
12
+ def self.new(*args, &block)
13
+ if self == Resource
14
+ case arg = args.shift
15
+ when /^_:(.*)$/ then Node.new($1, *args, &block)
16
+ else URI.new(arg, *args, &block)
17
+ end
18
+ else
19
+ super
20
+ end
21
+ end
9
22
  end
10
23
  end
@@ -49,9 +49,10 @@ module RDF
49
49
  # @param [Hash{Symbol => Object}] options
50
50
  # @option options [Resource] :context (nil)
51
51
  def initialize(subject = nil, predicate = nil, object = nil, options = {})
52
+ options = options.dup unless options.empty?
52
53
  case subject
53
54
  when Hash
54
- options = subject
55
+ options = subject.dup
55
56
  subject = options.delete(:subject)
56
57
  predicate = options.delete(:predicate)
57
58
  object = options.delete(:object)
@@ -143,7 +144,7 @@ module RDF
143
144
  # @param [Statement] other
144
145
  # @return [Boolean]
145
146
  def eql?(other)
146
- other.is_a?(Statement) && self == other
147
+ other.is_a?(Statement) && self == other && self.context == other.context
147
148
  end
148
149
 
149
150
  ##
@@ -157,6 +158,7 @@ module RDF
157
158
  # @param [Statement] other
158
159
  # @return [Boolean]
159
160
  def ===(other)
161
+ return false if has_context? && context != other.context
160
162
  return false if has_subject? && subject != other.subject
161
163
  return false if has_predicate? && predicate != other.predicate
162
164
  return false if has_object? && object != other.object
@@ -228,7 +230,10 @@ module RDF
228
230
  when RDF::URI then "<#{object}>"
229
231
  else object.inspect
230
232
  end
231
- buffer << " ."
233
+ buffer << case context
234
+ when nil then " ."
235
+ else " <#{context}> ."
236
+ end
232
237
  buffer.string
233
238
  end
234
239
  end
@@ -2,7 +2,7 @@ module RDF::NTriples
2
2
  ##
3
3
  # N-Triples format specification.
4
4
  #
5
- # @example Obtaining an RDF/NTriples format class
5
+ # @example Obtaining an NTriples format class
6
6
  # RDF::Format.for(:ntriples) #=> RDF::NTriples::Format
7
7
  # RDF::Format.for("etc/doap.nt")
8
8
  # RDF::Format.for(:file_name => "etc/doap.nt")
@@ -2,7 +2,7 @@ module RDF::NTriples
2
2
  ##
3
3
  # N-Triples parser.
4
4
  #
5
- # @example Obtaining an RDF/NTriples reader class
5
+ # @example Obtaining an NTriples reader class
6
6
  # RDF::Reader.for(:ntriples) #=> RDF::NTriples::Reader
7
7
  # RDF::Reader.for("etc/doap.nt")
8
8
  # RDF::Reader.for(:file_name => "etc/doap.nt")
@@ -104,7 +104,7 @@ module RDF
104
104
  # @yieldparam [Reader]
105
105
  # @raise [FormatError] if no reader available for the specified format
106
106
  def self.open(filename, options = {}, &block)
107
- File.open(filename, 'rb') do |file|
107
+ Kernel.open(filename, 'rb') do |file|
108
108
  if reader = self.for(options[:format] || filename)
109
109
  reader.new(file, options, &block)
110
110
  else
@@ -89,9 +89,9 @@ module RDF
89
89
  # @yield [repository]
90
90
  # @yieldparam [Repository] repository
91
91
  def initialize(options = {}, &block)
92
- @uri = options.delete(:uri)
93
- @title = options.delete(:title)
94
- @options = options
92
+ @options = options.dup
93
+ @uri = @options.delete(:uri)
94
+ @title = @options.delete(:title)
95
95
 
96
96
  # Provide a default in-memory implementation:
97
97
  if self.class.equal?(RDF::Repository)
@@ -2,15 +2,15 @@ module RDF
2
2
  module VERSION
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 1
5
+ TINY = 2
6
6
  EXTRA = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
9
- STRING << "-#{EXTRA}" if EXTRA
9
+ STRING << ".#{EXTRA}" if EXTRA
10
10
 
11
11
  ##
12
12
  # @return [String]
13
- def self.to_s() STRING end
13
+ def self.to_s() STRING end
14
14
 
15
15
  ##
16
16
  # @return [String]
@@ -97,11 +97,13 @@ module RDF
97
97
  end
98
98
 
99
99
  def self.dump(data, io = nil, options = {})
100
+ io = File.open(io, 'w') if io.is_a?(String)
100
101
  if io
101
102
  new(io) do |writer|
102
103
  data.each_statement do |statement|
103
104
  writer << statement
104
105
  end
106
+ writer.flush
105
107
  end
106
108
  else
107
109
  buffer do |writer|
@@ -258,6 +260,16 @@ module RDF
258
260
  raise NotImplementedError # override in subclasses
259
261
  end
260
262
 
263
+ ##
264
+ # Flushes the underlying output buffer.
265
+ #
266
+ # @return [void]
267
+ def flush
268
+ @output.flush if @output.respond_to?(:flush)
269
+ end
270
+
271
+ alias_method :flush!, :flush
272
+
261
273
  protected
262
274
 
263
275
  def puts(*args)
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Arto Bendiken
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-03-14 00:00:00 +01:00
18
+ date: 2010-03-27 00:00:00 +01:00
19
19
  default_executable: rdf
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency