Class: AndFeathers::Tarball

Inherits:
Object
  • Object
show all
Includes:
ContainsDirectories, ContainsFiles, Enumerable
Defined in:
lib/and_feathers/tarball.rb,
lib/and_feathers/tarball/file.rb,
lib/and_feathers/tarball/directory.rb,
lib/and_feathers/tarball/contains_files.rb,
lib/and_feathers/tarball/contains_directories.rb

Overview

The base tarball representation

Defined Under Namespace

Modules: ContainsDirectories, ContainsFiles Classes: Directory, File

Constant Summary

Constants included from ContainsFiles

ContainsFiles::NO_CONTENT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ContainsDirectories

#dir

Methods included from ContainsFiles

#file

Constructor Details

#initialize(path = '.') ⇒ Tarball

Creates a new Tarball

Parameters:

  • path (String) (defaults to: '.')

    the base tarball path



27
28
29
30
# File 'lib/and_feathers/tarball.rb', line 27

def initialize(path = '.')
  @path = path
  @children = []
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



20
21
22
# File 'lib/and_feathers/tarball.rb', line 20

def path
  @path
end

Instance Method Details

#each {|child| ... } ⇒ Object

Iterates through each entity in the tarball, depth-first

Yield Parameters:



37
38
39
40
41
42
# File 'lib/and_feathers/tarball.rb', line 37

def each(&block)
  @children.each do |child|
    block.call(child)
    child.each(&block)
  end
end

#to_ioStringIO

Returns this Tarball as a GZipped and tarred StringIO

Returns:

  • (StringIO)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/and_feathers/tarball.rb', line 49

def to_io
  tarball_io = StringIO.new("")

  Gem::Package::TarWriter.new(tarball_io) do |tar|
    each do |child|
      case child
      when File
        tar.add_file(child.path, child.mode) do |file|
          file.write child.read
        end
      when Directory
        tar.mkdir(child.path, child.mode)
      end
    end
  end

  gzip_io = StringIO.new("")

  Zlib::GzipWriter.new(gzip_io).tap do |writer|
    writer.write(tarball_io.tap(&:rewind).string)
    writer.close
  end

  StringIO.new(gzip_io.string)
end