Class: Workspace::Dir

Inherits:
Object
  • Object
show all
Defined in:
lib/workspace/dir.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workspace, path = "/") ⇒ Dir



9
10
11
12
# File 'lib/workspace/dir.rb', line 9

def initialize(workspace, path = "/")
  @workspace = workspace
  @path = path
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/workspace/dir.rb', line 3

def path
  @path
end

#workspaceObject

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_pathObject



30
31
32
# File 'lib/workspace/dir.rb', line 30

def absolute_path
  to_s
end

#children(glob = "*", &block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/workspace/dir.rb', line 88

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

#cleanObject



67
68
69
70
# File 'lib/workspace/dir.rb', line 67

def clean
  delete
  create
end

#copy(target_dir) ⇒ Object



51
52
53
54
55
# File 'lib/workspace/dir.rb', line 51

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

#createObject



38
39
40
41
# File 'lib/workspace/dir.rb', line 38

def create
  FileUtils.mkdir_p(to_s)
  self
end

#deleteObject



63
64
65
# File 'lib/workspace/dir.rb', line 63

def delete
  FileUtils.rm_rf(to_s)
end

#dir(dir_path) ⇒ Object



76
77
78
# File 'lib/workspace/dir.rb', line 76

def dir(dir_path)
  Workspace::Dir.new(@workspace, ::File.join(@path, dir_path))
end

#directories(&block) ⇒ Object



104
105
106
# File 'lib/workspace/dir.rb', line 104

def directories(&block)
  children("*/", &block)
end

#empty?Boolean



47
48
49
# File 'lib/workspace/dir.rb', line 47

def empty?
  !exists? or children.count == 0
end

#exists?Boolean



43
44
45
# File 'lib/workspace/dir.rb', line 43

def exists?
  ::File.directory?(to_s)
end

#file(file_path) ⇒ Object



72
73
74
# File 'lib/workspace/dir.rb', line 72

def file(file_path)
  Workspace::File.new(@workspace, ::File.join(@path, file_path))
end

#files(&block) ⇒ Object



100
101
102
# File 'lib/workspace/dir.rb', line 100

def files(&block)
  children("*.*", &block)
end

#move(target_dir) ⇒ Object



57
58
59
60
61
# File 'lib/workspace/dir.rb', line 57

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

#nameObject



34
35
36
# File 'lib/workspace/dir.rb', line 34

def name
  ::File.basename(to_s)
end

#parent_dirObject



84
85
86
# File 'lib/workspace/dir.rb', line 84

def parent_dir
  root_dir.dir(::File.expand_path("..", @path))
end

#relative_path(relative_dir = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# 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)
    result = second.relative_path_from(first).to_s
    result
  else
    @path.gsub(%r{^/}, "")
  end
end

#root_dirObject



80
81
82
# File 'lib/workspace/dir.rb', line 80

def root_dir
  Workspace::Dir.new(@workspace, "")
end

#to_sObject



14
15
16
# File 'lib/workspace/dir.rb', line 14

def to_s
  ::File.join(@workspace, @path)
end