lib/rdf/model/statement.rb in rdf-0.0.9 vs lib/rdf/model/statement.rb in rdf-0.1.0

- old
+ new

@@ -1,21 +1,21 @@ module RDF ## # An RDF statement. # # @example Creating an RDF statement - # s = RDF::URI.new("http://gemcutter.org/gems/rdf") + # s = RDF::URI.new("http://rubygems.org/gems/rdf") # p = RDF::DC.creator # o = RDF::URI.new("http://ar.to/#self") # RDF::Statement.new(s, p, o) # # @example Creating an RDF statement with a context # RDF::Statement.new(s, p, o, :context => uri) # # @example Creating an RDF statement from a `Hash` # RDF::Statement.new({ - # :subject => RDF::URI.new("http://gemcutter.org/gems/rdf"), + # :subject => RDF::URI.new("http://rubygems.org/gems/rdf"), # :predicate => RDF::DC.creator, # :object => RDF::URI.new("http://ar.to/#self"), # }) # class Statement < Value @@ -60,11 +60,15 @@ @id = options.delete(:id) if options.has_key?(:id) @context = options.delete(:context) || options.delete(:graph) @options = options @subject = subject @predicate = predicate - @object = object + @object = case object + when nil then nil + when RDF::Value then object + else RDF::Literal.new(object) + end end end ## # @return [Boolean] @@ -204,16 +208,28 @@ def to_hash(subject_key = :subject, predicate_key = :predicate, object_key = :object) {subject_key => subject, predicate_key => predicate, object_key => object} end ## + # Returns a string representation of this statement. + # # @return [String] def to_s require 'stringio' unless defined?(StringIO) StringIO.open do |buffer| - buffer << "<#{subject}> " - buffer << "<#{predicate}> " - buffer << "<#{object}> ." + buffer << case subject + when RDF::Node then subject.to_s + when RDF::URI then "<#{subject}>" + else subject.inspect + end + buffer << " <#{predicate}> " + buffer << case object + when RDF::Literal then object.to_s + when RDF::Node then object.to_s + when RDF::URI then "<#{object}>" + else object.inspect + end + buffer << " ." buffer.string end end end end