Class: JsonWriteStream::StatefulWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/json-write-stream/stateful.rb

Direct Known Subclasses

StatefulArrayWriter, StatefulObjectWriter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ StatefulWriter

Returns a new instance of StatefulWriter.



12
13
14
15
16
17
18
# File 'lib/json-write-stream/stateful.rb', line 12

def initialize(stream)
  @stream = stream
  @index = 0
  @stack = []
  @closed = false
  after_initialize
end

Instance Attribute Details

#closedObject (readonly) Also known as: closed?

Returns the value of attribute closed.



9
10
11
# File 'lib/json-write-stream/stateful.rb', line 9

def closed
  @closed
end

#indexObject (readonly)

Returns the value of attribute index.



9
10
11
# File 'lib/json-write-stream/stateful.rb', line 9

def index
  @index
end

#stackObject (readonly)

Returns the value of attribute stack.



9
10
11
# File 'lib/json-write-stream/stateful.rb', line 9

def stack
  @stack
end

#streamObject (readonly)

Returns the value of attribute stream.



9
10
11
# File 'lib/json-write-stream/stateful.rb', line 9

def stream
  @stream
end

Instance Method Details

#after_initializeObject



20
21
# File 'lib/json-write-stream/stateful.rb', line 20

def after_initialize
end

#closeObject



78
79
80
81
82
# File 'lib/json-write-stream/stateful.rb', line 78

def close
  flush
  stream.close
  nil
end

#close_arrayObject



55
56
57
58
59
60
61
62
63
# File 'lib/json-write-stream/stateful.rb', line 55

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_objectObject



45
46
47
48
49
50
51
52
53
# File 'lib/json-write-stream/stateful.rb', line 45

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

Returns:

  • (Boolean)


92
93
94
# File 'lib/json-write-stream/stateful.rb', line 92

def eos?
  (stack.size == 0 && index > 0) || closed?
end

#flushObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/json-write-stream/stateful.rb', line 65

def flush
  until stack.empty?
    if in_object?
      close_object
    else
      close_array
    end
  end

  @closed = true
  nil
end

#in_array?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/json-write-stream/stateful.rb', line 88

def in_array?
  current ? current.is_array? : false
end

#in_object?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/json-write-stream/stateful.rb', line 84

def in_object?
  current ? current.is_object? : false
end

#write_array(*args) ⇒ Object



29
30
31
32
33
# File 'lib/json-write-stream/stateful.rb', line 29

def write_array(*args)
  check_eos
  current.write_array(*args) if current
  stack.push(StatefulArrayWriter.new(stream))
end

#write_element(*args) ⇒ Object



40
41
42
43
# File 'lib/json-write-stream/stateful.rb', line 40

def write_element(*args)
  check_eos
  current.write_element(*args)
end

#write_key_value(*args) ⇒ Object



35
36
37
38
# File 'lib/json-write-stream/stateful.rb', line 35

def write_key_value(*args)
  check_eos
  current.write_key_value(*args)
end

#write_object(*args) ⇒ Object



23
24
25
26
27
# File 'lib/json-write-stream/stateful.rb', line 23

def write_object(*args)
  check_eos
  current.write_object(*args) if current
  stack.push(StatefulObjectWriter.new(stream))
end