Class: RDF::Writer Abstract
- Inherits:
-
Object
- Object
- RDF::Writer
- Extended by:
- Enumerable, Util::Aliasing::LateBound
- Includes:
- Writable
- Defined in:
- lib/rdf/writer.rb
Overview
The base class for RDF serializers.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
Any additional options for this writer.
Class Method Summary collapse
-
.buffer(*args) {|writer| ... } ⇒ String
Buffers output into a string buffer.
- .dump(data, io = nil, options = {}) ⇒ void
-
.each {|klass| ... } ⇒ Enumerator
Enumerates known RDF writer classes.
-
.for(options = {}) ⇒ Class
Finds an RDF writer class based on the given criteria.
-
.format(klass = nil) ⇒ Class
(also: format_class)
Retrieves the RDF serialization format class for this writer class.
-
.open(filename, options = {}, &block) ⇒ RDF::Writer
Writes output to the given ‘filename`.
-
.to_sym ⇒ Symbol
Returns a symbol appropriate to use with RDF::Writer.for().
Instance Method Summary collapse
-
#base_uri ⇒ Hash{Symbol => RDF::URI}
Returns the base URI used for this writer.
-
#flush ⇒ void
(also: #flush!)
Flushes the underlying output buffer.
- #format_list(value, options = {}) ⇒ String abstract
- #format_literal(value, options = {}) ⇒ String abstract
- #format_node(value, options = {}) ⇒ String abstract
- #format_term(term, options = {}) ⇒ String (also: #format_value)
- #format_uri(value, options = {}) ⇒ String abstract
-
#initialize(output = $stdout, options = {}) {|writer| ... } ⇒ Writer
constructor
Initializes the writer.
-
#prefix(name, uri = nil) ⇒ RDF::URI
(also: #prefix!)
Defines the given named URI prefix for this writer.
-
#prefixes ⇒ Hash{Symbol => RDF::URI}
Returns the URI prefixes currently defined for this writer.
-
#prefixes=(prefixes) ⇒ Hash{Symbol => RDF::URI}
Defines the given URI prefixes for this writer.
-
#to_sym ⇒ Symbol
Returns a symbol appropriate to use with RDF::Writer.for().
-
#write_comment(text) ⇒ void
abstract
‘self`.
-
#write_epilogue ⇒ void
abstract
‘self`.
-
#write_graph(graph) ⇒ void
deprecated
Deprecated.
replace by ‘RDF::Writable#insert_graph`
-
#write_prologue ⇒ void
abstract
‘self`.
-
#write_statement(statement) ⇒ void
(also: #insert_statement)
‘self`.
-
#write_statements(*statements) ⇒ void
deprecated
Deprecated.
replace by ‘RDF::Writable#insert_statements`
-
#write_triple(subject, predicate, object) ⇒ void
abstract
‘self`.
-
#write_triples(*triples) ⇒ void
‘self`.
Methods included from Util::Aliasing::LateBound
Methods included from Writable
Constructor Details
#initialize(output = $stdout, options = {}) {|writer| ... } ⇒ Writer
Initializes the writer.
198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/rdf/writer.rb', line 198 def initialize(output = $stdout, = {}, &block) @output, @options = output, .dup @nodes, @node_id = {}, 0 if block_given? write_prologue case block.arity when 1 then block.call(self) else instance_eval(&block) end write_epilogue end end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Any additional options for this writer.
217 218 219 |
# File 'lib/rdf/writer.rb', line 217 def @options end |
Class Method Details
.buffer(*args) {|writer| ... } ⇒ String
Buffers output into a string buffer.
139 140 141 142 143 144 |
# File 'lib/rdf/writer.rb', line 139 def self.buffer(*args, &block) StringIO.open do |buffer| self.new(buffer, *args) { |writer| block.call(writer) } buffer.string end end |
.dump(data, io = nil, options = {}) ⇒ void
This method returns an undefined value.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/rdf/writer.rb', line 113 def self.dump(data, io = nil, = {}) io = File.open(io, 'w') if io.is_a?(String) method = data.respond_to?(:each_statement) ? :each_statement : :each if io new(io, ) do |writer| data.send(method) do |statement| writer << statement end writer.flush end else buffer() do |writer| data.send(method) do |statement| writer << statement end end end end |
.each {|klass| ... } ⇒ Enumerator
Enumerates known RDF writer classes.
50 51 52 |
# File 'lib/rdf/writer.rb', line 50 def self.each(&block) @@subclasses.each(&block) end |
.for(format) ⇒ Class .for(filename) ⇒ Class .for(options = {}) ⇒ Class
Finds an RDF writer class based on the given criteria.
79 80 81 82 83 84 |
# File 'lib/rdf/writer.rb', line 79 def self.for( = {}) = .merge(:has_writer => true) if .is_a?(Hash) if format = self.format || Format.for() format.writer end end |
.format(klass = nil) ⇒ Class Also known as: format_class
Retrieves the RDF serialization format class for this writer class.
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rdf/writer.rb', line 90 def self.format(klass = nil) if klass.nil? Format.each do |format| if format.writer == self return format end end nil # not found end end |
.open(filename, options = {}, &block) ⇒ RDF::Writer
Writes output to the given ‘filename`.
154 155 156 157 158 159 160 |
# File 'lib/rdf/writer.rb', line 154 def self.open(filename, = {}, &block) File.open(filename, 'wb') do |file| = .dup [:file_name] ||= filename self.for([:format] || ).new(file, , &block) end end |
.to_sym ⇒ Symbol
Returns a symbol appropriate to use with RDF::Writer.for()
165 166 167 168 169 170 |
# File 'lib/rdf/writer.rb', line 165 def self.to_sym elements = self.to_s.split("::") sym = elements.pop sym = elements.pop if sym == 'Writer' sym.downcase.to_s.to_sym end |
Instance Method Details
#base_uri ⇒ Hash{Symbol => RDF::URI}
Returns the base URI used for this writer.
227 228 229 |
# File 'lib/rdf/writer.rb', line 227 def base_uri @options[:base_uri] end |
#flush ⇒ void Also known as: flush!
This method returns an undefined value.
Flushes the underlying output buffer.
285 286 287 288 |
# File 'lib/rdf/writer.rb', line 285 def flush @output.flush if @output.respond_to?(:flush) self end |
#format_list(value, options = {}) ⇒ String
411 412 413 |
# File 'lib/rdf/writer.rb', line 411 def format_list(value, = {}) format_term(value.subject, ) end |
#format_literal(value, options = {}) ⇒ String
401 402 403 |
# File 'lib/rdf/writer.rb', line 401 def format_literal(value, = {}) raise NotImplementedError.new("#{self.class}#format_literal") # override in subclasses end |
#format_node(value, options = {}) ⇒ String
381 382 383 |
# File 'lib/rdf/writer.rb', line 381 def format_node(value, = {}) raise NotImplementedError.new("#{self.class}#format_node") # override in subclasses end |
#format_term(term, options = {}) ⇒ String Also known as: format_value
363 364 365 366 367 368 369 370 371 372 |
# File 'lib/rdf/writer.rb', line 363 def format_term(term, = {}) case term when String then format_literal(RDF::Literal(term, ), ) when RDF::List then format_list(term, ) when RDF::Literal then format_literal(term, ) when RDF::URI then format_uri(term, ) when RDF::Node then format_node(term, ) else nil end end |
#format_uri(value, options = {}) ⇒ String
391 392 393 |
# File 'lib/rdf/writer.rb', line 391 def format_uri(value, = {}) raise NotImplementedError.new("#{self.class}#format_uri") # override in subclasses end |
#prefix(name, uri) ⇒ RDF::URI #prefix(name) ⇒ RDF::URI Also known as: prefix!
Defines the given named URI prefix for this writer.
275 276 277 278 |
# File 'lib/rdf/writer.rb', line 275 def prefix(name, uri = nil) name = name.to_s.empty? ? nil : (name.respond_to?(:to_sym) ? name.to_sym : name.to_s.to_sym) uri.nil? ? prefixes[name] : prefixes[name] = uri end |
#prefixes ⇒ Hash{Symbol => RDF::URI}
Returns the URI prefixes currently defined for this writer.
239 240 241 |
# File 'lib/rdf/writer.rb', line 239 def prefixes @options[:prefixes] ||= {} end |
#prefixes=(prefixes) ⇒ Hash{Symbol => RDF::URI}
Defines the given URI prefixes for this writer.
254 255 256 |
# File 'lib/rdf/writer.rb', line 254 def prefixes=(prefixes) @options[:prefixes] = prefixes end |
#to_sym ⇒ Symbol
Returns a symbol appropriate to use with RDF::Writer.for()
175 176 177 |
# File 'lib/rdf/writer.rb', line 175 def to_sym self.class.to_sym end |
#write_comment(text) ⇒ void
This method returns an undefined value.
Returns ‘self`.
309 310 311 |
# File 'lib/rdf/writer.rb', line 309 def write_comment(text) self end |
#write_epilogue ⇒ void
This method returns an undefined value.
Returns ‘self`.
301 302 303 |
# File 'lib/rdf/writer.rb', line 301 def write_epilogue self end |
#write_graph(graph) ⇒ void
replace by ‘RDF::Writable#insert_graph`
This method returns an undefined value.
Returns ‘self`.
317 318 319 320 |
# File 'lib/rdf/writer.rb', line 317 def write_graph(graph) graph.each_triple { |*triple| write_triple(*triple) } self end |
#write_prologue ⇒ void
This method returns an undefined value.
Returns ‘self`.
294 295 296 |
# File 'lib/rdf/writer.rb', line 294 def write_prologue self end |
#write_statement(statement) ⇒ void Also known as: insert_statement
This method returns an undefined value.
Returns ‘self`.
334 335 336 337 |
# File 'lib/rdf/writer.rb', line 334 def write_statement(statement) write_triple(*statement.to_triple) self end |
#write_statements(*statements) ⇒ void
replace by ‘RDF::Writable#insert_statements`
This method returns an undefined value.
Returns ‘self`.
326 327 328 329 |
# File 'lib/rdf/writer.rb', line 326 def write_statements(*statements) statements.flatten.each { |statement| write_statement(statement) } self end |
#write_triple(subject, predicate, object) ⇒ void
This method returns an undefined value.
Returns ‘self`.
355 356 357 |
# File 'lib/rdf/writer.rb', line 355 def write_triple(subject, predicate, object) raise NotImplementedError.new("#{self.class}#write_triple") # override in subclasses end |
#write_triples(*triples) ⇒ void
This method returns an undefined value.
Returns ‘self`.
343 344 345 346 |
# File 'lib/rdf/writer.rb', line 343 def write_triples(*triples) triples.each { |triple| write_triple(*triple) } self end |