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

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

Overview

Zip file serializer. Dumps/loads contents from ‘.zip` files. Automatically detects if zip file contains a `.marshal` or `.yml` file, or any other registered `:format => serializer` combo.

Instance Method Summary collapse

Constructor Details

#initialize(properties) ⇒ Zip

Creates a new Zip serializer.

Parameters:



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

def initialize properties
  super()
  @properties = properties
end

Instance Method Details

#dump(contents, filepath) ⇒ TContents

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:

  • (TContents)

    number of bytes written to disk.

See Also:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rambling/trie/serializers/zip.rb', line 48

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

    raise unless serializer

    serializer.dump contents, entry_path

    zip.add filename, entry_path
  end

  ::File.size filepath
end

#load(filepath) ⇒ TContents

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

Parameters:

  • filepath (String)

    the filepath to load contents from.

Returns:

  • (TContents)

    all contents of the unzipped loaded file.

See Also:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rambling/trie/serializers/zip.rb', line 24

def load filepath
  require 'zip'

  ::Zip::File.open filepath do |zip|
    entry = zip.entries.first
    raise unless entry

    entry_name = entry.name
    entry_path = path entry_name
    entry.extract entry_path

    serializer = serializers.resolve entry_name
    raise unless serializer

    serializer.load entry_path
  end
end