Module: Madeleine::Automatic::Deserialize

Defined in:
lib/madeleine/automatic.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.detect(io) ⇒ Object

Detect format of an io stream. Leave it rewound.



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
# File 'lib/madeleine/automatic.rb', line 367

def Deserialize.detect(io)
  c = io.getc
  c1 = io.getc
  io.rewind
  if (c == Marshal::MAJOR_VERSION && c1 <= Marshal::MINOR_VERSION)
    Marshal
  elsif (c == 31 && c1 == 139) # gzip magic numbers
    ZMarshal
  else
    while (s = io.gets)
      break if (s !~ /^\s*$/) # ignore blank lines
    end
    io.rewind
    if (s && s =~ /^\s*<\?[xX][mM][lL]/) # "<?xml" begins an xml serialization
      SOAP::Marshal
    else
      while (s = io.gets)
        break if (s !~ /^\s*#/ && s !~ /^\s*$/) # ignore blank and comment lines
      end
      io.rewind
      if (s && s =~ /^\s*---/) # "---" is the yaml header
        YAML
      else
        nil # failed to detect
      end
    end
  end
end

.load(io, marshaller = Marshal) ⇒ Object

If detection didn’t work, raise up the exception



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/madeleine/automatic.rb', line 400

def Deserialize.load(io, marshaller=Marshal)
  begin
    raise "Must detect with YAML" if marshaller == YAML
    marshaller.load(io)
  rescue Exception => e
    io.rewind
    detected_marshaller = detect(io)
    if (detected_marshaller == ZMarshal)
      zio = Zlib::GzipReader.new(io)
      detected_zmarshaller = detect(zio)
      zio.finish
      io.rewind
      if (detected_zmarshaller)
        ZMarshal.new(detected_zmarshaller).load(io)
      else
        raise e
      end
    elsif (detected_marshaller)
      detected_marshaller.load(io)
    else
      raise e
    end
  end
end