lib/rdf/writer.rb in rdf-3.0.4 vs lib/rdf/writer.rb in rdf-3.0.5
- old
+ new
@@ -156,80 +156,70 @@
##
# @param [RDF::Enumerable, #each] data
# the graph or repository to dump
# @param [IO, File, String] io
# the output stream or file to write to
- # @param [Encoding, String, Symbol] encoding
- # the encoding to use on the output stream.
- # Defaults to the format associated with `content_encoding`.
# @param [Hash{Symbol => Object}] options
# passed to {RDF::Writer#initialize} or {RDF::Writer.buffer}
# @return [void]
- def self.dump(data, io = nil, encoding: nil, **options)
- if io.is_a?(String)
- io = File.open(io, 'w')
- elsif io.respond_to?(:external_encoding) && io.external_encoding
- encoding ||= io.external_encoding
- end
- io.set_encoding(encoding) if io.respond_to?(:set_encoding) && encoding
+ def self.dump(data, io = nil, **options)
+ io = File.open(io, 'w') if io.is_a?(String)
method = data.respond_to?(:each_statement) ? :each_statement : :each
if io
- new(io, encoding: encoding, **options) do |writer|
+ new(io, **options) do |writer|
+ io.set_encoding(writer.encoding) if io.respond_to?(:set_encoding)
data.send(method) do |statement|
writer << statement
end
writer.flush
end
else
- buffer(encoding: encoding, **options) do |writer|
+ buffer(**options) do |writer|
data.send(method) do |statement|
writer << statement
end
end
end
end
##
# Buffers output into a string buffer.
#
- # @param [Encoding, String, Symbol] encoding
- # the encoding to use on the output stream.
- # Defaults to the format associated with `content_encoding`.
# @param [Hash{Symbol => Object}] options
# passed to {RDF::Writer#initialize}
# @yield [writer]
# @yieldparam [RDF::Writer] writer
# @yieldreturn [void]
# @return [String]
# @raise [ArgumentError] if no block is provided
- def self.buffer(*args, encoding: nil, **options, &block)
- encoding ||= Encoding::UTF_8 if RUBY_PLATFORM == "java"
+ def self.buffer(*args, **options, &block)
raise ArgumentError, "block expected" unless block_given?
StringIO.open do |buffer|
- buffer.set_encoding(encoding) if encoding
- self.new(buffer, *args, encoding: encoding, **options) { |writer| block.call(writer) }
+ self.new(buffer, *args, **options) do |writer|
+ buffer.set_encoding(writer.encoding)
+ block.call(writer)
+ end
buffer.string
end
end
##
# Writes output to the given `filename`.
#
# @param [String, #to_s] filename
- # @param [Encoding, String, Symbol] encoding
- # the encoding to use on the output stream.
- # Defaults to the format associated with `content_encoding`.
# @param [Symbol] format (nil)
# @param [Hash{Symbol => Object}] options
# any additional options (see {RDF::Writer#initialize} and {RDF::Format.for})
# @return [RDF::Writer]
- def self.open(filename, encoding: nil, format: nil, **options, &block)
+ def self.open(filename, format: nil, **options, &block)
File.open(filename, 'wb') do |file|
- file.set_encoding(encoding) if encoding
format_options = options.dup
format_options[:file_name] ||= filename
- self.for(format || format_options).new(file, encoding: encoding, **options, &block)
+ self.for(format || format_options).new(file, **options) do |writer|
+ file.set_encoding(writer.encoding)
+ block.call(writer)
+ end
end
end
##
# Returns a symbol appropriate to use with RDF::Writer.for()