Class: Yutani::DirectoryTree

Inherits:
Object
  • Object
show all
Defined in:
lib/yutani/directory_tree.rb

Overview

An abstraction of a real directory tree on disk Permits us to decide later whether this will be written to disk or embedded in a tarball, or sent over scp, etc.

Defined Under Namespace

Classes: File

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix = './') ⇒ DirectoryTree

Returns a new instance of DirectoryTree.



13
14
15
16
# File 'lib/yutani/directory_tree.rb', line 13

def initialize(prefix = './')
  @prefix = prefix
  @files = []
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



11
12
13
# File 'lib/yutani/directory_tree.rb', line 11

def files
  @files
end

#prefixObject (readonly)

Returns the value of attribute prefix.



11
12
13
# File 'lib/yutani/directory_tree.rb', line 11

def prefix
  @prefix
end

Instance Method Details

#add_file(path, permissions, content) ⇒ Object



18
19
20
# File 'lib/yutani/directory_tree.rb', line 18

def add_file(path, permissions, content)
  @files << File.new(::File.join(@prefix, path), permissions.to_i, content)
end

#to_fsObject



22
23
24
25
26
27
28
29
# File 'lib/yutani/directory_tree.rb', line 22

def to_fs
  @files.each do |f|
    FileUtils.mkdir_p(::File.dirname(f.path))
    ::File.open(f.path, 'w+', f.permissions) do |new_f|
      new_f.write f.content
    end
  end
end

#to_tar(io = STDOUT) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/yutani/directory_tree.rb', line 31

def to_tar(io = STDOUT)
  Gem::Package::TarWriter.new(io) do |tar|
    @files.each do |f|
      tar.mkdir(::File.dirname(f.path), '0755')

      tar.add_file_simple(f.path, f.permissions, f.content.bytes.size) do |tar_file|
        tar_file.write f.content
      end
    end
  end
end