Sha256: 0960f5cc342f1d63598d5c3e92de5a0275c835dfdabb063384ed3820c0348d3e

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

module RDF::NTriples
  ##
  # N-Triples serializer.
  #
  # @see http://www.w3.org/TR/rdf-testcases/#ntriples
  class Writer < RDF::Writer
    format RDF::NTriples::Format

    ##
    # @param  [String] text
    # @return [void]
    def write_comment(text)
      puts "# #{text}"
    end

    ##
    # @param  [Resource] subject
    # @param  [URI]      predicate
    # @param  [Value]    object
    # @return [void]
    def write_triple(subject, predicate, object)
      puts "%s %s %s ." % [subject, predicate, object].map { |value| format_value(value) }
    end

    ##
    # @param  [URI]                    value
    # @param  [Hash{Symbol => Object}] options
    # @return [String]
    def format_uri(value, options = {})
      "<%s>" % uri_for(value)
    end

    ##
    # @param  [Node]                   value
    # @param  [Hash{Symbol => Object}] options
    # @return [String]
    def format_node(value, options = {})
      "_:%s" % node.id
    end

    ##
    # @param  [Literal, String, #to_s] value
    # @param  [Hash{Symbol => Object}] options
    # @return [String]
    def format_literal(value, options = {})
      case value
        when RDF::Literal
          text = quoted(escaped(value.value))
          text << "@#{value.language}" if value.language
          text << "^^<#{uri_for(value.datatype)}>" if value.datatype
          text
        else
          quoted(escaped(value.to_s))
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rdf-0.0.9 lib/rdf/ntriples/writer.rb
rdf-0.0.8 lib/rdf/ntriples/writer.rb
rdf-0.0.7 lib/rdf/ntriples/writer.rb