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

Returns a new instance of Path.



7
8
9
10
# File 'lib/begin/path.rb', line 7

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

Instance Method Details

#basenameObject



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

def basename
  File.basename @path
end

#contains?(path) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/begin/path.rb', line 81

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

#copy_to(destination) ⇒ Object



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

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

#dir_contentsObject



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

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

#directory?Boolean

Returns:

  • (Boolean)


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

def directory?
  File.directory? @path
end

#ensure_dir_existsObject

Raises:

  • (IOError)


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

def ensure_dir_exists
  ensure_exists
  return if directory?

  raise IOError, "#{@help} '#{@path}' is not a directory"
end

#ensure_existsObject

Raises:

  • (IOError)


28
29
30
31
32
# File 'lib/begin/path.rb', line 28

def ensure_exists
  return if File.exist? @path

  raise IOError, "#{@help} '#{@path}' does not exist"
end

Raises:

  • (IOError)


34
35
36
37
38
39
# File 'lib/begin/path.rb', line 34

def ensure_symlink_exists
  ensure_exists
  return if File.symlink? @path

  raise IOError, "#{@help} '#{@path}' is not a symbolic link"
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/begin/path.rb', line 12

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

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  File.exist? @path
end

#hashObject



16
17
18
# File 'lib/begin/path.rb', line 16

def hash
  @path.hash
end

#make_dirObject



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

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

#make_parent_dirsObject



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

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

#to_sObject



20
21
22
# File 'lib/begin/path.rb', line 20

def to_s
  @path
end

#to_strObject



24
25
26
# File 'lib/begin/path.rb', line 24

def to_str
  @path
end