Class: BFS::Bucket::InMem

Inherits:
Abstract show all
Defined in:
lib/bfs/bucket/in_mem.rb

Overview

InMem buckets are useful for tests

Defined Under Namespace

Classes: Entry, Writer

Instance Attribute Summary

Attributes inherited from Abstract

#encoding, #perm

Instance Method Summary collapse

Methods inherited from Abstract

#close, #cp, #mv, open, #read, #write

Constructor Details

#initialize(**opts) ⇒ InMem

Returns a new instance of InMem.



26
27
28
29
# File 'lib/bfs/bucket/in_mem.rb', line 26

def initialize(**opts)
  super(**opts.dup)
  @files = {}
end

Instance Method Details

#clearObject

Reset bucket and clear all files.



32
33
34
# 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) ⇒ Object

Creates a new file and opens it for writing.

Parameters:

  • path (String)

    The creation path.

  • opts (Hash)

    Additional options.



69
70
71
72
73
# 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())
  end.perform(&block)
end

#glob(pattern = '**/*', **_opts) ⇒ Object

Iterates over the contents of a bucket using a glob pattern



46
47
48
49
50
51
52
# 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) ⇒ Object

Info returns the file info

Raises:



55
56
57
58
59
60
# 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) ⇒ Object

Lists the contents of a bucket using a glob pattern



37
38
39
40
41
42
43
# 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, &block) ⇒ Object

Opens an existing file for reading

Raises:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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) ⇒ Object

Deletes a file.



92
93
94
# File 'lib/bfs/bucket/in_mem.rb', line 92

def rm(path, **_opts)
  @files.delete(norm_path(path))
end