Class: Cuprum::Cli::Dependencies::FileSystem
- Inherits:
-
Object
- Object
- Cuprum::Cli::Dependencies::FileSystem
- Defined in:
- lib/cuprum/cli/dependencies/file_system.rb
Overview
Utility wrapping filesystem operations.
Direct Known Subclasses
Defined Under Namespace
Classes: DirectoryIsAFileError, DirectoryNotFoundError, FileError, FileIsADirectoryError, FileNotFoundError, Mock
Instance Attribute Summary collapse
-
#root_path ⇒ String
readonly
The path to the root directory.
Instance Method Summary collapse
-
#create_directory(path, recursive: false) ⇒ String
(also: #make_directory)
Creates a directory at the requested path.
-
#directory?(path) ⇒ true, false
(also: #directory_exists?)
Checks if the requested directory exists.
- #each_file(pattern) ⇒ Object
-
#file?(path) ⇒ true, false
(also: #file_exists?)
Checks if the requested file exists.
-
#initialize(root_path: Dir.pwd) ⇒ FileSystem
constructor
A new instance of FileSystem.
- #read_file(file_or_path) ⇒ Object (also: #read)
-
#with_tempfile {|the| ... } ⇒ Object
Creates a tempfile and passes it to the block.
- #write_file(file_or_path, data) ⇒ Object (also: #write)
Constructor Details
#initialize(root_path: Dir.pwd) ⇒ FileSystem
Returns a new instance of FileSystem.
31 32 33 |
# File 'lib/cuprum/cli/dependencies/file_system.rb', line 31 def initialize(root_path: Dir.pwd) @root_path = root_path end |
Instance Attribute Details
#root_path ⇒ String (readonly)
Returns the path to the root directory.
36 37 38 |
# File 'lib/cuprum/cli/dependencies/file_system.rb', line 36 def root_path @root_path end |
Instance Method Details
#create_directory(path, recursive: false) ⇒ String Also known as: make_directory
Creates a directory at the requested path.
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/cuprum/cli/dependencies/file_system.rb', line 45 def create_directory(path, recursive: false) tools.assertions.validate_name(path, as: 'path') resolved = resolve_path(path) return path if directory?(resolved) handle_create_errors(path) do recursive ? FileUtils.mkdir_p(resolved) : FileUtils.mkdir(resolved) end path end |
#directory?(path) ⇒ true, false Also known as: directory_exists?
Checks if the requested directory exists.
66 67 68 69 70 71 72 |
# File 'lib/cuprum/cli/dependencies/file_system.rb', line 66 def directory?(path) tools.assertions.validate_name(path, as: 'path') path = resolve_path(path) File.exist?(path) && File.directory?(path) end |
#each_file ⇒ Enumerator<String> #each_file({ |file| }) {|the| ... } ⇒ nil
90 91 92 93 94 95 96 97 98 |
# File 'lib/cuprum/cli/dependencies/file_system.rb', line 90 def each_file(pattern, &) return enum_for(:each_file, pattern) unless block_given? path = resolve_path(pattern) Dir[path].each(&) nil end |
#file?(path) ⇒ true, false Also known as: file_exists?
Checks if the requested file exists.
106 107 108 109 110 111 112 |
# File 'lib/cuprum/cli/dependencies/file_system.rb', line 106 def file?(path) tools.assertions.validate_name(path, as: 'path') path = resolve_path(path) File.exist?(path) && File.file?(path) end |
#read_file(file) ⇒ String #read_file(path) ⇒ String Also known as: read
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/cuprum/cli/dependencies/file_system.rb', line 131 def read_file(file_or_path) validate_file(file_or_path, as: 'file') return file_or_path.read if io_stream?(file_or_path) path = resolve_path(file_or_path) File.read(path) rescue Errno::EISDIR raise FileIsADirectoryError, "unable to read file #{file_or_path} - file is a directory" rescue Errno::ENOENT raise FileNotFoundError, "unable to read file #{file_or_path} - file not found" end |
#with_tempfile {|the| ... } ⇒ Object
Creates a tempfile and passes it to the block.
153 |
# File 'lib/cuprum/cli/dependencies/file_system.rb', line 153 def with_tempfile(&) = Tempfile.create(&) |
#write_file(file, data) ⇒ Integer #write_file(path, data) ⇒ Integer Also known as: write
176 177 178 179 180 181 182 183 184 |
# File 'lib/cuprum/cli/dependencies/file_system.rb', line 176 def write_file(file_or_path, data) validate_file(file_or_path, as: 'file') return file_or_path.write(data) if io_stream?(file_or_path) path = resolve_path(file_or_path) handle_write_errors(file_or_path) { File.write(path, data) } end |