Class: CFBundle::Storage::Zip

Inherits:
Base
  • Object
show all
Defined in:
lib/cfbundle/storage/zip.rb

Overview

A bundle storage that reads from a ZIP archive.

Instance Method Summary collapse

Constructor Details

#initialize(zip, path, skip_close: false) ⇒ Zip

Returns a new instance of Zip.

Parameters:

  • zip (Zip::File)

    The Zip file containing the bundle.

  • path (String)

    The path of the bundle within the Zip file.

  • skip_close (Boolean) (defaults to: false)

    Whether the storage should skip closing the Zip file when receiving #close.



12
13
14
15
16
# File 'lib/cfbundle/storage/zip.rb', line 12

def initialize(zip, path, skip_close: false)
  @zip = zip
  @root = path
  @skip_close = skip_close
end

Instance Method Details

#closevoid

This method returns an undefined value.

Invoked when the storage is no longer needed.

This method closes the underlying Zip file unless the storage was initialized with skip_close: true.



58
59
60
# File 'lib/cfbundle/storage/zip.rb', line 58

def close
  @zip.close unless @skip_close
end

#directory?(path) ⇒ Object

Returns whether a given directory exists within the storage.

Parameters:

  • path (String)

    The path of a directory, relative to the storage.



30
31
32
33
# File 'lib/cfbundle/storage/zip.rb', line 30

def directory?(path)
  entry = find(path)
  !entry.nil? && entry.directory?
end

#exist?(path) ⇒ Object

Returns whether a given path exists within the storage.

Parameters:

  • path (String)

    The path of a file or directory, relative to the storage.



19
20
21
# File 'lib/cfbundle/storage/zip.rb', line 19

def exist?(path)
  find(path) != nil
end

#file?(path) ⇒ Object

Returns whether a given file exists within the storage.

Parameters:

  • path (String)

    The path of a file, relative to the storage.



24
25
26
27
# File 'lib/cfbundle/storage/zip.rb', line 24

def file?(path)
  entry = find(path)
  !entry.nil? && entry.file?
end

#foreach(path) ⇒ Enumerator

Returns an enumerator that enumerates the files contained in a directory.

Parameters:

  • path (String)

    The path to the directory to enumerate.

Returns:

  • (Enumerator)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cfbundle/storage/zip.rb', line 41

def foreach(path)
  Enumerator.new do |y|
    directory = find! path
    base = @zip.entries.sort.each
    loop do
      entry = base.next
      next unless entry.parent_as_string == directory.name
      y << PathUtils.join(path, File.basename(entry.name))
    end
  end
end

#open(path) {|file| ... } ⇒ Object, IO

Opens a file for reading in the storage.

Parameters:

  • path (String)

    The path of the file to open.

Yield Parameters:

  • file (IO)

    The opened file. It is automatically closed when the block terminates.

Returns:

  • (Object)

    The return value of the block when a block if given.

  • (IO)

    The opened file when no block is given.



36
37
38
# File 'lib/cfbundle/storage/zip.rb', line 36

def open(path, &block)
  find!(path).get_input_stream(&block)
end