Module: DsUtils

Defined in:
lib/ds_utils.rb,
lib/ds_utils/version.rb

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.create_do_fileObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ds_utils.rb', line 26

def self.create_do_file
  @do_file = "flatten_directory_#{timestamp}.rb"
  
  File.open(@do_file, 'w', 0744) {|file|
    file.puts "#!/usr/bin/env ruby"
    @move_files.each {|ll|
      file.puts "File.rename '#{ll}', '#{move_to_name(ll)}'"
    }
    @subdirectories.sort{|a,b| b.count(File::SEPARATOR) - a.count(File::SEPARATOR)}.each {|sd|
      file.puts "Dir.rmdir('#{sd}')"
    }
  }
  
end

.create_undo_fileObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ds_utils.rb', line 41

def self.create_undo_file
  @undo_file = "unflatten_directory_#{timestamp}.rb"
  
  File.open(@undo_file, 'w', 0744) {|file|
    file.puts "#!/usr/bin/env ruby"
    file.puts "require 'fileutils'"
    @subdirectories.each {|sd|
      file.puts "FileUtils.mkdir_p '#{sd}' unless Dir.exist?('#{sd}')"
    } 
    @move_files.each {|ll|
      file.puts "File.rename '#{move_to_name(ll)}', '#{ll}'"
    }
  }
end

.flatten(thisdir) ⇒ Object

Move tiles from subdirectories of topdir to topdir



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ds_utils.rb', line 5

def self.flatten(thisdir)
  @do_listing = []
  @undo_listing = []
  @move_files = []
  
  dir_contents = Dir["#{thisdir}/**/*"]
  @move_files = dir_contents.reject{|item| File.directory?(item)}
  @subdirectories = dir_contents.select{|item| File.directory?(item)}
 
  create_do_file
  create_undo_file
  
  puts "Please examine the contents of file #{@do_file} and then execute it with the command:"
  puts "          ./#{@do_file}"
  puts "To undo the changes, please execute:"
  puts "          ./#{@undo_file}"

  return [@do_file, @undo_file]
end

.move_to_name(filename) ⇒ Object



56
57
58
59
# File 'lib/ds_utils.rb', line 56

def self.move_to_name(filename)
  split_string_array = filename.split(File::SEPARATOR)
  return split_string_array[0] + File::SEPARATOR + split_string_array[1..-1].join('_')
end

.timestampObject



61
62
63
# File 'lib/ds_utils.rb', line 61

def self.timestamp
  return Time.now.strftime("%Y_%m_%d_%Y%m%dT%H%M%S%z")
end