Class: Begin::Path
- Inherits:
-
Object
- Object
- Begin::Path
- 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
- #basename ⇒ Object
- #contains?(path) ⇒ Boolean
- #copy_to(destination) ⇒ Object
- #dir_contents ⇒ Object
- #directory? ⇒ Boolean
- #ensure_dir_exists ⇒ Object
- #ensure_exists ⇒ Object
- #ensure_symlink_exists ⇒ Object
- #eql?(other) ⇒ Boolean
- #exists? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(path, parent_dir, help) ⇒ Path
constructor
A new instance of Path.
- #make_dir ⇒ Object
- #make_parent_dirs ⇒ Object
- #to_s ⇒ Object
- #to_str ⇒ Object
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. path, parent_dir @help = help end |
Instance Method Details
#basename ⇒ Object
69 70 71 |
# File 'lib/begin/path.rb', line 69 def basename File.basename @path end |
#contains?(path) ⇒ 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_contents ⇒ Object
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
73 74 75 |
# File 'lib/begin/path.rb', line 73 def directory? File.directory? @path end |
#ensure_dir_exists ⇒ Object
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_exists ⇒ Object
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 |
#ensure_symlink_exists ⇒ Object
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
12 13 14 |
# File 'lib/begin/path.rb', line 12 def eql?(other) @path.eql?(other.to_str) end |
#exists? ⇒ Boolean
77 78 79 |
# File 'lib/begin/path.rb', line 77 def exists? File.exist? @path end |
#hash ⇒ Object
16 17 18 |
# File 'lib/begin/path.rb', line 16 def hash @path.hash end |
#make_dir ⇒ Object
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_dirs ⇒ Object
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_s ⇒ Object
20 21 22 |
# File 'lib/begin/path.rb', line 20 def to_s @path end |
#to_str ⇒ Object
24 25 26 |
# File 'lib/begin/path.rb', line 24 def to_str @path end |