lib/rdf/writer.rb in rdf-1.0.4 vs lib/rdf/writer.rb in rdf-1.0.5

- old
+ new

@@ -103,17 +103,22 @@ end ## # @param [RDF::Enumerable, #each] data # the graph or repository to dump - # @param [IO, File] io + # @param [IO, File, String] io # the output stream or file to write to # @param [Hash{Symbol => Object}] options # passed to {RDF::Writer#initialize} or {RDF::Writer.buffer} # @return [void] def self.dump(data, io = nil, options = {}) - io = File.open(io, 'w') if io.is_a?(String) + if io.is_a?(String) + io = File.open(io, 'w') + elsif io.respond_to?(:external_encoding) && io.external_encoding + options = {:encoding => io.external_encoding}.merge(options) + end + io.set_encoding(options[:encoding]) if io.respond_to?(:set_encoding) && options[:encoding] method = data.respond_to?(:each_statement) ? :each_statement : :each if io new(io, options) do |writer| data.send(method) do |statement| writer << statement @@ -136,13 +141,15 @@ # @yieldparam [RDF::Writer] writer # @yieldreturn [void] # @return [String] # @raise [ArgumentError] if no block is provided def self.buffer(*args, &block) + options = args.last.is_a?(Hash) ? args.last : {} raise ArgumentError, "block expected" unless block_given? StringIO.open do |buffer| + buffer.set_encoding(options[:encoding]) if buffer.respond_to?(:set_encoding) && options[:encoding] self.new(buffer, *args) { |writer| block.call(writer) } buffer.string end end @@ -154,10 +161,11 @@ # any additional options (see {RDF::Writer#initialize} and {RDF::Format.for}) # @option options [Symbol] :format (nil) # @return [RDF::Writer] def self.open(filename, options = {}, &block) File.open(filename, 'wb') do |file| + file.set_encoding(options[:encoding]) if file.respond_to?(:set_encoding) && options[:encoding] format_options = options.dup format_options[:file_name] ||= filename self.for(options[:format] || format_options).new(file, options, &block) end end @@ -437,10 +445,10 @@ protected ## # @return [void] def puts(*args) - @output.puts(*args) + @output.puts(*args.map {|s| s.respond_to?(:force_encoding) ? s.force_encoding(encoding) : s}) end ## # @param [RDF::Resource] uriref # @return [String]