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

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

#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



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

#cleanObject



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

#createObject



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

def create
  FileUtils.mkdir_p(to_s)
  self
end

#deleteObject



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

Returns:

  • (Boolean)


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

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

#exists?Boolean

Returns:

  • (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

#nameObject



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

def name
  ::File.basename(to_s)
end

#parent_dirObject



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

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
# 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_dirObject



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

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