Class: VimPK::Commands::Move

Inherits:
Object
  • Object
show all
Defined in:
lib/vimpk/commands/move.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Move

Returns a new instance of Move.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
# File 'lib/vimpk/commands/move.rb', line 8

def initialize(name, options)
  raise ArgumentError, "New pack or type is required" unless options.pack || options.type
  @name = name || raise(ArgumentError, "Package name is required")
  @path = options.path
  @pack = options.pack
  @type = options.type
end

Instance Attribute Details

#destObject (readonly)

Returns the value of attribute dest.



6
7
8
# File 'lib/vimpk/commands/move.rb', line 6

def dest
  @dest
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vimpk/commands/move.rb', line 16

def call
  glob = Dir.glob(File.join(@path, "*", "{start,opt}", @name))

  if glob.empty?
    raise PackageNotFoundError, "Package #{@name} not found in #{@path}."
  elsif glob.size > 1
    raise MultiplePackagesFoundError, "Multiple packages #{@name} found in #{glob.join(" and ")}."
  else
    source = glob.first
    current_type = File.basename(File.dirname(source))
    current_pack = File.basename(File.dirname(File.dirname(source)))
    @dest = File.join(@path, @pack || current_pack, @type || current_type, @name)

    if File.exist?(dest)
      raise ArgumentError, "Package #{@name} already exists in #{dest}."
    else
      FileUtils.mkdir_p(File.dirname(dest))
    end

    FileUtils.mv(source, dest)
  end
end