class BFS::Bucket::Abstract
Attributes
Public Class Methods
Initializes a new bucket @param [Hash] opts options @option opts [String] :encoding Custom encoding. Default: Encoding.default_external. @option opts [Integer] :perm optional file permissions. Default: 0600.
# File lib/bfs/bucket/abstract.rb, line 25 def initialize(encoding: Encoding.default_external, perm: nil, **_opts) @encoding = encoding case perm when Integer @perm = perm when String @perm = perm.to_i(8) end BFS.defer(self, :close) end
Behaves like new, but accepts an optional block. If a block is given, buckets are automatically closed after the block is yielded.
# File lib/bfs/bucket/abstract.rb, line 10 def self.open(*args, **opts) bucket = new(*args, **opts) return bucket unless block_given? begin yield bucket ensure bucket.close end end
Public Instance Methods
Closes the underlying connection
# File lib/bfs/bucket/abstract.rb, line 108 def close; end
Copies src to dst
@param [String] src The source path. @param [String] dst The destination path.
# File lib/bfs/bucket/abstract.rb, line 90 def cp(src, dst, **opts) self.open(src, **opts) do |r| create(dst, **opts) do |w| IO.copy_stream(r, w) end end end
Creates a new file and opens it for writing
# File lib/bfs/bucket/abstract.rb, line 54 def create(_path, **_opts) raise 'not implemented' end
Iterates over the contents of a bucket using a glob pattern
# File lib/bfs/bucket/abstract.rb, line 44 def glob(_pattern = '**', **_opts) raise 'not implemented' end
Info returns the info for a single file
# File lib/bfs/bucket/abstract.rb, line 49 def info(_path, **_opts) raise 'not implemented' end
Lists the contents of a bucket using a glob pattern
# File lib/bfs/bucket/abstract.rb, line 39 def ls(_pattern = '**', **_opts) raise 'not implemented' end
Moves src to dst
@param [String] src The source path. @param [String] dst The destination path.
# File lib/bfs/bucket/abstract.rb, line 102 def mv(src, dst, **_opts) cp(src, dst) rm(src) end
Opens an existing file for reading May raise BFS::FileNotFound
# File lib/bfs/bucket/abstract.rb, line 60 def open(_path, **_opts) raise 'not implemented' end
Shortcut method to read the contents of a file into memory
@param [String] path The path to read from. @param [Hash] opts Additional options, see open
.
# File lib/bfs/bucket/abstract.rb, line 73 def read(path, **opts) self.open(path, **opts, &:read) end
Deletes a file.
# File lib/bfs/bucket/abstract.rb, line 65 def rm(_path, **_opts) raise 'not implemented' end
Shortcut method to write data to path
@param [String] path The path to write to. @param [String] data The data to write. @param [Hash] opts Additional options, see create
.
# File lib/bfs/bucket/abstract.rb, line 82 def write(path, data, **opts) create(path, **opts) {|f| f.write data } end
Protected Instance Methods
# File lib/bfs/bucket/abstract.rb, line 125 def full_path(path) path = norm_path(path) path = File.join(@prefix, path) if @prefix path end
# File lib/bfs/bucket/abstract.rb, line 112 def norm_meta(meta) norm = {} meta.each do |key, value| nkey = key.to_s.downcase.split('-').map(&:capitalize).join('-') norm[nkey] = value end if meta.is_a?(Hash) norm end
# File lib/bfs/bucket/abstract.rb, line 121 def norm_path(path) BFS.norm_path(path) if path end
# File lib/bfs/bucket/abstract.rb, line 131 def trim_prefix(path) path.slice!(0, @prefix.size) if @prefix && path.slice(0, @prefix.size) == @prefix path end