Class: CodeKindly::Utils::File

Inherits:
Object
  • Object
show all
Defined in:
lib/code_kindly/utils/file.rb

Class Method Summary collapse

Class Method Details

.all(path) ⇒ Object



7
8
9
# 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) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# 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) ⇒ Object



25
26
27
28
29
30
31
32
33
# 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) ⇒ Object



35
36
37
38
# 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) ⇒ Object

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



42
43
44
45
46
47
# 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