Class: FileCrawler::Finder::Command::Move::Fixer

Inherits:
Object
  • Object
show all
Defined in:
lib/file_crawler/finder/command/move.rb

Instance Method Summary collapse

Instance Method Details

#fix_path(filepath, check_array, index = 0) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/file_crawler/finder/command/move.rb', line 65

def fix_path(filepath, check_array, index=0)
  newpath = filepath
  newpath = "#{filepath} (#{index})" if index > 0
  return fix_path(filepath, check_array, index + 1) if exist?(newpath, check_array)

  newpath
end

#make_fixed_paths(filepaths) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/file_crawler/finder/command/move.rb', line 54

def make_fixed_paths(filepaths)
  dest = []
  filepaths.each {|filepath|
    fixed_path = fix_path(filepath[1], dest)
    filepath[1] = fixed_path
    dest << fixed_path
  }

  filepaths
end

#make_mv(filepaths) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/file_crawler/finder/command/move.rb', line 43

def make_mv(filepaths)
  cmds = []
  mkdirs = []
  make_fixed_paths(filepaths).map {|file|
    mkdirs << "mkdir -p #{File.dirname(file[1])}"
    cmds << "mv #{file[0]} #{file[1]}"
  }

  mkdirs.uniq + cmds
end

#make_new_path(sources, destination) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/file_crawler/finder/command/move.rb', line 30

def make_new_path(sources, destination)
  case sources
  when Hash
    return make_new_path_for_collection(sources, destination)
  when Array
    return make_new_path_for_array(sources, destination)
  when String
    return make_new_path_for_array([sources], destination)
  end

  ArgumentError
end