Class: Pow::Directory

Inherits:
Base
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pow/directory.rb

Overview

Pow object representing a directory. Inherits from Pow::Base

Instance Attribute Summary

Attributes inherited from Base

#path

Instance Method Summary collapse

Methods inherited from Base

#/, #<=>, #==, #=~, #[], #accessed_at, #changed_at, #create_directory, #create_file, #directory?, #eql?, #exists?, #extension, #file?, #modified_at, #move_to!, #name, open, #parent, #permissions, #permissions=, #rename_to, #size, #to_s, working_directory, #write

Constructor Details

#initialize(path, mode = nil, &block) ⇒ Directory

:nodoc:



7
8
9
10
# File 'lib/pow/directory.rb', line 7

def initialize(path, mode=nil, &block) #:nodoc:
  super
  open(&block) if block_given?
end

Instance Method Details

#children(options = {}) ⇒ Object

Returns all files and directories in the directory.

Parameters

options<Hash>
:no_dirs, :no_files

(defaults to :no_dirs => true, :no_files => true)



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pow/directory.rb', line 85

def children(options={})
  options = {:no_dirs => false, :no_files => false}.merge(options)

  children = []
  Dir.foreach(path) do |child|
    child_path = ::File.join(path, child)

    next if child == '.'
    next if child == '..'
    next if (::File.file?(child_path) and options[:no_files]) 
    next if (::File.directory?(child_path) and options[:no_dirs])
    children << Pow(child_path) 
  end
  
  children
end

#copy_to(dest) ⇒ Object Also known as: cp



39
40
41
# File 'lib/pow/directory.rb', line 39

def copy_to(dest)
  FileUtils.cp_r(path, dest.to_s)
end

#copy_to!(dest) ⇒ Object Also known as: cp!



44
45
46
47
# File 'lib/pow/directory.rb', line 44

def copy_to!(dest)
  Pow(dest).parent.create_directory
  FileUtils.cp_r(path, dest.to_s)
end

#create(&block) ⇒ Object

:nodoc:



24
25
26
# File 'lib/pow/directory.rb', line 24

def create(&block) #:nodoc:
  create_directory(&block)
end

#deleteObject

Deletes an empty directory

Raises:



29
30
31
32
# File 'lib/pow/directory.rb', line 29

def delete
  raise PowError, "Can not delete '#{path}'. It must be empty before you delete it!" unless children.empty?
  Dir.rmdir path
end

#delete!Object

Recurslivly deletes the directory, DANGER! DANGER!



35
36
37
# File 'lib/pow/directory.rb', line 35

def delete!
  FileUtils.rm_r path
end

#directoriesObject Also known as: dirs

Returns all the directories in the directory



76
77
78
# File 'lib/pow/directory.rb', line 76

def directories
  children(:no_files => true)
end

#each(&block) ⇒ Object

Yields the child paths to an each block.

Raises:



103
104
105
106
# File 'lib/pow/directory.rb', line 103

def each(&block)
  raise PowError, "'#{path.realpath}' does not exist!" unless exists?
  children.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/pow/directory.rb', line 57

def empty?
  children.empty?
end

#filesObject

Returns all the files in the directory



71
72
73
# File 'lib/pow/directory.rb', line 71

def files
  children(:no_dirs => true)
end

#glob(pattern, *flags) ⇒ Object

A wrapper for Dir.glob, returns files & directories found by expanding pattern.



66
67
68
# File 'lib/pow/directory.rb', line 66

def glob(pattern, *flags)
  Dir[::File.join(to_s, pattern), *flags].collect {|path| Pow(path)}
end

#move_to(dest) ⇒ Object Also known as: mv



50
51
52
53
54
# File 'lib/pow/directory.rb', line 50

def move_to(dest)
  if FileUtils.mv(path.to_s, dest.to_s)
    self.path = dest
  end
end

#open(mode = nil, &block) ⇒ Object

:nodoc:

Raises:



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pow/directory.rb', line 12

def open(mode=nil, &block) #:nodoc:
  raise PowError, "'#{path}' does not exist!" unless exists?
  
  begin
    former_dir = Dir.pwd
    Dir.chdir self.to_s
    block.call self
  ensure
    Dir.chdir(former_dir)
  end
end