Module: FileCrawler::Finder::Command::Move
- Included in:
- FileCrawler::Finder
- Defined in:
- lib/file_crawler/finder/command/move.rb
Instance Method Summary collapse
- #create_directory_if_needed(directory) ⇒ Object
- #exist_file?(current, destination) ⇒ Boolean
- #find_free_filename(current, destination) ⇒ Object
- #is_same?(current, destination) ⇒ Boolean
- #move(destination) ⇒ Object
- #move_from_collection(destination) ⇒ Object
- #move_from_collection_with_numbering(source, destination) ⇒ Object
- #move_with_numbering(source, destination) ⇒ Object
- #valiable_to_move?(current, destination) ⇒ Boolean
Instance Method Details
#create_directory_if_needed(directory) ⇒ Object
70 71 72 |
# File 'lib/file_crawler/finder/command/move.rb', line 70 def create_directory_if_needed(directory) Dir.mkdir(directory, 0777) unless File.exist?(directory) end |
#exist_file?(current, destination) ⇒ Boolean
97 98 99 100 |
# File 'lib/file_crawler/finder/command/move.rb', line 97 def exist_file?(current, destination) next_path = destination + '/' + File.basename(current) File.exist?(next_path) end |
#find_free_filename(current, destination) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/file_crawler/finder/command/move.rb', line 74 def find_free_filename(current, destination) filename = File.basename(current) index = 1 new_filename = "#{filename} (#{index})" while exist_file?(new_filename, destination) index += 1 new_filename = "#{filename} (#{index})" end new_filename end |
#is_same?(current, destination) ⇒ Boolean
93 94 95 |
# File 'lib/file_crawler/finder/command/move.rb', line 93 def is_same?(current, destination) current == destination end |
#move(destination) ⇒ Object
8 9 10 11 12 |
# File 'lib/file_crawler/finder/command/move.rb', line 8 def move(destination) tap { @rows = move_with_numbering(@rows, destination) } end |
#move_from_collection(destination) ⇒ Object
14 15 16 17 18 |
# File 'lib/file_crawler/finder/command/move.rb', line 14 def move_from_collection(destination) tap { @rows = move_from_collection_with_numbering(@rows, destination) } end |
#move_from_collection_with_numbering(source, destination) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/file_crawler/finder/command/move.rb', line 61 def move_from_collection_with_numbering(source, destination) result = source.map {|key, value| directory = destination + '/' + key move_with_numbering(value, directory) }.flatten result end |
#move_with_numbering(source, destination) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/file_crawler/finder/command/move.rb', line 20 def move_with_numbering(source, destination) move_targets = [] not_move_targets = [] rename_targets = [] source.each {|directory| if is_same?(directory, destination) not_move_targets << directory next end if exist_file?(directory, destination) rename_targets << directory next end if move_targets.include?(directory) rename_targets << directory next end move_targets << directory } create_directory_if_needed(destination) FileUtils.mv(move_targets, destination) renamed_targets = [] rename_targets.each {|directory| filename = find_free_filename(directory, destination) to = destination + '/' + filename FileUtils.mv(directory, to) renamed_targets << to } move_targets.map {|directory| destination + '/' + File.basename(directory) } + not_move_targets + renamed_targets end |
#valiable_to_move?(current, destination) ⇒ Boolean
87 88 89 90 91 |
# File 'lib/file_crawler/finder/command/move.rb', line 87 def valiable_to_move?(current, destination) return false if is_same?(current, destination) !exist_file?(current, destination) end |