Class: Begin::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/begin/path.rb

Overview

The canonical file path representation used throughout the application. Paths are immediately expanded into absolute file paths on construction

Instance Method Summary collapse

Constructor Details

#initialize(path, parent_dir, help) ⇒ Path



5
6
7
8
# File 'lib/begin/path.rb', line 5

def initialize(path, parent_dir, help)
  @path = File.expand_path path, parent_dir
  @help = help
end

Instance Method Details

#basenameObject



64
65
66
# File 'lib/begin/path.rb', line 64

def basename
  File.basename @path
end

#contains?(path) ⇒ Boolean



76
77
78
# File 'lib/begin/path.rb', line 76

def contains?(path)
  path.to_str.start_with? @path
end

#copy_to(destination) ⇒ Object



58
59
60
61
62
# File 'lib/begin/path.rb', line 58

def copy_to(destination)
  ensure_exists
  destination.ensure_dir_exists
  FileUtils.cp @path, destination
end

#dir_contentsObject



43
44
45
46
# File 'lib/begin/path.rb', line 43

def dir_contents
  escaped_path = @path.gsub(/[\\\{\}\[\]\*\?\.]/) { |x| '\\' + x }
  Dir.glob(File.join([escaped_path, '*']))
end

#directory?Boolean



68
69
70
# File 'lib/begin/path.rb', line 68

def directory?
  File.directory? @path
end

#ensure_dir_existsObject

Raises:

  • (IOError)


37
38
39
40
41
# File 'lib/begin/path.rb', line 37

def ensure_dir_exists
  ensure_exists
  return if directory?
  raise IOError, "#{@help} '#{@path}' is not a directory"
end

#ensure_existsObject

Raises:

  • (IOError)


26
27
28
29
# File 'lib/begin/path.rb', line 26

def ensure_exists
  return if File.exist? @path
  raise IOError, "#{@help} '#{@path}' does not exist"
end

Raises:

  • (IOError)


31
32
33
34
35
# File 'lib/begin/path.rb', line 31

def ensure_symlink_exists
  ensure_exists
  return if File.symlink? @path
  raise IOError, "#{@help} '#{@path}' is not a symbolic link"
end

#eql?(other) ⇒ Boolean



10
11
12
# File 'lib/begin/path.rb', line 10

def eql?(other)
  @path.eql?(other.to_str)
end

#exists?Boolean



72
73
74
# File 'lib/begin/path.rb', line 72

def exists?
  File.exist? @path
end

#hashObject



14
15
16
# File 'lib/begin/path.rb', line 14

def hash
  @path.hash
end

#make_dirObject



48
49
50
51
# File 'lib/begin/path.rb', line 48

def make_dir
  Dir.mkdir @path unless File.exist? @path
  ensure_dir_exists
end

#make_parent_dirsObject



53
54
55
56
# File 'lib/begin/path.rb', line 53

def make_parent_dirs
  parent = File.dirname @path
  FileUtils.mkdir_p parent
end

#to_sObject



18
19
20
# File 'lib/begin/path.rb', line 18

def to_s
  @path
end

#to_strObject



22
23
24
# File 'lib/begin/path.rb', line 22

def to_str
  @path
end