class BFS::Bucket::Abstract

Attributes

encoding[R]
perm[R]

Public Class Methods

new(encoding: Encoding.default_external, perm: nil, **_opts) click to toggle source

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
open(*args, **opts) { |bucket| ... } click to toggle source

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

close() click to toggle source

Closes the underlying connection

# File lib/bfs/bucket/abstract.rb, line 108
def close; end
cp(src, dst, **opts) click to toggle source

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
create(_path, **_opts) click to toggle source

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
glob(_pattern = '**', **_opts) click to toggle source

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(_path, **_opts) click to toggle source

Info returns the info for a single file

# File lib/bfs/bucket/abstract.rb, line 49
def info(_path, **_opts)
  raise 'not implemented'
end
ls(_pattern = '**', **_opts) click to toggle source

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
mv(src, dst, **_opts) click to toggle source

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
open(_path, **_opts) click to toggle source

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
read(path, **opts) click to toggle source

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
rm(_path, **_opts) click to toggle source

Deletes a file.

# File lib/bfs/bucket/abstract.rb, line 65
def rm(_path, **_opts)
  raise 'not implemented'
end
write(path, data, **opts) click to toggle source

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

full_path(path) click to toggle source
# 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
norm_meta(meta) click to toggle source
# 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
norm_path(path) click to toggle source
# File lib/bfs/bucket/abstract.rb, line 121
def norm_path(path)
  BFS.norm_path(path) if path
end
trim_prefix(path) click to toggle source
# 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