Class: Ciphr::Functions::ZLib::Gzip

Inherits:
InvertibleFunction show all
Defined in:
lib/ciphr/functions/zlib.rb

Defined Under Namespace

Classes: UncloseableIOProxy

Instance Attribute Summary

Attributes inherited from InvertibleFunction

#invert

Attributes inherited from Function

#args, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from InvertibleFunction

invertable?

Methods inherited from Function

aligned, inherited, #initialize, invertable?, #prepend, #read

Constructor Details

This class inherits a constructor from Ciphr::Functions::Function

Class Method Details

.paramsObject



91
92
93
# File 'lib/ciphr/functions/zlib.rb', line 91

def self.params 
  [:input]
end

.variantsObject



85
86
87
88
89
# File 'lib/ciphr/functions/zlib.rb', line 85

def self.variants
  [
    [['gzip','gz'], {}]
  ]
end

Instance Method Details

#applyObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ciphr/functions/zlib.rb', line 53

def apply
  input = @args[0]
  sio = StringIO.new
  sio.binmode
  gz = !invert ? Zlib::GzipWriter.new(UncloseableIOProxy.new(sio)) : Zlib::GzipReader.new(input) 
  Proc.new do
    if invert # unzip
      gz.read(256)
    else # zip
      chunk = input.read(256)
      if chunk
        gz.write chunk 
        sio.rewind
        ret = sio.read
        sio.rewind
        sio.truncate(0)
        ret
      elsif gz
        gz.close
        gz = nil
        sio.rewind
        ret = sio.read
        sio.rewind
        sio.truncate(0)
        ret
      else
        nil
      end
    end
  end
end