Class: CloudSync::Resource

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Resource

Returns a new instance of Resource.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cloud_sync/resource.rb', line 13

def initialize uri
  raise "Invalid resource string #{uri}" unless uri =~ /\w+:/
  
  @uri = uri
  @type, @path = uri.split(":")
  @type = @type.to_sym
  
  raise "Invalid medium type: #{@type}" unless MediaTypes.has_key?(@type)
    
  @medium = MediaTypes[@type].new
end

Instance Attribute Details

#mediumObject

Returns the value of attribute medium.



11
12
13
# File 'lib/cloud_sync/resource.rb', line 11

def medium
  @medium
end

#pathObject

Returns the value of attribute path.



11
12
13
# File 'lib/cloud_sync/resource.rb', line 11

def path
  @path
end

#typeObject

Returns the value of attribute type.



11
12
13
# File 'lib/cloud_sync/resource.rb', line 11

def type
  @type
end

#uriObject

Returns the value of attribute uri.



11
12
13
# File 'lib/cloud_sync/resource.rb', line 11

def uri
  @uri
end

Instance Method Details

#copy(to) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cloud_sync/resource.rb', line 29

def copy to
  
  raise "Resource #{@uri} does not exist" unless exists?

  input  = reader
  output = to.writer
  output.write(input.read)
  
  input.close if input.respond_to? :close
  output.close if output.respond_to? :close
end

#exists?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/cloud_sync/resource.rb', line 25

def exists?
  @medium.exists?(@path)
end

#readerObject



45
46
47
# File 'lib/cloud_sync/resource.rb', line 45

def reader
  @medium.reader @path
end

#writerObject



41
42
43
# File 'lib/cloud_sync/resource.rb', line 41

def writer
  @medium.writer @path
end