Class: CoralBackup::FileSelector

Inherits:
Object
  • Object
show all
Defined in:
lib/coral_backup/file_selector.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileSelector

Returns a new instance of FileSelector.



8
9
10
# File 'lib/coral_backup/file_selector.rb', line 8

def initialize
  @files = []
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



6
7
8
# File 'lib/coral_backup/file_selector.rb', line 6

def files
  @files
end

Class Method Details

.selectObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/coral_backup/file_selector.rb', line 22

def self.select
  file_selector = new
  Readline.completion_proc = Readline::FILENAME_COMPLETION_PROC
  while buf = Readline.readline("> ")
    expanded = Shellwords.split(buf)
    warn "WARNING: #{expanded.length} files are being added:" unless expanded.length == 1

    expanded.each do |ex|
      begin
        file_selector.add_file(ex)
      rescue Errno::ENOENT => e
        warn e
      end
    end
  end

  file_selector.files.uniq
end

.single_selectObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/coral_backup/file_selector.rb', line 42

def self.single_select
  file_selector = new
  Readline.completion_proc = Readline::FILENAME_COMPLETION_PROC
  file_added = false
  while !file_added && buf = Readline.readline("> ")
    expanded = Shellwords.split(buf)
    warn "WARNING: #{expanded.length} files are being added:" unless expanded.length == 1
    expanded.each do |ex|
      begin
        file_selector.add_file(ex)
      rescue Errno::ENOENT => e
        warn e
      else
        file_added = true
      end
    end
  end

  raise "Wrong number of files (#{file_selector.files.length} for 1)" unless file_selector.files.length == 1

  file_selector.files[0]
end

Instance Method Details

#add_file(filename) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/coral_backup/file_selector.rb', line 12

def add_file(filename)
  if FileTest.exist?(filename)
    expanded_filename = File.expand_path(filename)
    @files << File.expand_path(expanded_filename)
    warn expanded_filename
  else
    raise Errno::ENOENT, filename
  end
end