class CodeKindly::Utils::File

Public Class Methods

all(path) click to toggle source
# File lib/code_kindly/utils/file.rb, line 7
def all(path)
  CodeKindly::Utils::Dir.all path
end
choose_from_options(dir_path, h_l = nil) click to toggle source
# File lib/code_kindly/utils/file.rb, line 11
def choose_from_options(dir_path, h_l = nil)
  require 'highline'
  h_l ||= HighLine.new
  file_opts = file_options(dir_path)
  return nil if CodeKindly::Utils::Presence.blank? file_opts

  msg = file_opts.map { |k, v| "\n  #{k}: #{v}" } + ["\n  0: None"]
  option = h_l.ask("Select a file:#{msg.join}", Integer)
  file_path = file_opts.fetch(option, nil)
  return if file_path.nil?

  ::File.join(dir_path, file_path)
end
file_options(path) click to toggle source
# File lib/code_kindly/utils/file.rb, line 25
def file_options(path)
  require 'map'
  options = Map.new
  key = 0
  find(path).each do |file|
    options[key += 1] = file
  end
  options
end
find(path) click to toggle source
# File lib/code_kindly/utils/file.rb, line 35
def find(path)
  require 'fileutils'
  all(path).select { |entry| ::File.file?("#{path}/#{entry}") }
end
trash!(file_string) click to toggle source

move to trash (or delete) existing downloaded files sudo gem install osx-trash (www.dribin.org/dave/blog/archives/2008/05/24/osx_trash/)

# File lib/code_kindly/utils/file.rb, line 42
def trash!(file_string)
  command = command_to_trash_files(file_string)
  return if command.nil?

  Kernel.system(command)
end

Private Class Methods

command_to_trash_files(file_string) click to toggle source
# File lib/code_kindly/utils/file.rb, line 51
def command_to_trash_files(file_string)
  return if Command.run("ls #{file_string}").result.nil?

  trash = OS.which('trash')
  if trash then "#{trash.chomp} #{file_string}"
  elsif ::File.directory?('~/.Trash') then "mv #{file_string} ~/.Trash"
  else "rm #{file_string}"
  end
end