Class: Defog::Handle

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

Overview

Create a Defog::Handle proxy instance via Defog::Proxy#file, such as

defog = Defog::Proxy.new(:provider => :AWS, :aws_access_key_id => access_key, ...)

handle = defog.file("key/to/my/file")

or

defog.file("key/to/my/file") do |handle|
   # ... access the proxy handle ...
end

The #proxy_path attribute method returns a Pathname giving the local proxy file location. Querying the attribute does not upload, download, synchronize, or otherwise interact with the cloud or local proxy file in any way – it just returns a constructed Pathname. The proxy_path is a deterministic function of the cloud key and Defog::Proxy#proxy_root, so you can rely on it not changing between independent accesses to a cloud file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy, key) ⇒ Handle

:nodoc:



32
33
34
35
36
# File 'lib/defog/handle.rb', line 32

def initialize(proxy, key) #:nodoc:
  @proxy = proxy
  @key = key
  @proxy_path = Pathname.new("#{@proxy.proxy_root}/#{@key}").expand_path
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



26
27
28
# File 'lib/defog/handle.rb', line 26

def key
  @key
end

#proxyObject (readonly)

:nodoc:



27
28
29
# File 'lib/defog/handle.rb', line 27

def proxy
  @proxy
end

#proxy_pathObject (readonly)

Pathname where proxy file is, was, or will be located.



30
31
32
# File 'lib/defog/handle.rb', line 30

def proxy_path
  @proxy_path
end

Instance Method Details

#deleteObject

Deletes the remote cloud file if it exists



44
45
46
# File 'lib/defog/handle.rb', line 44

def delete
  fog_model.andand.destroy
end

#exist?Boolean

Returns true if the remote cloud file exists

Returns:

  • (Boolean)


39
40
41
# File 'lib/defog/handle.rb', line 39

def exist?
  !!fog_model
end

#fog_modelObject

Returns the underlying Fog::Model, should you need it for something.



75
76
77
# File 'lib/defog/handle.rb', line 75

def fog_model
  @proxy.fog_wrapper.fog_head(@key)
end

#last_modifiedObject

Returns the modification date of the remote cloud file, or nil if it doesn’t exist



55
56
57
# File 'lib/defog/handle.rb', line 55

def last_modified
  fog_model.andand.last_modified
end

#open(mode, opts = {}, &block) ⇒ Object

Returns a Defog::File object, which is a specialization of ::File.

mode can be “r”, “r+”, “w”, “w+”, “a”, or “a+” with the usual semantics. When opened in a readable mode (“r”, “r+”, “w+”, “a+”), first caches the cloud file in the local proxy. When opened in a writeable mode (“r+”, “w”, “w+”, “a”, “a+”), arranges to upload the changes back to the cloud file at close time.

Like ::File.open, if called with a block yields the file object to the block and ensures the file will be closed when leaving the block.

Normally upon close the proxy file is synchronized as needed and then deleted. Pass

:persist => true

to suppress deleting the file and so maintain the file after closing. See File#close for more details.



95
96
97
98
# File 'lib/defog/handle.rb', line 95

def open(mode, opts={}, &block)
  opts = opts.keyword_args(:persist => @proxy.persist)
  File.open(opts.merge(:handle => self, :mode => mode), &block)
end

#sizeObject

Returns the size of the remote cloud file, or nil if it doesn’t exist



49
50
51
# File 'lib/defog/handle.rb', line 49

def size
  fog_model.andand.content_length
end

#url(opts = {}) ⇒ Object

Returns a URL to access the remote cloud file.

The option

:expiry => time

Specifies the expiration of time-limited URLS when using :AWS. The default is Time.now + 10.minutes. The expiry is ignored when using :local

For :local cloud files, if Rails is defined and the file is in the Rails app’s public directory, returns a site path relative to the public directory. Otherwise returns a "file://" URL



69
70
71
72
# File 'lib/defog/handle.rb', line 69

def url(opts={})
  opts = opts.keyword_args(:expiry => Time.now + 10*60)
  @proxy.fog_wrapper.url(@key, opts.expiry)
end