Sha256: 1ccb2a220d95aa8b42d28b25ce1e22077fd9446ad9dc30106d55797ba3b0447c

Contents?: true

Size: 1007 Bytes

Versions: 2

Compression:

Stored size: 1007 Bytes

Contents

# encoding: UTF-8

require 'xml-write-stream/base'
require 'xml-write-stream/yielding_writer'
require 'xml-write-stream/stateful_writer'

class XmlWriteStream
  class InvalidAttributeKeyError < StandardError; end
  class InvalidTagNameError < StandardError; end
  class EndOfStreamError < StandardError; end
  class NoTopLevelTagError < StandardError; end
  class InvalidHeaderPositionError < StandardError; end

  DEFAULT_ENCODING = Encoding::UTF_8

  class << self
    def from_stream(stream, encoding = DEFAULT_ENCODING)
      stream.set_encoding(encoding)

      if block_given?
        yield writer = YieldingWriter.new(stream)
        writer.close
      else
        StatefulWriter.new(stream)
      end
    end

    def open(file, encoding = DEFAULT_ENCODING)
      handle = File.open(file, 'w')
      handle.set_encoding(encoding)

      if block_given?
        yield writer = YieldingWriter.new(handle)
        writer.close
      else
        StatefulWriter.new(handle)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
xml-write-stream-1.1.0 lib/xml-write-stream.rb
xml-write-stream-1.0.2 lib/xml-write-stream.rb