Class: YamlWriteStream::StatefulWriter

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

#closedObject (readonly) 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

#emitterObject (readonly)

Returns the value of attribute emitter.



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

def emitter
  @emitter
end

#firstObject (readonly)

Returns the value of attribute first.



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

def first
  @first
end

#stackObject (readonly)

Returns the value of attribute stack.



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

def stack
  @stack
end

#streamObject (readonly)

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_initializeObject



21
22
23
# File 'lib/yaml-write-stream/stateful.rb', line 21

def after_initialize
  @first = true
end

#closeObject



45
46
47
48
49
# File 'lib/yaml-write-stream/stateful.rb', line 45

def close
  flush
  stream.close
  nil
end

#close_mapObject



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_sequenceObject



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

Returns:

  • (Boolean)


77
78
79
# File 'lib/yaml-write-stream/stateful.rb', line 77

def eos?
  closed? || (!first && stack.size == 0)
end

#flushObject



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
  # psych gets confused if you open a file and don't at least
  # pretend to write something
  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

Returns:

  • (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

Returns:

  • (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