class FileManager

Attributes

excludes[R]
path[RW]

Public Class Methods

new(options = {}) click to toggle source
# 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

Public Instance Methods

all_files(all = false) click to toggle source
# 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) click to toggle source
# File lib/file_management/file_manager.rb, line 64
def delete_file(file)
  return false unless file_exists?(file)
  FileUtils.rm(pwd(file))
end
excludes=(value) click to toggle source
# File lib/file_management/file_manager.rb, line 19
def excludes=(value)
  if value.nil? || value.class == Integer
    raise FileManagementErrors.new('Invalid value for @excludes')
  else
    @excludes = value
  end
end
file_exists?(file) click to toggle source
# File lib/file_management/file_manager.rb, line 41
def file_exists?(file)
  all_files.include?(file)
end
file_size(file) click to toggle source
# 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() click to toggle source
# File lib/file_management/file_manager.rb, line 31
def format_excludes
  return excludes.split(' ') if excludes.class == String
  excludes
end
make_file(file) click to toggle source
# 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) click to toggle source
# File lib/file_management/file_manager.rb, line 27
def pwd(file)
  "#{path}/#{file}"
end
rename(file, new_name) click to toggle source
# 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) click to toggle source
# File lib/file_management/file_manager.rb, line 45
def stats(file, request)
  File.stat(pwd(file)).send(request)
end

Private Instance Methods

add_accessor(name) click to toggle source
# File lib/file_management/file_manager.rb, line 71
def add_accessor(name)
  self.class.send(:attr_accessor, name)
end
resolve_path(path) click to toggle source
# File lib/file_management/file_manager.rb, line 75
def resolve_path(path)
  FileUtils.mkdir_p(path.split)
  raise FileManagementErrors.new('Directory could not be created') unless File.directory?(path)
end