Class: IMW::Tools::Transferer

Inherits:
Object
  • Object
show all
Defined in:
lib/imw/tools/transferer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, source, destination) ⇒ Transferer

Returns a new instance of Transferer.

Raises:



7
8
9
10
11
12
# File 'lib/imw/tools/transferer.rb', line 7

def initialize action, source, destination
  @action      = normalize_action(action)
  @source      = IMW.open(source)
  @destination = IMW.open(destination)
  raise IMW::PathError.new("Source and destination have the same URI: #{@source.uri}") if @source.uri.to_s == @destination.uri.to_s
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



5
6
7
# File 'lib/imw/tools/transferer.rb', line 5

def action
  @action
end

#destinationObject

Returns the value of attribute destination.



5
6
7
# File 'lib/imw/tools/transferer.rb', line 5

def destination
  @destination
end

#sourceObject

Returns the value of attribute source.



5
6
7
# File 'lib/imw/tools/transferer.rb', line 5

def source
  @source
end

Instance Method Details

#transfer!Object

Transfer source to destination.

For local transfer, will raise errors unless the necessary paths exist.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/imw/tools/transferer.rb', line 18

def transfer!
  if source.is_local? 
    source.should_exist!("Cannot #{action}") 
    source_scheme = 'file'                   
  else
    source_scheme = source.scheme
  end
  
  if destination.is_local?
    destination.dir.should_exist!("Cannot #{action}")          
    destination_scheme = 'file'
  else
    destination_scheme = destination.scheme
  end
  
  method             = "#{source_scheme}_to_#{destination_scheme}"
  
  if respond_to?(method)
    send(method)
  else
    raise IMW::NoMethodError.new("Do not know how to #{action} #{source.uri} => #{destination.uri} (#{source_scheme.inspect} => #{destination_scheme.inspect})")
  end
  destination.reopen
end