Class: Yutani::DirectoryTree
- Inherits:
-
Object
- Object
- Yutani::DirectoryTree
- 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
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Instance Method Summary collapse
- #add_file(path, permissions, content) ⇒ Object
-
#initialize(prefix = './') ⇒ DirectoryTree
constructor
A new instance of DirectoryTree.
- #to_fs ⇒ Object
- #to_tar(io = STDOUT) ⇒ Object
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
#files ⇒ Object (readonly)
Returns the value of attribute files.
11 12 13 |
# File 'lib/yutani/directory_tree.rb', line 11 def files @files end |
#prefix ⇒ Object (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, , content) @files << File.new(::File.join(@prefix, path), .to_i, content) end |
#to_fs ⇒ Object
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.) 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., f.content.bytes.size) do |tar_file| tar_file.write f.content end end end end |