Class: Dens::Mover

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

Constant Summary collapse

OriginDoesNotExistError =
Class.new(RuntimeError)
DestinationDoesNotExistError =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin, destination = Dir.pwd) ⇒ Mover

Returns a new instance of Mover.



17
18
19
20
# File 'lib/dens.rb', line 17

def initialize(origin, destination = Dir.pwd)
  @origin = origin
  @destination = destination
end

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



12
13
14
# File 'lib/dens.rb', line 12

def destination
  @destination
end

#originObject (readonly)

Returns the value of attribute origin.



12
13
14
# File 'lib/dens.rb', line 12

def origin
  @origin
end

Class Method Details

.move(origin, destination) ⇒ Object



22
23
24
# File 'lib/dens.rb', line 22

def self.move(origin, destination)
  Mover.new(origin, destination).move
end

Instance Method Details

#moveObject



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
# File 'lib/dens.rb', line 26

def move
  raise OriginDoesNotExistError unless Dir.exists?(origin)
  raise DestinationDoesNotExistError unless Dir.exists?(destination)

  Dir.glob(File.join(origin, "*.png")).each do |drawable_origin|
    drawable_extension = File.extname(drawable_origin)
    drawable_name = File.basename(drawable_origin, drawable_extension)

    partitions = drawable_name.partition("-")
    density = partitions.last
    name = partitions.first

    drawable_destination = File.join(destination, "drawable", name)
    if (!density.empty?)
      directory = File.join(destination, "drawable-#{density}")
      if File.exists?(directory)
        drawable_destination = File.join(directory, name)
      end
    end
    drawable_destination += drawable_extension

    puts "#{drawable_origin} => #{drawable_destination}"
    FileUtils.move(drawable_origin, drawable_destination)
  end
end