lib/defog/file.rb in defog-0.3.0 vs lib/defog/file.rb in defog-0.3.1

- old
+ new

@@ -1,7 +1,8 @@ module Defog - # Create a Defog::File proxy instance via Defog::Proxy#file, such as + # Create a Defog::File proxy instance via Defog::Handle#open or via the + # shortcut from Defog::Proxy#file, such as # # defog = Defog::Proxy.new(:provider => :AWS, :aws_access_key_id => access_key, ...) # # defog.file("key/to/my/file", "w") do |file| # # ... access the proxy file ... @@ -26,23 +27,34 @@ # system("convert souce.png -scale 100x100 #{file.path}") # end # # (Note that the proxy file path has the same file extension as the cloud key string.) # - # Upon closing the proxy file, in normal use the cloud storage gets synchronized and - # the proxy deleted. See File#close for more details. + # Upon closing the proxy file, in normal use the cloud storage gets + # synchronized if needed and the proxy deleted. To prevent deletion, you + # can use: + # defog.file("key", "r", :persist => true) + # See File#close for more details. + # + # If you are managing your cache size, when opening a proxy for writing you may want to provide a hint as + # to the expected size of the data: + # defog.file("key", "w", :size_hint => 500.kilobytes) + # See README for more details. + # class File < ::File def initialize(opts={}, &block) #:nodoc: opts = opts.keyword_args(:handle => :required, :mode => :required, :persist => :optional, :size_hint => :optional) @handle = opts.handle @persist = opts.persist key = @handle.key proxy_path = @handle.proxy_path proxy_path.dirname.mkpath - case opts.mode + re_encoding = /(b|:.*)$/ + @encoding = opts.mode.match(re_encoding).to_s + case opts.mode.sub(re_encoding,'') when "r" download = true @upload = false cache_size = @handle.size when "w", "w+" @@ -62,14 +74,14 @@ download_proxy if download super(proxy_path, opts.mode, &block) end def download_proxy - @handle.proxy.fog_wrapper.get_file(@handle.key, @handle.proxy_path) + @handle.proxy.fog_wrapper.get_file(@handle.key, @handle.proxy_path, @encoding) end def upload_proxy - @handle.proxy.fog_wrapper.put_file(@handle.key, @handle.proxy_path) + @handle.proxy.fog_wrapper.put_file(@handle.key, @handle.proxy_path, @encoding) end # Closes the proxy file and synchronizes the cloud storage (if it was # opened as writeable) then deletes the proxy file.