Class: YamlWriteStream::StatefulWriter
- Inherits:
-
Object
- Object
- YamlWriteStream::StatefulWriter
show all
- Defined in:
- lib/yaml-write-stream/stateful.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(emitter, stream) ⇒ StatefulWriter
Returns a new instance of StatefulWriter.
12
13
14
15
16
17
18
19
|
# File 'lib/yaml-write-stream/stateful.rb', line 12
def initialize(emitter, stream)
@emitter = emitter
@stream = stream
@stack = []
@closed = false
after_initialize
@first = true
end
|
Instance Attribute Details
#closed ⇒ Object
Also known as:
closed?
Returns the value of attribute closed.
9
10
11
|
# File 'lib/yaml-write-stream/stateful.rb', line 9
def closed
@closed
end
|
#emitter ⇒ Object
Returns the value of attribute emitter.
9
10
11
|
# File 'lib/yaml-write-stream/stateful.rb', line 9
def emitter
@emitter
end
|
#first ⇒ Object
Returns the value of attribute first.
9
10
11
|
# File 'lib/yaml-write-stream/stateful.rb', line 9
def first
@first
end
|
#stack ⇒ Object
Returns the value of attribute stack.
9
10
11
|
# File 'lib/yaml-write-stream/stateful.rb', line 9
def stack
@stack
end
|
#stream ⇒ Object
Returns the value of attribute stream.
9
10
11
|
# File 'lib/yaml-write-stream/stateful.rb', line 9
def stream
@stream
end
|
Instance Method Details
#after_initialize ⇒ Object
21
22
23
|
# File 'lib/yaml-write-stream/stateful.rb', line 21
def after_initialize
@first = true
end
|
#close ⇒ Object
45
46
47
48
49
|
# File 'lib/yaml-write-stream/stateful.rb', line 45
def close
flush
stream.close
nil
end
|
#close_map ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/yaml-write-stream/stateful.rb', line 89
def close_map
if in_map?
stack.pop.close
else
raise NotInMapError, 'not currently writing a map.'
end
end
|
#close_sequence ⇒ Object
97
98
99
100
101
102
103
|
# File 'lib/yaml-write-stream/stateful.rb', line 97
def close_sequence
if in_sequence?
stack.pop.close
else
raise NotInSequenceError, 'not currently writing an sequence.'
end
end
|
#eos? ⇒ Boolean
77
78
79
|
# File 'lib/yaml-write-stream/stateful.rb', line 77
def eos?
closed? || (!first && stack.size == 0)
end
|
#flush ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/yaml-write-stream/stateful.rb', line 25
def flush
write_scalar('') if first
until stack.empty?
if in_map?
close_map
else
close_sequence
end
end
emitter.end_document(true)
emitter.end_stream
stream.flush
@closed = true
nil
end
|
#in_map? ⇒ Boolean
81
82
83
|
# File 'lib/yaml-write-stream/stateful.rb', line 81
def in_map?
current ? current.is_map? : false
end
|
#in_sequence? ⇒ Boolean
85
86
87
|
# File 'lib/yaml-write-stream/stateful.rb', line 85
def in_sequence?
current ? current.is_sequence? : false
end
|
#write_element(*args) ⇒ Object
71
72
73
74
75
|
# File 'lib/yaml-write-stream/stateful.rb', line 71
def write_element(*args)
check_eos
@first = false
current.write_element(*args)
end
|
#write_key_value(*args) ⇒ Object
65
66
67
68
69
|
# File 'lib/yaml-write-stream/stateful.rb', line 65
def write_key_value(*args)
check_eos
@first = false
current.write_key_value(*args)
end
|
#write_map(*args) ⇒ Object
51
52
53
54
55
56
|
# File 'lib/yaml-write-stream/stateful.rb', line 51
def write_map(*args)
check_eos
@first = false
current.write_map(*args) if current
stack.push(StatefulMappingWriter.new(emitter, stream))
end
|
#write_sequence(*args) ⇒ Object
58
59
60
61
62
63
|
# File 'lib/yaml-write-stream/stateful.rb', line 58
def write_sequence(*args)
check_eos
@first = false
current.write_sequence(*args) if current
stack.push(StatefulSequenceWriter.new(emitter, stream))
end
|