Class: Cotta::CottaDir
- Inherits:
-
Object
- Object
- Cotta::CottaDir
- Defined in:
- lib/cotta/cotta_dir.rb
Overview
This class represents a directory
Instance Attribute Summary collapse
-
#factory ⇒ Object
readonly
file factory of the directory.
-
#path ⇒ Object
readonly
Path of the directory.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#archive(target = nil, &block) ⇒ Object
archive this directory and call the given block to determine if a file or directory should be included.
- #chdir(&block) ⇒ Object
-
#copy_to(target) ⇒ Object
copy this directory to target directory this method assumes that this directory and the target directory are backed by the same file system.
- #copy_to_path(target_path) ⇒ Object
-
#delete ⇒ Object
deletes this directory and all its children.
-
#dir(name) ⇒ Object
returns the sub-directory with the given name.
-
#exists? ⇒ Boolean
returns true if this directory exists.
-
#file(name) ⇒ Object
returns the file under this directory with the given name.
-
#initialize(factory, path) ⇒ CottaDir
constructor
Create an instance of CottaDir that is on the given path and backed by the given system.
- #inspect ⇒ Object
-
#list ⇒ Object
returns the content of this directory as an array of CottaFile and CottaDirectory.
-
#mkdirs ⇒ Object
creates this directory and its parent directory.
-
#move_to(target) ⇒ Object
move this directory to target directory this method assumes that this directory and the target directory are backed by the same file system.
-
#move_to_path(target_path) ⇒ Object
move this directory to target path this method assumes that this directory and the target directory are backed by the same file system.
-
#name ⇒ Object
name of the directory.
-
#parent ⇒ Object
returns the parent directory of this directory or nil if this is root.
-
#relative_path_from(entry) ⇒ Object
returns the relative path from the given file or directory.
-
#root? ⇒ Boolean
returns true if this directory is the root directory.
-
#stat ⇒ Object
returns the stat of the current directory.
- #to_s ⇒ Object
Constructor Details
#initialize(factory, path) ⇒ CottaDir
Create an instance of CottaDir that is on the given path and backed by the given system
13 14 15 16 17 |
# File 'lib/cotta/cotta_dir.rb', line 13 def initialize(factory, path) @path = path @factory = factory @name = @path.basename.to_s end |
Instance Attribute Details
#factory ⇒ Object (readonly)
file factory of the directory
8 9 10 |
# File 'lib/cotta/cotta_dir.rb', line 8 def factory @factory end |
#path ⇒ Object (readonly)
Path of the directory
5 6 7 |
# File 'lib/cotta/cotta_dir.rb', line 5 def path @path end |
Instance Method Details
#==(other) ⇒ Object
171 172 173 |
# File 'lib/cotta/cotta_dir.rb', line 171 def ==(other) return @path == other.path && factory.system == other.factory.system end |
#archive(target = nil, &block) ⇒ Object
archive this directory and call the given block to determine if a file or directory should be included
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/cotta/cotta_dir.rb', line 115 def archive(target = nil, &block) require 'rubygems/package' unless target target = parent.file("#{name}.tar") end target.write_binary do |io| writer = Gem::Package::TarWriter.new(io) do |tar_io| archive_dir(tar_io, self, &block) end end target end |
#chdir(&block) ⇒ Object
167 168 169 |
# File 'lib/cotta/cotta_dir.rb', line 167 def chdir(&block) factory.system.chdir(@path, &block) end |
#copy_to(target) ⇒ Object
copy this directory to target directory this method assumes that this directory and the target directory are backed by the same file system
108 109 110 111 |
# File 'lib/cotta/cotta_dir.rb', line 108 def copy_to(target) target.parent.mkdirs factory.system.copy_dir(@path, target.path) end |
#copy_to_path(target_path) ⇒ Object
151 152 153 |
# File 'lib/cotta/cotta_dir.rb', line 151 def copy_to_path(target_path) copy_to(cotta.dir(target_path)) end |
#delete ⇒ Object
deletes this directory and all its children
81 82 83 84 85 86 87 88 |
# File 'lib/cotta/cotta_dir.rb', line 81 def delete if (exists?) list.each do |children| children.delete end factory.system.delete_dir(@path) end end |
#dir(name) ⇒ Object
returns the sub-directory with the given name
63 64 65 |
# File 'lib/cotta/cotta_dir.rb', line 63 def dir(name) return CottaDir.new(factory, @path.join(name)) end |
#exists? ⇒ Boolean
returns true if this directory exists
36 37 38 |
# File 'lib/cotta/cotta_dir.rb', line 36 def exists? factory.system.dir_exists?(@path) end |
#file(name) ⇒ Object
returns the file under this directory with the given name
68 69 70 |
# File 'lib/cotta/cotta_dir.rb', line 68 def file(name) return CottaFile.new(factory, @path.join(name)) end |
#inspect ⇒ Object
175 176 177 |
# File 'lib/cotta/cotta_dir.rb', line 175 def inspect return "#{self.class}:#{self.object_id}-#@path" end |
#list ⇒ Object
returns the content of this directory as an array of CottaFile and CottaDirectory
157 158 159 160 161 162 163 164 165 |
# File 'lib/cotta/cotta_dir.rb', line 157 def list factory.system.list(@path).collect do |item| candidate = dir(item) if (not candidate.exists?) candidate = file(item) end candidate end end |
#mkdirs ⇒ Object
creates this directory and its parent directory
73 74 75 76 77 78 |
# File 'lib/cotta/cotta_dir.rb', line 73 def mkdirs if (not exists?) parent.mkdirs factory.system.mkdir @path end end |
#move_to(target) ⇒ Object
move this directory to target directory this method assumes that this directory and the target directory are backed by the same file system
93 94 95 96 |
# File 'lib/cotta/cotta_dir.rb', line 93 def move_to(target) target.parent.mkdirs factory.system.move_dir(@path, target.path) end |
#move_to_path(target_path) ⇒ Object
move this directory to target path this method assumes that this directory and the target directory are backed by the same file system
101 102 103 |
# File 'lib/cotta/cotta_dir.rb', line 101 def move_to_path(target_path) move_to(cotta.dir(target_path)) end |
#name ⇒ Object
name of the directory
20 21 22 23 24 25 26 27 28 |
# File 'lib/cotta/cotta_dir.rb', line 20 def name name = nil if root? name = path.to_s else name = @path.basename.to_s end return name end |
#parent ⇒ Object
returns the parent directory of this directory or nil if this is root
47 48 49 50 51 52 53 54 55 |
# File 'lib/cotta/cotta_dir.rb', line 47 def parent parent_path = @path.cotta_parent return nil unless parent_path candidate = CottaDir.new(factory, parent_path) if (block_given?) candidate = candidate.parent until candidate.nil? or yield candidate end candidate end |
#relative_path_from(entry) ⇒ Object
returns the relative path from the given file or directory
58 59 60 |
# File 'lib/cotta/cotta_dir.rb', line 58 def relative_path_from(entry) @path.relative_path_from(entry.path) end |
#root? ⇒ Boolean
returns true if this directory is the root directory
31 32 33 |
# File 'lib/cotta/cotta_dir.rb', line 31 def root? parent.nil? end |
#stat ⇒ Object
returns the stat of the current directory
41 42 43 |
# File 'lib/cotta/cotta_dir.rb', line 41 def stat factory.system.dir_stat(@path) end |
#to_s ⇒ Object
179 180 181 |
# File 'lib/cotta/cotta_dir.rb', line 179 def to_s @path.to_s end |