Class: FileManager
- Inherits:
-
Object
- Object
- FileManager
- Defined in:
- lib/file_management/file_manager.rb
Instance Attribute Summary collapse
-
#excludes ⇒ Object
Returns the value of attribute excludes.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #all_files(all = false) ⇒ Object
- #delete_file(file) ⇒ Object
- #file_exists?(file) ⇒ Boolean
- #file_size(file) ⇒ Object
- #format_excludes ⇒ Object
-
#initialize(options = {}) ⇒ FileManager
constructor
A new instance of FileManager.
- #make_file(file) ⇒ Object
- #pwd(file) ⇒ Object
- #rename(file, new_name) ⇒ Object
- #stats(file, request) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ FileManager
Returns a new instance of FileManager.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/file_management/file_manager.rb', line 7 def initialize( = {}) @path = [:path] || './' @excludes = [:excludes] || '' resolve_path(@path) .each do |k,v| if k != :path && k != :excludes instance_variable_set('@' + k.to_s, v) add_accessor(k) end end end |
Instance Attribute Details
#excludes ⇒ Object
Returns the value of attribute excludes.
6 7 8 |
# File 'lib/file_management/file_manager.rb', line 6 def excludes @excludes end |
#path ⇒ Object
Returns the value of attribute path.
5 6 7 |
# File 'lib/file_management/file_manager.rb', line 5 def path @path end |
Instance Method Details
#all_files(all = false) ⇒ Object
36 37 38 39 |
# File 'lib/file_management/file_manager.rb', line 36 def all_files(all = false) file_list = Dir.entries(path).select { |f| File.file?("#{path}/#{f}") } file_list - (%w(. .. .DS_Store file_management.rb) + format_excludes) end |
#delete_file(file) ⇒ Object
64 65 66 67 |
# File 'lib/file_management/file_manager.rb', line 64 def delete_file(file) return false unless file_exists?(file) FileUtils.rm(pwd(file)) end |
#file_exists?(file) ⇒ Boolean
41 42 43 |
# File 'lib/file_management/file_manager.rb', line 41 def file_exists?(file) all_files.include?(file) end |
#file_size(file) ⇒ Object
49 50 51 52 |
# File 'lib/file_management/file_manager.rb', line 49 def file_size(file) puts 'File does not exist' unless file_exists?(file) Filesize.new(File.size(pwd(file)), Filesize::SI).pretty end |
#format_excludes ⇒ Object
31 32 33 34 |
# File 'lib/file_management/file_manager.rb', line 31 def format_excludes return excludes.split(' ') if excludes.class == String excludes end |
#make_file(file) ⇒ Object
54 55 56 57 |
# File 'lib/file_management/file_manager.rb', line 54 def make_file(file) return false if file_exists?(file) FileUtils.touch(pwd(file)) end |
#pwd(file) ⇒ Object
27 28 29 |
# File 'lib/file_management/file_manager.rb', line 27 def pwd(file) "#{path}/#{file}" end |
#rename(file, new_name) ⇒ Object
59 60 61 62 |
# File 'lib/file_management/file_manager.rb', line 59 def rename(file, new_name) return false if file_exists?(File.split(new_name).last) File.rename(pwd(file), pwd(new_name)) end |
#stats(file, request) ⇒ Object
45 46 47 |
# File 'lib/file_management/file_manager.rb', line 45 def stats(file, request) File.stat(pwd(file)).send(request) end |