Class: RDF::SPARQL::Writer

Inherits:
Writer
  • Object
show all
Includes:
Common
Defined in:
lib/lib/writer.rb

Instance Method Summary collapse

Methods included from Common

#distinct?, #each_statement, #each_target, #each_triple, #each_variable, #reduced?, #statements, #targets, #triples, #type, #variables

Constructor Details

#initialize(output = $stdout, options = {}) {|writer| ... } ⇒ Writer

Returns a new instance of Writer.

Parameters:

  • output (IO, File) (defaults to: $stdout)

Yields:

  • (writer)

Yield Parameters:



12
13
14
15
16
17
18
19
20
# File 'lib/lib/writer.rb', line 12

def initialize(output = $stdout, options = {}, &block)
  @output, @options = output, options
  @graph = RDF::Graph.new
  @targets = []
  if block_given?
    block.call(self)
    write_query
  end
end

Instance Method Details

#new_target(name_or_uri = nil) ⇒ Object

-


44
45
46
47
48
49
50
51
# File 'lib/lib/writer.rb', line 44

def new_target(name_or_uri = nil)
  @targets << case name_or_uri
    when String     then RDF::Query::Variable.new(name_or_uri)
    when RDF::URI   then name_or_uri
    else            RDF::Query::Variable.new('v%s' % rand(10000))
  end
  @targets.last
end

#write_queryObject

-


56
57
58
59
60
# File 'lib/lib/writer.rb', line 56

def write_query
  puts format_prologue
  puts format_where
  puts write_epilogue
end

#write_triple(subject, predicate, object) ⇒ void

This method returns an undefined value.

Stores the SPARQL representation of a triple.

Parameters:

  • subject (RDF::Resource)
  • predicate (RDF::URI)
  • object (RDF::Value)


31
32
33
# File 'lib/lib/writer.rb', line 31

def write_triple(subject, predicate, object)
  @graph << RDF::Query::Pattern.new(subject, predicate, object)
end

#write_triples(triples) ⇒ Object

Parameters:

  • triples (Array<Array(Value)>)


37
38
39
# File 'lib/lib/writer.rb', line 37

def write_triples(triples)
  triples.each { |triple| write_triple(*triple) }
end