Class: YamlWriteStream::YieldingWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/yaml-write-stream/yielding.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(emitter, stream) ⇒ YieldingWriter

Returns a new instance of YieldingWriter.



8
9
10
11
12
13
# File 'lib/yaml-write-stream/yielding.rb', line 8

def initialize(emitter, stream)
  @emitter = emitter
  @stream = stream
  @first = true
  @closed = false
end

Instance Attribute Details

#closedObject (readonly) Also known as: closed?

Returns the value of attribute closed.



5
6
7
# File 'lib/yaml-write-stream/yielding.rb', line 5

def closed
  @closed
end

#emitterObject (readonly)

Returns the value of attribute emitter.



5
6
7
# File 'lib/yaml-write-stream/yielding.rb', line 5

def emitter
  @emitter
end

#firstObject (readonly)

Returns the value of attribute first.



5
6
7
# File 'lib/yaml-write-stream/yielding.rb', line 5

def first
  @first
end

#streamObject (readonly)

Returns the value of attribute stream.



5
6
7
# File 'lib/yaml-write-stream/yielding.rb', line 5

def stream
  @stream
end

Instance Method Details

#closeObject



24
25
26
27
28
29
# File 'lib/yaml-write-stream/yielding.rb', line 24

def close
  flush
  stream.close
  @closed = true
  nil
end

#flushObject



15
16
17
18
19
20
21
22
# File 'lib/yaml-write-stream/yielding.rb', line 15

def flush
  # psych gets confused if you open a file and don't at least
  # pretend to write something
  write_scalar('') if first
  emitter.end_document(true)
  emitter.end_stream
  nil
end

#write_map {|YieldingMappingWriter.new(emitter, stream)| ... } ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/yaml-write-stream/yielding.rb', line 43

def write_map
  @first = false

  # anchor, tag, implicit, style
  emitter.start_mapping(
    nil, nil, false, Psych::Nodes::Sequence::ANY
  )

  yield YieldingMappingWriter.new(emitter, stream)
  emitter.end_mapping
end

#write_sequence {|YieldingSequenceWriter.new(emitter, stream)| ... } ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/yaml-write-stream/yielding.rb', line 31

def write_sequence
  @first = false

  # anchor, tag, implicit, style
  emitter.start_sequence(
    nil, nil, false, Psych::Nodes::Sequence::ANY
  )

  yield YieldingSequenceWriter.new(emitter, stream)
  emitter.end_sequence
end