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
5 6 7 8 |
# File 'lib/begin/path.rb', line 5 def initialize(path, parent_dir, help) @path = File. path, parent_dir @help = help end |
Instance Method Details
#basename ⇒ Object
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_contents ⇒ Object
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_exists ⇒ Object
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_exists ⇒ Object
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 |
#ensure_symlink_exists ⇒ Object
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 |
#hash ⇒ Object
14 15 16 |
# File 'lib/begin/path.rb', line 14 def hash @path.hash end |
#make_dir ⇒ Object
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_dirs ⇒ Object
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_s ⇒ Object
18 19 20 |
# File 'lib/begin/path.rb', line 18 def to_s @path end |
#to_str ⇒ Object
22 23 24 |
# File 'lib/begin/path.rb', line 22 def to_str @path end |