Class: App::UtilsFiles
- Inherits:
-
Object
- Object
- App::UtilsFiles
- Defined in:
- lib/core/utils_files.rb
Class Method Summary collapse
- .delete_file(full_path_and_file) ⇒ Object
- .file_exists(full_path_and_file) ⇒ Object
- .get_files_in_dir(path, only_with_extension = nil) ⇒ Object
- .get_full_path(path) ⇒ Object
- .path_exists(full_path) ⇒ Object
-
.read_file(full_path_and_file) ⇒ Object
Get content of a file as an “Array of Lines”.
-
.write_file(full_path_and_file, array_of_lines) ⇒ Object
Write an “Array of Lines” to a file.
-
.write_file_string(full_path_and_file, content) ⇒ Object
Write a “String” to a file.
Class Method Details
.delete_file(full_path_and_file) ⇒ Object
66 67 68 69 70 |
# File 'lib/core/utils_files.rb', line 66 def self.delete_file(full_path_and_file) if file_exists(full_path_and_file) FileUtils.rm(full_path_and_file) end end |
.file_exists(full_path_and_file) ⇒ Object
76 77 78 |
# File 'lib/core/utils_files.rb', line 76 def self.file_exists(full_path_and_file) File.exist?(File.(full_path_and_file)) end |
.get_files_in_dir(path, only_with_extension = nil) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/core/utils_files.rb', line 84 def self.get_files_in_dir(path, only_with_extension = nil) path = "/#{App::UtilsStrings.remove_surrounding_slashes(File.expand_path(path))}" unless path_exists(path) raise RuntimeError, "Directory doesn't exist: #{path}" end files = Dir.glob("#{path}/**/*.#{only_with_extension.nil? ? '*' : only_with_extension}") files end |
.get_full_path(path) ⇒ Object
80 81 82 |
# File 'lib/core/utils_files.rb', line 80 def self.get_full_path(path) "/#{App::UtilsStrings.remove_surrounding_slashes(File.expand_path(path))}" end |
.path_exists(full_path) ⇒ Object
72 73 74 |
# File 'lib/core/utils_files.rb', line 72 def self.path_exists(full_path) File.directory?(File.(full_path)) end |
.read_file(full_path_and_file) ⇒ Object
Get content of a file as an “Array of Lines”
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/core/utils_files.rb', line 52 def self.read_file(full_path_and_file) full_path_and_file = File.(full_path_and_file) unless file_exists(full_path_and_file) App::Terminal::error("The file doesn't exist: #{App::Terminal::format_directory(full_path_and_file)}", nil, true) end file_content = [] file = File.open(full_path_and_file).read file.gsub!(/\r\n?/, "\n") file.each_line do |line| file_content << line end file_content end |
.write_file(full_path_and_file, array_of_lines) ⇒ Object
Write an “Array of Lines” to a file.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/core/utils_files.rb', line 10 def self.write_file(full_path_and_file, array_of_lines) full_path_and_file = File.(full_path_and_file) unless array_of_lines.is_a? Array raise RuntimeError, "Expected an array of lines to write to file, instead got: #{array_of_lines.class}" end validate_file_and_path(full_path_and_file) begin File.open(full_path_and_file, 'w') { |file| array_of_lines.each_with_index do |line, index| if index == array_of_lines.size - 1 file.write("#{line}") else file.write("#{line}\n") end end file.close } rescue Exception => e App::Terminal::error('Something went wrong', "#{e.message}", true) end end |
.write_file_string(full_path_and_file, content) ⇒ Object
Write a “String” to a file.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/core/utils_files.rb', line 34 def self.write_file_string(full_path_and_file, content) full_path_and_file = File.(full_path_and_file) unless content.is_a? String raise RuntimeError, "Expected an String to write to file, instead got: #{content.class}" end validate_file_and_path(full_path_and_file) begin File.open(full_path_and_file, 'w') { |file| file.write("#{content}") file.close } rescue Exception => e App::Terminal::error('Something went wrong', "#{e.message}", true) end end |