Class: Grabass::Selection

Inherits:
Object
  • Object
show all
Defined in:
lib/grabass.rb

Instance Method Summary collapse

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, options)
  selection = Dir[File.join source_dir, @glob]

  if selection.length == 0
    puts "No files selected with glob \"#{@glob}\"" unless options[:quiet]
    return false
  elsif selection.length == 1
    puts "Moving \"#{File.basename selection[0]}\" to #{@destination}" unless options[:quiet]

    if File.exists? @destination
      if options[:force]
        if File.directory?(selection[0]) == File.directory?(@destination)
          puts "Removing existing \"#{@destination}\"..." unless options[:quiet]
          FileUtils.rm_rf @destination
        end
      else
        puts "#{@destination} already exists. Use --force to replace." unless options[: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 options[:quiet]

    failures = 0
    selection.each do |file|
      destination_file = File.join @destination, file.sub(source_dir, '')
      if File.exists? destination_file
        if options[:force]
          if File.directory?(file) == File.directory?(destination_file)
            puts "Removing \"#{destination_file}\"..." unless options[:quiet]
            FileUtils.rm_rf destination_file
          end
        else
          puts "#{destination_file} already exists. Use --force to replace." unless options[:quiet]
          failures += 1
        end
      end
    end

    return false if failures > 0

    FileUtils.mkdir_p @destination
    FileUtils.mv selection, @destination
  end

  true
end