Class: Rambling::Trie::Serializers::Zip

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rambling/trie/serializers/zip.rb

Overview

Zip file serializer. Dumps/loads contents from zip files. Automatically detects if zip file contains `.marshal` or `.yml` file

Instance Method Summary collapse

Constructor Details

#initialize(properties) ⇒ Zip

Creates a new Zip serializer.

Parameters:

  • properties (Properties)

    the configuration properties set up so far.


12
13
14
# File 'lib/rambling/trie/serializers/zip.rb', line 12

def initialize properties
  @properties = properties
end

Instance Method Details

#dump(contents, filepath) ⇒ Numeric

Dumps contents and zips into a specified filepath.

Parameters:

  • contents (String)

    the contents to dump.

  • filepath (String)

    the filepath to dump the contents to.

Returns:

  • (Numeric)

    number of bytes written to disk.


37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rambling/trie/serializers/zip.rb', line 37

def dump contents, filepath
  require 'zip'

  ::Zip::File.open filepath, ::Zip::File::CREATE do |zip|
    filename = ::File.basename filepath, '.zip'

    entry_path = path filename
    serializer = serializers.resolve filename
    serializer.dump contents, entry_path

    zip.add filename, entry_path
  end
end

#load(filepath) ⇒ String

Unzip contents from specified filepath and load in contents from unzipped files.

Parameters:

  • filepath (String)

    the filepath to load contents from.

Returns:

  • (String)

    all contents of the unzipped loaded file.


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rambling/trie/serializers/zip.rb', line 20

def load filepath
  require 'zip'

  ::Zip::File.open filepath do |zip|
    entry = zip.entries.first
    entry_path = path entry.name
    entry.extract entry_path

    serializer = serializers.resolve entry.name
    serializer.load entry_path
  end
end