Class: XmlWriteStream::YieldingWriter
- Defined in:
- lib/xml-write-stream/yielding_writer.rb
Constant Summary
Constants inherited from Base
Base::ATTRIBUTE_ESCAPE_CHARS, Base::ATTRIBUTE_ESCAPE_HASH, Base::ATTRIBUTE_KEY_REGEX, Base::DEFAULT_HEADER_ATTRIBUTES, Base::DEFAULT_INDENT, Base::TAG_NAME_REGEX, Base::TEXT_ESCAPE_CHARS, Base::TEXT_ESCAPE_HASH
Instance Attribute Summary collapse
-
#indent ⇒ Object
readonly
Returns the value of attribute indent.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
Instance Method Summary collapse
- #close ⇒ Object
- #flush ⇒ Object
-
#initialize(stream, options = {}) ⇒ YieldingWriter
constructor
A new instance of YieldingWriter.
- #open_tag(tag_name, attributes = {}) {|_self| ... } ⇒ Object
- #write_header(attributes = {}) ⇒ Object
- #write_text(text, options = {}) ⇒ Object
Constructor Details
#initialize(stream, options = {}) ⇒ YieldingWriter
Returns a new instance of YieldingWriter.
7 8 9 10 11 |
# File 'lib/xml-write-stream/yielding_writer.rb', line 7 def initialize(stream, = {}) @stream = stream @level = 0 @indent = .fetch(:indent, Base::DEFAULT_INDENT) end |
Instance Attribute Details
#indent ⇒ Object (readonly)
Returns the value of attribute indent.
5 6 7 |
# File 'lib/xml-write-stream/yielding_writer.rb', line 5 def indent @indent end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
5 6 7 |
# File 'lib/xml-write-stream/yielding_writer.rb', line 5 def level @level end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
5 6 7 |
# File 'lib/xml-write-stream/yielding_writer.rb', line 5 def stream @stream end |
Instance Method Details
#close ⇒ Object
53 54 55 |
# File 'lib/xml-write-stream/yielding_writer.rb', line 53 def close stream.close end |
#flush ⇒ Object
50 51 |
# File 'lib/xml-write-stream/yielding_writer.rb', line 50 def flush end |
#open_tag(tag_name, attributes = {}) {|_self| ... } ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/xml-write-stream/yielding_writer.rb', line 13 def open_tag(tag_name, attributes = {}) check_closed check_tag_name(tag_name) check_attributes(attributes) stream.write(indent_spaces) write_open_tag(tag_name, attributes) write_newline @level += 1 yield self if block_given? @level -= 1 stream.write(indent_spaces) write_close_tag(tag_name) write_newline end |
#write_header(attributes = {}) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/xml-write-stream/yielding_writer.rb', line 41 def write_header(attributes = {}) if level > 0 raise InvalidHeaderPositionError, 'header must be the first element written.' end super end |
#write_text(text, options = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/xml-write-stream/yielding_writer.rb', line 29 def write_text(text, = {}) check_closed if level == 0 raise NoTopLevelTagError end stream.write(indent_spaces) super write_newline end |