Class: FileManager

Inherits:
Object
  • Object
show all
Defined in:
lib/file_management/file_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  @path = options[:path] || './'
  @excludes = options[:excludes] || ''
  resolve_path(@path)
  options.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

#excludesObject

Returns the value of attribute excludes.



6
7
8
# File 'lib/file_management/file_manager.rb', line 6

def excludes
  @excludes
end

#pathObject

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

Returns:

  • (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_excludesObject



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