Class: Workspace::Dir
- Inherits:
-
Object
- Object
- Workspace::Dir
- Defined in:
- lib/workspace/dir.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
-
#workspace ⇒ Object
Returns the value of attribute workspace.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #absolute_path ⇒ Object
- #children(glob = "*", &block) ⇒ Object
- #clean ⇒ Object
- #copy(target_dir) ⇒ Object
- #create ⇒ Object
- #delete ⇒ Object
- #dir(dir_path) ⇒ Object
- #directories(&block) ⇒ Object
- #empty? ⇒ Boolean
- #exists? ⇒ Boolean
- #file(file_path) ⇒ Object
- #files(&block) ⇒ Object
-
#initialize(workspace, path = "/") ⇒ Dir
constructor
A new instance of Dir.
- #move(target_dir) ⇒ Object
- #name ⇒ Object
- #parent_dir ⇒ Object
- #relative_path(relative_dir = nil) ⇒ Object
- #root_dir ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(workspace, path = "/") ⇒ Dir
Returns a new instance of Dir.
9 10 11 12 |
# File 'lib/workspace/dir.rb', line 9 def initialize(workspace, path = "/") @workspace = workspace @path = path end |
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
3 4 5 |
# File 'lib/workspace/dir.rb', line 3 def path @path end |
#workspace ⇒ Object
Returns the value of attribute workspace.
3 4 5 |
# File 'lib/workspace/dir.rb', line 3 def workspace @workspace end |
Instance Method Details
#==(other) ⇒ Object
5 6 7 |
# File 'lib/workspace/dir.rb', line 5 def ==(other) other.class == self.class && other.to_s == self.to_s end |
#absolute_path ⇒ Object
29 30 31 |
# File 'lib/workspace/dir.rb', line 29 def absolute_path to_s end |
#children(glob = "*", &block) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/workspace/dir.rb', line 87 def children(glob = "*", &block) entries = [] ::Dir.chdir(to_s) do ::Dir[glob].each do |path| entry = ::File.directory?(::File.join(to_s, path)) ? dir(path) : file(path) yield entry if block_given? entries.push(entry) end end entries end |
#clean ⇒ Object
66 67 68 69 |
# File 'lib/workspace/dir.rb', line 66 def clean delete create end |
#copy(target_dir) ⇒ Object
50 51 52 53 54 |
# File 'lib/workspace/dir.rb', line 50 def copy(target_dir) target_dir.parent_dir.create unless target_dir.parent_dir.exists? FileUtils.cp_r(to_s, target_dir.to_s) self end |
#create ⇒ Object
37 38 39 40 |
# File 'lib/workspace/dir.rb', line 37 def create FileUtils.mkdir_p(to_s) self end |
#delete ⇒ Object
62 63 64 |
# File 'lib/workspace/dir.rb', line 62 def delete FileUtils.rm_rf(to_s) end |
#dir(dir_path) ⇒ Object
75 76 77 |
# File 'lib/workspace/dir.rb', line 75 def dir(dir_path) Workspace::Dir.new(@workspace, ::File.join(@path, dir_path)) end |
#directories(&block) ⇒ Object
103 104 105 |
# File 'lib/workspace/dir.rb', line 103 def directories(&block) children("*/", &block) end |
#empty? ⇒ Boolean
46 47 48 |
# File 'lib/workspace/dir.rb', line 46 def empty? !exists? or children.count == 0 end |
#exists? ⇒ Boolean
42 43 44 |
# File 'lib/workspace/dir.rb', line 42 def exists? ::File.directory?(to_s) end |
#file(file_path) ⇒ Object
71 72 73 |
# File 'lib/workspace/dir.rb', line 71 def file(file_path) Workspace::File.new(@workspace, ::File.join(@path, file_path)) end |
#files(&block) ⇒ Object
99 100 101 |
# File 'lib/workspace/dir.rb', line 99 def files(&block) children("*.*", &block) end |
#move(target_dir) ⇒ Object
56 57 58 59 60 |
# File 'lib/workspace/dir.rb', line 56 def move(target_dir) target_dir.parent_dir.create unless target_dir.parent_dir.exists? FileUtils.mv(to_s, target_dir.to_s) self end |
#name ⇒ Object
33 34 35 |
# File 'lib/workspace/dir.rb', line 33 def name ::File.basename(to_s) end |
#parent_dir ⇒ Object
83 84 85 |
# File 'lib/workspace/dir.rb', line 83 def parent_dir root_dir.dir(::File.("..", @path)) end |
#relative_path(relative_dir = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/workspace/dir.rb', line 18 def relative_path(relative_dir = nil) if relative_dir relative_dir = relative_dir.dir if relative_dir.class == Workspace::File first = Pathname.new(relative_dir.path) second = Pathname.new(path) second.relative_path_from(first).to_s else @path.gsub(%r{^/}, "") end end |
#root_dir ⇒ Object
79 80 81 |
# File 'lib/workspace/dir.rb', line 79 def root_dir Workspace::Dir.new(@workspace, "") end |
#to_s ⇒ Object
14 15 16 |
# File 'lib/workspace/dir.rb', line 14 def to_s ::File.join(@workspace, @path) end |