Class: Nabokov::FileManager
- Inherits:
-
Object
- Object
- Nabokov::FileManager
- Defined in:
- lib/nabokov/core/file_manager.rb
Overview
This class is the wrapper around the FileUtils
Class Method Summary collapse
-
.copy(original_file_path, destination_file_path) ⇒ Object
Copies given file to the given destination.
-
.copy_and_rename(original_file_path, to_directory, new_name) ⇒ Object
Copies given file to the given destination and renames to the given name Note: the extension of the file remains the same that’s why new name can not contain ‘.’.
-
.remove(path) ⇒ Object
Removes the filve at the given path.
Class Method Details
.copy(original_file_path, destination_file_path) ⇒ Object
Copies given file to the given destination
8 9 10 11 12 |
# File 'lib/nabokov/core/file_manager.rb', line 8 def self.copy(original_file_path, destination_file_path) raise "Couldn't find file at '#{original_file_path}'" unless File.exist?(original_file_path) FileUtils.cp(original_file_path, destination_file_path) File.(destination_file_path) end |
.copy_and_rename(original_file_path, to_directory, new_name) ⇒ Object
Copies given file to the given destination and renames to the given name Note: the extension of the file remains the same that’s why new name can not contain ‘.’
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/nabokov/core/file_manager.rb', line 16 def self.copy_and_rename(original_file_path, to_directory, new_name) raise "Couldn't find file at '#{original_file_path}'" unless File.exist?(original_file_path) raise "Couldn't find directory at '#{to_directory}'" unless Dir.exist?(to_directory) raise "New name of the file could not be empty" if new_name.empty? raise "New name of the file '#{new_name}' contains invalid character '.'" if new_name.include?(".") original_file_pathname = Pathname.new(original_file_path) original_file_extension = original_file_pathname.extname new_file_pathname = Pathname.new(to_directory) + Pathname.new(new_name + original_file_extension) new_file_path = new_file_pathname.to_s FileUtils.cp(original_file_path, new_file_path) File.(new_file_path) end |
.remove(path) ⇒ Object
Removes the filve at the given path
31 32 33 34 |
# File 'lib/nabokov/core/file_manager.rb', line 31 def self.remove(path) raise "Can not file neither file nor directory at '#{path}'" unless File.exist?(path) or Dir.exist?(path) FileUtils.rm_rf(path) end |