Class: NBTFile::Emitter

Inherits:
Object
  • Object
show all
Includes:
Private, Tokens
Defined in:
lib/nbtfile.rb

Constant Summary

Constants included from Private

Private::TOKEN_CLASSES_BY_INDEX, Private::TOKEN_INDICES_BY_CLASS

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ Emitter

:nodoc:



512
513
514
515
# File 'lib/nbtfile.rb', line 512

def initialize(stream) #:nodoc:
  @gz = Zlib::GzipWriter.new(stream)
  @state = TopEmitterState.new()
end

Instance Method Details

#emit_compound(name) ⇒ Object

Emit a TAG_Compound token, call the block, and then emit a matching TAG_End token.



524
525
526
527
528
529
530
531
# File 'lib/nbtfile.rb', line 524

def emit_compound(name) #:yields:
  emit_token(TAG_Compound[name, nil])
  begin
    yield
  ensure
    emit_token(TAG_End[nil, nil])
  end
end

#emit_item(value) ⇒ Object

Emits a list item, given a value (the token type is assumed based on the element type of the enclosing list).



546
547
548
# File 'lib/nbtfile.rb', line 546

def emit_item(value)
  @state = @state.emit_item(@gz, value)
end

#emit_list(name, type) ⇒ Object

Emit a TAG_List token, call the block, and then emit a matching TAG_End token.



535
536
537
538
539
540
541
542
# File 'lib/nbtfile.rb', line 535

def emit_list(name, type) #:yields:
  emit_token(TAG_List[name, type])
  begin
    yield
  ensure
    emit_token(TAG_End[nil, nil])
  end
end

#emit_token(token) ⇒ Object

Emit a token. See the Tokens module for a list of token types.



518
519
520
# File 'lib/nbtfile.rb', line 518

def emit_token(token)
  @state = @state.emit_token(@gz, token)
end

#finishObject

:nodoc:



550
551
552
# File 'lib/nbtfile.rb', line 550

def finish #:nodoc:
  @gz.close
end