class DownloadsSorter

Constants

Apps
Archives
Audio
Books
Documents
Images
PATH
SUB_DIRS
VERSION

Attributes

dir_path[R]

Public Class Methods

new(dir_path) click to toggle source
# File lib/downloads_sorter.rb, line 17
def initialize(dir_path)
  raise NoPathError unless Dir.exists?(dir_path)
  @dir_path = dir_path
  check_or_create_dirs
end

Public Instance Methods

sort() click to toggle source
# File lib/downloads_sorter.rb, line 23
def sort
  Dir.chdir(@dir_path) do
    SUB_DIRS.each do |dir|
      move_files_to_sub_dir(dir, DownloadsSorter.const_get(dir))
    end
  end
end

Private Instance Methods

check_or_create_dirs() click to toggle source
# File lib/downloads_sorter.rb, line 33
def check_or_create_dirs
  SUB_DIRS.each do |dir|
    unless Dir.exists?(File.join(dir_path, dir))
      Dir.mkdir(File.join(dir_path, dir))
    end
  end
end
move_files_to_sub_dir(path, types) click to toggle source
# File lib/downloads_sorter.rb, line 41
def move_files_to_sub_dir(path, types)
  files = types.map { |type| Dir.glob(type) }.flatten
  if files.any?
    files.each do |file|
      FileUtils.mv(file, path)
    end
  end 
end