class Rambling::Trie::Serializers::Zip

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

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.
# File lib/rambling/trie/serializers/zip.rb, line 12
def initialize properties
  @properties = properties
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 [Numeric] 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 41
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) 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 [String] 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 22
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 67
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 59
def serializers
  properties.serializers
end
tmp_path() click to toggle source
# File lib/rambling/trie/serializers/zip.rb, line 63
def tmp_path
  properties.tmp_path
end