Class: Grabass::Selection
- Inherits:
-
Object
- Object
- Grabass::Selection
- Defined in:
- lib/grabass.rb
Instance Method Summary collapse
-
#initialize(glob, destination) ⇒ Selection
constructor
A new instance of Selection.
- #make_from(source_dir, options) ⇒ Object
Constructor Details
#initialize(glob, destination) ⇒ Selection
Returns a new instance of Selection.
106 107 108 |
# File 'lib/grabass.rb', line 106 def initialize(glob, destination) @glob, @destination = glob, destination end |
Instance Method Details
#make_from(source_dir, options) ⇒ Object
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/grabass.rb', line 110 def make_from(source_dir, ) selection = Dir[File.join source_dir, @glob] if selection.length == 0 puts "No files selected with glob \"#{@glob}\"" unless [:quiet] return false elsif selection.length == 1 puts "Moving \"#{File.basename selection[0]}\" to #{@destination}" unless [:quiet] if File.exists? @destination if [:force] if File.directory?(selection[0]) == File.directory?(@destination) puts "Removing existing \"#{@destination}\"..." unless [:quiet] FileUtils.rm_rf @destination end else puts "#{@destination} already exists. Use --force to replace." unless [:quiet] return false end end FileUtils.mkdir_p File.dirname @destination FileUtils.mv selection[0], @destination else puts "Moving #{selection.length} \"#{@glob}\" to #{@destination}" unless [:quiet] failures = 0 selection.each do |file| destination_file = File.join @destination, file.sub(source_dir, '') if File.exists? destination_file if [:force] if File.directory?(file) == File.directory?(destination_file) puts "Removing \"#{destination_file}\"..." unless [:quiet] FileUtils.rm_rf destination_file end else puts "#{destination_file} already exists. Use --force to replace." unless [:quiet] failures += 1 end end end return false if failures > 0 FileUtils.mkdir_p @destination FileUtils.mv selection, @destination end true end |