class Rambling::Trie::Serializers::Zip

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.

Attributes

properties[R]

Public Class Methods

new(properties) click to toggle source

Creates a new Zip serializer. @param [Configuration::Properties] properties the configuration

properties set up so far.
Calls superclass method
# File lib/rambling/trie/serializers/zip.rb, line 13
def initialize properties
  @properties = properties
  super()
end

Public Instance Methods

dump(contents, filepath) click to toggle source

Dumps contents and zips into a specified filepath. @param [String] contents the contents to dump. @param [String] filepath the filepath to dump the contents to. @return [TContents] number of bytes written to disk. @see github.com/rubyzip/rubyzip#basic-zip-archive-creation

Zip archive creation
# File lib/rambling/trie/serializers/zip.rb, line 43
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

  ::File.size filepath
end
load(filepath) click to toggle source

Unzip contents from specified filepath and load in contents from unzipped files. @param [String] filepath the filepath to load contents from. @return [TContents] all contents of the unzipped loaded file. @see github.com/rubyzip/rubyzip#reading-a-zip-file Zip

reading a file
# File lib/rambling/trie/serializers/zip.rb, line 24
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

Private Instance Methods

path(filename) click to toggle source
# File lib/rambling/trie/serializers/zip.rb, line 71
def path filename
  require 'securerandom'
  ::File.join tmp_path, "#{SecureRandom.uuid}-#{filename}"
end
serializers() click to toggle source
# File lib/rambling/trie/serializers/zip.rb, line 63
def serializers
  properties.serializers
end
tmp_path() click to toggle source
# File lib/rambling/trie/serializers/zip.rb, line 67
def tmp_path
  properties.tmp_path
end