Class: JsonWriteStream::StatefulWriter
- Inherits:
-
Object
- Object
- JsonWriteStream::StatefulWriter
- Defined in:
- lib/json-write-stream/stateful.rb
Instance Attribute Summary collapse
-
#closed ⇒ Object
(also: #closed?)
readonly
Returns the value of attribute closed.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
Instance Method Summary collapse
- #close ⇒ Object
- #close_array ⇒ Object
- #close_object ⇒ Object
- #eos? ⇒ Boolean
- #flush ⇒ Object
- #in_array? ⇒ Boolean
- #in_object? ⇒ Boolean
- #indent_size ⇒ Object
-
#initialize(stream, options = {}) ⇒ StatefulWriter
constructor
A new instance of StatefulWriter.
- #pretty? ⇒ Boolean
- #write_array(*args) ⇒ Object
- #write_element(*args) ⇒ Object
- #write_key_value(*args) ⇒ Object
- #write_object(*args) ⇒ Object
Constructor Details
#initialize(stream, options = {}) ⇒ StatefulWriter
Returns a new instance of StatefulWriter.
14 15 16 17 18 19 20 |
# File 'lib/json-write-stream/stateful.rb', line 14 def initialize(stream, = {}) @stream = stream @stack = [] @closed = false @options = @index = 0 end |
Instance Attribute Details
#closed ⇒ Object (readonly) Also known as: closed?
Returns the value of attribute closed.
11 12 13 |
# File 'lib/json-write-stream/stateful.rb', line 11 def closed @closed end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
11 12 13 |
# File 'lib/json-write-stream/stateful.rb', line 11 def index @index end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/json-write-stream/stateful.rb', line 11 def @options end |
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
11 12 13 |
# File 'lib/json-write-stream/stateful.rb', line 11 def stack @stack end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
11 12 13 |
# File 'lib/json-write-stream/stateful.rb', line 11 def stream @stream end |
Instance Method Details
#close ⇒ Object
90 91 92 93 94 |
# File 'lib/json-write-stream/stateful.rb', line 90 def close flush stream.close nil end |
#close_array ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/json-write-stream/stateful.rb', line 67 def close_array if in_array? stack.pop.close current.increment if current increment else raise NotInArrayError, 'not currently writing an array.' end end |
#close_object ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/json-write-stream/stateful.rb', line 57 def close_object if in_object? stack.pop.close current.increment if current increment else raise NotInObjectError, 'not currently writing an object.' end end |
#eos? ⇒ Boolean
104 105 106 |
# File 'lib/json-write-stream/stateful.rb', line 104 def eos? (stack.size == 0 && index > 0) || closed? end |
#flush ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/json-write-stream/stateful.rb', line 77 def flush until stack.empty? if in_object? close_object else close_array end end @closed = true nil end |
#in_array? ⇒ Boolean
100 101 102 |
# File 'lib/json-write-stream/stateful.rb', line 100 def in_array? current ? current.is_array? : false end |
#in_object? ⇒ Boolean
96 97 98 |
# File 'lib/json-write-stream/stateful.rb', line 96 def in_object? current ? current.is_object? : false end |
#indent_size ⇒ Object
112 113 114 |
# File 'lib/json-write-stream/stateful.rb', line 112 def indent_size .fetch(:indent_size, 2) end |
#pretty? ⇒ Boolean
108 109 110 |
# File 'lib/json-write-stream/stateful.rb', line 108 def pretty? .fetch(:pretty, false) end |
#write_array(*args) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/json-write-stream/stateful.rb', line 34 def write_array(*args) check_eos new_indent_level = 1 if current current.write_array(*args) new_indent_level = current.indent_level + 1 end stack.push(StatefulArrayWriter.new(self, new_indent_level)) end |
#write_element(*args) ⇒ Object
52 53 54 55 |
# File 'lib/json-write-stream/stateful.rb', line 52 def write_element(*args) check_eos current.write_element(*args) end |
#write_key_value(*args) ⇒ Object
47 48 49 50 |
# File 'lib/json-write-stream/stateful.rb', line 47 def write_key_value(*args) check_eos current.write_key_value(*args) end |
#write_object(*args) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/json-write-stream/stateful.rb', line 22 def write_object(*args) check_eos new_indent_level = 1 if current current.write_object(*args) new_indent_level = current.indent_level + 1 end stack.push(StatefulObjectWriter.new(self, new_indent_level)) end |