Class: Rambling::Trie::Serializers::Zip
- Inherits:
-
Serializer
- Object
- Serializer
- Rambling::Trie::Serializers::Zip
- Defined in:
- lib/rambling/trie/serializers/zip.rb,
sig/lib/rambling/trie/serializers/zip.rbs
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 Attribute Summary collapse
-
#properties ⇒ Configuration::Properties[TValue]
readonly
Returns the value of attribute properties.
Instance Method Summary collapse
- #clean_up_tmp_files ⇒ void
-
#dump(contents, filepath) ⇒ Integer
Dumps contents and zips into a specified filepath.
-
#initialize(properties) ⇒ Zip
constructor
Creates a new Zip serializer.
-
#load(filepath) ⇒ Nodes::Node
Unzip contents from specified filepath and load in contents from unzipped files.
- #path_with_random_prefix(filename) ⇒ String
- #serializers ⇒ Configuration::ProviderCollection[Serializer[Nodes::Node[TValue]]]
- #tmp_path ⇒ String
Constructor Details
#initialize(properties) ⇒ Zip
Creates a new Zip serializer.
13 14 15 16 |
# File 'lib/rambling/trie/serializers/zip.rb', line 13 def initialize properties super() @properties = properties end |
Instance Attribute Details
#properties ⇒ Configuration::Properties[TValue] (readonly)
Returns the value of attribute properties.
69 70 71 |
# File 'lib/rambling/trie/serializers/zip.rb', line 69 def properties @properties end |
Instance Method Details
#clean_up_tmp_files ⇒ void
This method returns an undefined value.
84 85 86 87 88 89 90 91 92 |
# File 'lib/rambling/trie/serializers/zip.rb', line 84 def clean_up_tmp_files # @type var tmp_paths: Array[String] tmp_paths = [] begin yield tmp_paths ensure tmp_paths.each { |path| ::FileUtils.rm_f path } end end |
#dump(contents, filepath) ⇒ Integer
Dumps contents and zips into a specified filepath.
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 49 def dump contents, filepath require 'zip' clean_up_tmp_files do |tmp_paths| ::Zip::File.open filepath, create: true do |zip| filename = ::File.basename filepath, '.zip' entry_path = path_with_random_prefix filename tmp_paths << entry_path (serializers.resolve(filename) || raise).dump contents, entry_path zip.add filename, entry_path end end ::File.size filepath end |
#load(filepath) ⇒ Nodes::Node
Unzip contents from specified filepath and load in contents from unzipped files.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rambling/trie/serializers/zip.rb', line 24 def load filepath require 'zip' clean_up_tmp_files do |tmp_paths| ::Zip::File.open filepath do |zip| entry = zip.entries.first raise unless entry entry_name = entry.name entry_path = path_with_random_prefix entry_name tmp_paths << entry_path entry.extract ::File.basename(entry_path), destination_directory: tmp_path (serializers.resolve(entry_name) || raise).load entry_path end end end |
#path_with_random_prefix(filename) ⇒ String
79 80 81 82 |
# File 'lib/rambling/trie/serializers/zip.rb', line 79 def path_with_random_prefix filename require 'securerandom' ::File.join tmp_path, "#{SecureRandom.uuid}-#{filename}" end |
#serializers ⇒ Configuration::ProviderCollection[Serializer[Nodes::Node[TValue]]]
71 72 73 |
# File 'lib/rambling/trie/serializers/zip.rb', line 71 def serializers properties.serializers end |
#tmp_path ⇒ String
75 76 77 |
# File 'lib/rambling/trie/serializers/zip.rb', line 75 def tmp_path properties.tmp_path end |