Class: AtticCleanup::Storage::StoreFiles

Inherits:
Object
  • Object
show all
Defined in:
lib/attic-cleanup/storage/store_files.rb

Instance Method Summary collapse

Instance Method Details

#bundle(input) ⇒ Object

Puts the string of numbers in an array and removes the ‘,’



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/attic-cleanup/storage/store_files.rb', line 39

def bundle(input)
  bundle = input
  bundle = bundle.scan( /\d+/ )
  bundle.map!{ |bundle| bundle.to_i }
  @bundle_items = bundle
  @bundle_count = bundle.count
  
  # If the user typed 'all' instead of numbers, every file will
  # be set into the the array.
  if @input == "all"
    @bundle_items = []
    p = 0
    folder_files = AtticCleanup::Storage::FolderFiles.new
    folder_files.all_files = location
    folder_files.all_files.each do
      @bundle_items[p] = p
      p += 1
      @bundle_count = "all"
    end
  end
end

#checkObject

Checks if the selected path exists



31
32
33
34
35
36
# File 'lib/attic-cleanup/storage/store_files.rb', line 31

def check
  if !File.directory? @location
    puts "Invalid path "  + @location
    exit 1
  end
end

#inputObject

Easy method for input



7
8
9
10
# File 'lib/attic-cleanup/storage/store_files.rb', line 7

def input
  STDOUT.flush
  @input = $stdin.gets.chomp
end

#locationObject



12
13
14
# File 'lib/attic-cleanup/storage/store_files.rb', line 12

def location
  @location
end

#location=(value) ⇒ Object

Sets the location of the store method If the location wasn’t set, the default location will be used from the default_path.txt file



19
20
21
22
23
24
25
26
27
28
# File 'lib/attic-cleanup/storage/store_files.rb', line 19

def location= ( value )
  if value == ""
    value = AtticCleanup::Path::Custom.default
  elsif value == "."
    Dir.chdir(".")
    value = Dir.getwd()
    puts Dir.getwd()
  end
  @location = value
end

#store(a, f) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/attic-cleanup/storage/store_files.rb', line 62

def store(a, f)
  folder_files = AtticCleanup::Storage::FolderFiles.new
  # Selects all files from the current location
  folder_files.all_files = location
  
  # If option a is nil, the files will have to be selected manually
  if a == nil
    i = 1
    while i == 1
      # Message with the current location and simple instructions
      puts "\n"+location
      puts "Which files do you want to store in your attic?"
      puts "Type ls for the list of items"
      puts "Select the items by id. Example: 1,3,5"
      input()
  
      file_id = 0
      # If input is "all" all the files and folders will be added to the array
      if @input == "all"
        @bundle_count = "all"
        bundle("1")
        i = 0
      # If input is "ls" all the files and folder will be shown with their IDs
      elsif @input == "ls"
        folder_files.all_files.each do |k|
          puts file_id.to_s + " - " + AtticCleanup::Storage::FolderFiles.find(folder_files.all_files, file_id)
          file_id += 1
        end
      elsif @input == "exit"
        exit 1
      else
        bundle(@input)
        i = 0
      end
    end
  else
    # If option a was selected, all files will be added to the array
    @bundle_count = "all"
    @input = "all"
    bundle(@input)
  end
    
  if f == nil
    i = 1
    # If no items were selected, go back to the input
    if @bundle_count == 0
      puts "\n\nNo file was selected."
      store(nil, nil)
    end
    while i == 1
      # If option f was not selected, you will get a warning before moving the files
      puts "\nAre you sure you want to move #{@bundle_count} files/folders?\n [y/n]"
      input()
      if @input == "y"
        i = 0
      elsif @input == "n"
        exit 1
      else
        puts "y or n?"
      end
    end
  end

  # Moves each file to the MyAttic folder
  @bundle_items.each do |b|
    selected_file = File.join( location, AtticCleanup::Storage::FolderFiles.find(folder_files.all_files, b) )
    # If the file wasn't found, print error
    if AtticCleanup::Storage::FolderFiles.find(folder_files.all_files, b) == nil
      puts "File ID " + b.to_s + " was not found"
    elsif AtticCleanup::Path::Custom.check_ignore(selected_file) == true
      puts "Ignored #{selected_file}"
    else
      # If file was found move it, print it and record it in the log
      puts "Moving " + selected_file + " to your attic.."
      AtticCleanup::Log.save(selected_file, MyAttic::TODAY)
      FileUtils.mv(selected_file, MyAttic::TODAY)
    end
  end
  puts "The files have been moved to " + MyAttic::TODAY
end