Class: BFS::Bucket::InMem
Overview
InMem buckets are useful for tests
Defined Under Namespace
Instance Attribute Summary
Attributes inherited from Abstract
Instance Method Summary collapse
-
#clear ⇒ Object
Reset bucket and clear all files.
-
#create(path, encoding: self.encoding, content_type: nil, metadata: nil, **_opts, &block) ⇒ Object
Creates a new file and opens it for writing.
-
#info(path, **_opts) ⇒ Object
Info returns the file info.
-
#initialize(**opts) ⇒ InMem
constructor
A new instance of InMem.
-
#ls(pattern = '**/*', **_opts) ⇒ Object
Lists the contents of a bucket using a glob pattern.
-
#open(path, **_opts, &block) ⇒ Object
Opens an existing file for reading.
-
#rm(path, **_opts) ⇒ Object
Deletes a file.
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
#clear ⇒ Object
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.
61 62 63 64 65 |
# File 'lib/bfs/bucket/in_mem.rb', line 61 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, ()) end.perform(&block) end |
#info(path, **_opts) ⇒ Object
Info returns the file info
46 47 48 49 50 51 52 |
# File 'lib/bfs/bucket/in_mem.rb', line 46 def info(path, **_opts) path = norm_path(path) raise BFS::FileNotFound, path unless @files.key?(path) entry = @files[path] BFS::FileInfo.new(path: path, size: entry.io.size, mtime: entry.mtime, content_type: entry.content_type, metadata: entry.) 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 |key| y << key if File.fnmatch?(pattern, key, File::FNM_PATHNAME) end end end |
#open(path, **_opts, &block) ⇒ Object
Opens an existing file for reading
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/bfs/bucket/in_mem.rb', line 68 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.
84 85 86 |
# File 'lib/bfs/bucket/in_mem.rb', line 84 def rm(path, **_opts) @files.delete(norm_path(path)) end |