Class: XmlWriteStream::StatefulWriter
- Inherits:
-
Base
- Object
- Base
- XmlWriteStream::StatefulWriter
show all
- Defined in:
- lib/xml-write-stream/stateful_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 = {}) ⇒ StatefulWriter
Returns a new instance of StatefulWriter.
19
20
21
22
23
24
25
|
# File 'lib/xml-write-stream/stateful_writer.rb', line 19
def initialize(stream, options = {})
@stream = stream
@stack = []
@closed = false
@index = 0
@indent = options.fetch(:indent, Base::DEFAULT_INDENT)
end
|
Instance Attribute Details
#closed ⇒ Object
Also known as:
closed?
Returns the value of attribute closed.
16
17
18
|
# File 'lib/xml-write-stream/stateful_writer.rb', line 16
def closed
@closed
end
|
#indent ⇒ Object
Returns the value of attribute indent.
16
17
18
|
# File 'lib/xml-write-stream/stateful_writer.rb', line 16
def indent
@indent
end
|
#index ⇒ Object
Returns the value of attribute index.
16
17
18
|
# File 'lib/xml-write-stream/stateful_writer.rb', line 16
def index
@index
end
|
#stack ⇒ Object
Returns the value of attribute stack.
16
17
18
|
# File 'lib/xml-write-stream/stateful_writer.rb', line 16
def stack
@stack
end
|
#stream ⇒ Object
Returns the value of attribute stream.
16
17
18
|
# File 'lib/xml-write-stream/stateful_writer.rb', line 16
def stream
@stream
end
|
Instance Method Details
#close ⇒ Object
92
93
94
95
96
|
# File 'lib/xml-write-stream/stateful_writer.rb', line 92
def close
flush
stream.close
nil
end
|
#close_tag(options = {}) ⇒ Object
77
78
79
80
81
82
83
84
|
# File 'lib/xml-write-stream/stateful_writer.rb', line 77
def close_tag(options = {})
if in_tag?
stack_item = stack.pop
stream.write(indent_spaces) if stack_item.multiline?
write_close_tag(stack_item.tag_name)
write_newline if current && current.multiline?
end
end
|
#current ⇒ Object
106
107
108
|
# File 'lib/xml-write-stream/stateful_writer.rb', line 106
def current
stack.last
end
|
#eos? ⇒ Boolean
102
103
104
|
# File 'lib/xml-write-stream/stateful_writer.rb', line 102
def eos?
(stack.size == 0 && index > 0) || closed?
end
|
#flush ⇒ Object
86
87
88
89
90
|
# File 'lib/xml-write-stream/stateful_writer.rb', line 86
def flush
close_tag until stack.empty?
@closed = true
nil
end
|
#in_tag? ⇒ Boolean
98
99
100
|
# File 'lib/xml-write-stream/stateful_writer.rb', line 98
def in_tag?
stack.size > 0 && !closed?
end
|
#open_single_line_tag(tag_name, attributes = {}) ⇒ Object
31
32
33
|
# File 'lib/xml-write-stream/stateful_writer.rb', line 31
def open_single_line_tag(tag_name, attributes = {})
open_tag_helper(tag_name, false, attributes)
end
|
#open_tag(tag_name, attributes = {}) ⇒ Object
27
28
29
|
# File 'lib/xml-write-stream/stateful_writer.rb', line 27
def open_tag(tag_name, attributes = {})
open_tag_helper(tag_name, true, attributes)
end
|
#open_tag_helper(tag_name, multiline, attributes = {}) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/xml-write-stream/stateful_writer.rb', line 35
def open_tag_helper(tag_name, multiline, attributes = {})
check_eos
if index == 0
stack.push(StackItem.new(tag_name, multiline))
end
check_tag_name(tag_name)
check_attributes(attributes)
stream.write(indent_spaces) if index > 0 && current.multiline?
write_open_tag(tag_name, attributes)
write_newline if multiline
if index > 0
stack.push(StackItem.new(tag_name, multiline))
end
@index += 1
end
|
68
69
70
71
72
73
74
75
|
# File 'lib/xml-write-stream/stateful_writer.rb', line 68
def (attributes = {})
if stack.size > 0
raise InvalidHeaderPositionError,
'header must be the first element written.'
end
super
end
|
#write_text(text, options = {}) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/xml-write-stream/stateful_writer.rb', line 55
def write_text(text, options = {})
check_eos
if stack.size == 0
raise NoTopLevelTagError
end
stream.write(indent_spaces) if current.multiline?
super
write_newline if current.multiline?
end
|