class BFS::Bucket::InMem

InMem buckets are useful for tests

Constants

Entry

Public Class Methods

new(**opts) click to toggle source
Calls superclass method BFS::Bucket::Abstract::new
# File lib/bfs/bucket/in_mem.rb, line 26
def initialize(**opts)
  super(**opts.dup)
  @files = {}
end

Public Instance Methods

clear() click to toggle source

Reset bucket and clear all files.

# File lib/bfs/bucket/in_mem.rb, line 32
def clear
  @files.clear
end
create(path, encoding: self.encoding, content_type: nil, metadata: nil, **_opts, &block) click to toggle source

Creates a new file and opens it for writing.

@param [String] path The creation path. @param [Hash] opts Additional options. @option opts [String] :encoding Custom encoding. @option opts [String] :content_type Custom content type. @option opts [Hash] :metadata Metadata key-value pairs.

# File lib/bfs/bucket/in_mem.rb, line 69
def create(path, encoding: self.encoding, content_type: nil, metadata: nil, **_opts, &block)
  Writer.new(encoding: encoding) do |wio|
    @files[norm_path(path)] = Entry.new(wio, Time.now, content_type, norm_meta(metadata))
  end.perform(&block)
end
glob(pattern = '**/*', **_opts) click to toggle source

Iterates over the contents of a bucket using a glob pattern

# File lib/bfs/bucket/in_mem.rb, line 46
def glob(pattern = '**/*', **_opts)
  Enumerator.new do |y|
    @files.each_key do |path|
      y << file_info(path) if File.fnmatch?(pattern, path, File::FNM_PATHNAME)
    end
  end
end
info(path, **_opts) click to toggle source

Info returns the file info

# File lib/bfs/bucket/in_mem.rb, line 55
def info(path, **_opts)
  path = norm_path(path)
  raise BFS::FileNotFound, path unless @files.key?(path)

  file_info(path)
end
ls(pattern = '**/*', **_opts) click to toggle source

Lists the contents of a bucket using a glob pattern

# File lib/bfs/bucket/in_mem.rb, line 37
def ls(pattern = '**/*', **_opts)
  Enumerator.new do |y|
    @files.each_key do |path|
      y << path if File.fnmatch?(pattern, path, File::FNM_PATHNAME)
    end
  end
end
open(path, **_opts) { |io| ... } click to toggle source

Opens an existing file for reading

# File lib/bfs/bucket/in_mem.rb, line 76
def open(path, **_opts, &block)
  path = norm_path(path)
  raise BFS::FileNotFound, path unless @files.key?(path)

  io = @files[path].io
  io.reopen(io.string)
  return io unless block

  begin
    yield(io)
  ensure
    io.close
  end
end
rm(path, **_opts) click to toggle source

Deletes a file.

# File lib/bfs/bucket/in_mem.rb, line 92
def rm(path, **_opts)
  @files.delete(norm_path(path))
end

Private Instance Methods

file_info(path) click to toggle source
# File lib/bfs/bucket/in_mem.rb, line 98
def file_info(path)
  entry = @files[path]
  BFS::FileInfo.new path: path, size: entry.io.size, mtime: entry.mtime, content_type: entry.content_type, metadata: entry.metadata
end