Class: ToolsFiles
Class Method Summary collapse
-
.create_dir(directory, directory_name) ⇒ Object
Create a directory in work area.
-
.create_file(directory, file_name, file_name_set) ⇒ Object
Create a file in work area.
-
.load_file(file_to_load) ⇒ Object
Load a file in work area.
- .open_file(file, default_editor = nil) ⇒ Object
-
.purge_files(path, select, time) ⇒ Object
Purge files in directory.
-
.remove_file(file_to_remove) ⇒ Object
Delete a file in work area.
Instance Method Summary collapse
- #create_file_array(file_name, content) ⇒ Object
- #create_file_lines(file_name, content) ⇒ Object
-
#initialize(options = {}) ⇒ ToolsFiles
constructor
A new instance of ToolsFiles.
Constructor Details
#initialize(options = {}) ⇒ ToolsFiles
Returns a new instance of ToolsFiles.
5 |
# File 'lib/lib/files.rb', line 5 def initialize( = {}); end |
Class Method Details
.create_dir(directory, directory_name) ⇒ Object
Create a directory in work area
Sample
ToolsFiles.create_dir Tools.home + ‘/2018/xykotools/tools/home’, ‘tools_home’ home = (ToolsUtil.get_variable ‘tools_home’) => ~/2018/xykotools/tools/home
35 36 37 38 39 40 |
# File 'lib/lib/files.rb', line 35 def self.create_dir(directory, directory_name) directory += '/' unless directory.end_with? '/' complete_file = (directory + '/').gsub('//', '/') Dir.mkdir(complete_file) unless File.exist? complete_file ToolsUtil.set_variable directory_name, complete_file end |
.create_file(directory, file_name, file_name_set) ⇒ Object
Create a file in work area
Sample
ToolsFiles.create_file home, 'xyko_file.txt', 'xyko_file'
xyko = (ToolsUtil.get_variable 'xyko_file') => ~/2018/xykotools/tools/home/xyko_file.txt
53 54 55 56 57 |
# File 'lib/lib/files.rb', line 53 def self.create_file(directory, file_name, file_name_set) complete_file = (directory + '/' + file_name).gsub('//', '/') File.open(complete_file, 'w') unless File.exist? complete_file ToolsUtil.set_variable file_name_set, complete_file end |
.load_file(file_to_load) ⇒ Object
Load a file in work area
Sample
ToolsFiles.load_file file_to_load
66 67 68 69 70 |
# File 'lib/lib/files.rb', line 66 def self.load_file(file_to_load) return unless File.exist? file_to_load File.open(file_to_load, 'r') end |
.open_file(file, default_editor = nil) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/lib/files.rb', line 85 def self.open_file(file, default_editor = nil) if default_editor.nil? TTY::Editor.open(file, command: :vi) else TTY::Editor.open(file, command: default_editor) end end |
.purge_files(path, select, time) ⇒ Object
Purge files in directory
Sample
ToolsFiles. purge_files Cmdapi.configuration.home+‘/.cmdapi/backup’, ‘*’, 14*24*60*60’
Cmdapi.configuration.home+‘/.cmdapi/backup’, ‘*’, 14*24*60*60
17 18 19 20 21 22 23 24 |
# File 'lib/lib/files.rb', line 17 def self.purge_files(path, select, time) to_clean = Dir.glob(File.join(path, select)).select do |a| Time.now - File.ctime(a) > time end to_clean.each do |file_to_delete| File.delete(file_to_delete) end end |
.remove_file(file_to_remove) ⇒ Object
Delete a file in work area
Sample
ToolsFiles.delete_file file_to_delete
79 80 81 82 83 |
# File 'lib/lib/files.rb', line 79 def self.remove_file(file_to_remove) return unless File.exist? file_to_remove FileUtils.remove_file(file_to_remove) end |
Instance Method Details
#create_file_array(file_name, content) ⇒ Object
93 94 95 96 97 |
# File 'lib/lib/files.rb', line 93 def create_file_array file_name, content file = File.open(file_name, 'w') file.write content file.close end |
#create_file_lines(file_name, content) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/lib/files.rb', line 99 def create_file_lines file_name, content file = File.open(file_name, 'w') content.each do |line| file.puts line end file.close end |