Sha256: bd8693730febc27ad4c73356fa45009f20e797d61dd09615d71eaa21ebbf202c

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

module RDF::TriG
  ##
  # Streaming writer interface
  # @author [Gregg Kellogg](http://greggkellogg.net/)
  module StreamingWriter
    ##
    # Write out a statement, retaining current
    # `subject` and `predicate` to create more compact output
    # @return [void] `self`
    def stream_statement(statement)
      if statement.graph_name != @streaming_graph
        stream_epilogue
        if statement.graph_name
          @output.write "#{format_term(statement.graph_name, options)} {"
        end
        @streaming_graph, @streaming_subject, @streaming_predicate = statement.graph_name, statement.subject, statement.predicate
        @output.write "#{format_term(statement.subject, options)} "
        @output.write "#{statement.predicate == RDF.type ? 'a' : format_term(statement.predicate, options)} "
      elsif statement.subject != @streaming_subject
        @output.puts " ." if @previous_statement
        @output.write "#{indent(@streaming_subject ? 1 : 0)}"
        @streaming_subject, @streaming_predicate = statement.subject, statement.predicate
        @output.write "#{format_term(statement.subject, options)} "
        @output.write "#{statement.predicate == RDF.type ? 'a' : format_term(statement.predicate, options)} "
      elsif statement.predicate != @streaming_predicate
        @streaming_predicate = statement.predicate
        @output.write ";\n#{indent(@streaming_subject ? 2 : 1)}#{statement.predicate == RDF.type ? 'a' : format_term(statement.predicate, options)} "
      else
        @output.write ",\n#{indent(@streaming_subject ? 3 : 2)}"
      end
      @output.write("#{format_term(statement.object, options)}")
      @previous_statement = statement
    end

    ##
    # Complete open statements
    # @return [void] `self`
    def stream_epilogue
      case
      when @previous_statement.nil? ;
      when @streaming_graph then @output.puts " }"
      else @output.puts " ."
      end
    end

    private
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rdf-trig-3.0.1 lib/rdf/trig/streaming_writer.rb