Class: Siba::FileHelper
- Inherits:
-
Object
- Object
- Siba::FileHelper
- Extended by:
- FilePlug
- Defined in:
- lib/siba/helpers/file_helper.rb
Class Method Summary collapse
-
.change_file(path_to_file, &block) ⇒ Object
Used to replace text in file Example: change_file(“/path”) do |file_text| file_text.gsub “one”, “two” end.
- .dir_empty?(dir) ⇒ Boolean
-
.dirs(dir) ⇒ Object
Retuns an array containing names of sub-directories located in the dir.
- .dirs_count(dir) ⇒ Object
-
.dirs_same?(dir1, dir2) ⇒ Boolean
Raises error if dirs are not identical.
-
.entries(dir) ⇒ Object
Retuns an array containing all dir entires except ‘.’ and ‘..’ dirs.
-
.files(dir) ⇒ Object
Retuns an array containing names of files located in the dir.
-
.read(file) ⇒ Object
Reads a file in UTF-8 encoding.
-
.write(file, data) ⇒ Object
Write to file in UTF-8 encoding.
Methods included from FilePlug
siba_file, siba_file, siba_file=
Class Method Details
.change_file(path_to_file, &block) ⇒ Object
Used to replace text in file Example:
change_file("/path") do |file_text|
file_text.gsub "one", "two"
end
38 39 40 41 42 |
# File 'lib/siba/helpers/file_helper.rb', line 38 def change_file(path_to_file, &block) file_text = Siba::FileHelper.read path_to_file file_text = block.call file_text Siba::FileHelper.write path_to_file, file_text end |
.dir_empty?(dir) ⇒ Boolean
8 9 10 |
# File 'lib/siba/helpers/file_helper.rb', line 8 def dir_empty?(dir) entries(dir).empty? end |
.dirs(dir) ⇒ Object
Retuns an array containing names of sub-directories located in the dir
50 51 52 53 54 |
# File 'lib/siba/helpers/file_helper.rb', line 50 def dirs(dir) entries(dir).select do |entry| siba_file.file_directory?(File.join(dir,entry)) end end |
.dirs_count(dir) ⇒ Object
12 13 14 |
# File 'lib/siba/helpers/file_helper.rb', line 12 def dirs_count(dir) dirs(dir).size end |
.dirs_same?(dir1, dir2) ⇒ Boolean
Raises error if dirs are not identical
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/siba/helpers/file_helper.rb', line 64 def dirs_same?(dir1, dir2) dir1_entries = siba_file.dir_entries dir1 dir2_entries = siba_file.dir_entries dir2 diff1 = (dir1_entries - dir2_entries).map{|i| File.join(dir1,i)} diff2 = (dir2_entries - dir1_entries).map{|i| File.join(dir2,i)} diff = diff1 + diff2 msg = "The directories '#{File.basename(dir1)}' and '#{File.basename(dir2)}' are different: " raise Siba::Error, "#{msg}#{diff.take(10).join(', ')}" unless diff.empty? # compare files and directories dir1_entries.each do |dir| next if dir == "." || dir == ".." sub_dir1_entry = File.join dir1, dir sub_dir2_entry = File.join dir2, dir # compare files if siba_file.file_file? sub_dir1_entry raise "#{msg}'#{sub_dir2_entry}' is not a file" unless siba_file.file_file? sub_dir2_entry unless siba_file.file_utils_compare_file sub_dir1_entry, sub_dir2_entry raise Siba::Error, "#{msg}'#{sub_dir1_entry}' and '#{sub_dir2_entry}' files are different." end end # compare permissions if (siba_file.file_stat(sub_dir1_entry).mode % 01000) != (siba_file.file_stat(sub_dir2_entry).mode % 01000) raise Siba::Error, "#{msg}'#{sub_dir1_entry}' and '#{sub_dir2_entry}' entries have different permissions." end # compare sub-dirs if siba_file.file_directory? sub_dir1_entry raise Siba::Error, "#{msg}'#{sub_dir2_entry}' is not a directory" unless siba_file.file_directory? sub_dir2_entry dirs_same? sub_dir1_entry, sub_dir2_entry end end end |
.entries(dir) ⇒ Object
Retuns an array containing all dir entires except ‘.’ and ‘..’ dirs
45 46 47 |
# File 'lib/siba/helpers/file_helper.rb', line 45 def entries(dir) siba_file.dir_entries(dir) - %w{ . .. } end |
.files(dir) ⇒ Object
Retuns an array containing names of files located in the dir
57 58 59 60 61 |
# File 'lib/siba/helpers/file_helper.rb', line 57 def files(dir) entries(dir).select do |entry| siba_file.file_file?(File.join(dir,entry)) end end |
.read(file) ⇒ Object
Reads a file in UTF-8 encoding
17 18 19 20 21 22 23 |
# File 'lib/siba/helpers/file_helper.rb', line 17 def read(file) str = File.read file, { open_args: ["r:bom|utf-8"]} unless str.valid_encoding? raise Siba::Error, "Incorrect file encoding. Please save the options with UTF-8 encoding." end str end |
.write(file, data) ⇒ Object
Write to file in UTF-8 encoding
26 27 28 29 30 31 |
# File 'lib/siba/helpers/file_helper.rb', line 26 def write(file, data) siba_file.file_utils_remove_entry_secure file if siba_file.file_file? file siba_file.file_open(file, "w:utf-8") do |file| file << data end end |