Class: XmlWriteStream::YieldingWriter

Inherits:
Base
  • Object
show all
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

Instance Method Summary collapse

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, options = {})
  @stream = stream
  @level = 0
  @indent = options.fetch(:indent, Base::DEFAULT_INDENT)
end

Instance Attribute Details

#indentObject (readonly)

Returns the value of attribute indent.



5
6
7
# File 'lib/xml-write-stream/yielding_writer.rb', line 5

def indent
  @indent
end

#levelObject (readonly)

Returns the value of attribute level.



5
6
7
# File 'lib/xml-write-stream/yielding_writer.rb', line 5

def level
  @level
end

#streamObject (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

#closeObject



53
54
55
# File 'lib/xml-write-stream/yielding_writer.rb', line 53

def close
  stream.close
end

#flushObject



50
51
# File 'lib/xml-write-stream/yielding_writer.rb', line 50

def flush
end

#open_tag(tag_name, attributes = {}) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



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, options = {})
  check_closed

  if level == 0
    raise NoTopLevelTagError
  end

  stream.write(indent_spaces)
  super
  write_newline
end