Class: Boss::Parser

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/boss-protocol.rb

Overview

Parser implements IO-like behavior and provides deserializing of BOSS objects. Parser can store multiple root objects and share same object cache for all them

Instance Method Summary collapse

Constructor Details

#initialize(src = nil) ⇒ Parser

construct parser to read from the given string or IO-like object



323
324
325
326
327
328
# File 'lib/boss-protocol.rb', line 323

def initialize(src=nil)
  @io = src.class <= String ? StringIO.new(src) : src
  @io.set_encoding Encoding::BINARY
  @cache = [nil]
  @stream_mode = false
end

Instance Method Details

#eachObject

yields all objects in the stream



411
412
413
# File 'lib/boss-protocol.rb', line 411

def each
  yield get until eof?
end

#eof?Boolean

True is underlying IO-like object reached its end.

Returns:

  • (Boolean)


405
406
407
# File 'lib/boss-protocol.rb', line 405

def eof?
  @io.eof?
end

#getObject

Load the object (object tree) from the stream. Note that if there is more than one object in the stream that are stored with the same Formatter instance, they will share same cache and references, see Boss.Formatter.put for details. Note that nil is a valid restored object. Check eof? or catch EOFError, or use Boss.Parser.each to read all objects from the stream



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/boss-protocol.rb', line 337

def get
  code, value = rhdr
  case code
    when TYPE_INT
      value
    when TYPE_NINT
      -value
    when TYPE_EXTRA
      case value
        when TTRUE
          true
        when TFALSE
          false
        when DZERO, FZERO
          0
        when DONE, FONE
          1.0
        when DMINUSONE, FMINUSONE
          -1.0
        when TDOUBLE
          rdouble
        when TCOMPRESSED
          type, size = rhdr
          data       = rbin size
          case type
            when 0
              Boss.load data
            when 1
              Boss.load Zlib::Inflate.new(Zlib::MAX_WBITS).inflate(data)
            #when 2
            #  Boss.load Bzip2.uncompress data
            else
              raise UnknownTypeException, "type #{type}"
          end
        when TTIME
          Time.at renc
        when XT_STREAM_MODE
          @cache = [nil]
          @stream_mode = true
          get
        else
          raise UnknownTypeException
      end
    when TYPE_TEXT, TYPE_BIN
      s = rbin value
      s.force_encoding code == TYPE_BIN ? Encoding::BINARY : Encoding::UTF_8
      cache_object s
      s
    when TYPE_LIST
      #        p "items", value
      cache_object (list = [])
      value.times { list << get }
      list
    when TYPE_DICT
      cache_object (dict = {})
      value.times { dict[get] = get }
      dict
    when TYPE_CREF
      x = @cache[value]
      x.freeze if x.is_a?(String)
      x
    else
      raise UnknownTypeException
  end
end