Class: BFS::Bucket::InMem
Overview
InMem buckets are useful for tests
Defined Under Namespace
Classes: Entry
Instance Method Summary collapse
-
#clear ⇒ Object
Reset bucket and clear all files.
-
#create(path, opts = {}, &block) ⇒ Object
Creates a new file and opens it for writing.
-
#info(path, _opts = {}) ⇒ Object
Info returns the file info.
-
#initialize ⇒ 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
Constructor Details
#initialize ⇒ InMem
Returns a new instance of InMem.
10 11 12 |
# File 'lib/bfs/bucket/in_mem.rb', line 10 def initialize @files = {} end |
Instance Method Details
#clear ⇒ Object
Reset bucket and clear all files.
15 16 17 |
# File 'lib/bfs/bucket/in_mem.rb', line 15 def clear @files.clear end |
#create(path, opts = {}, &block) ⇒ Object
Creates a new file and opens it for writing
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/bfs/bucket/in_mem.rb', line 38 def create(path, opts={}, &block) io = StringIO.new @files[norm_path(path)] = Entry.new(io, Time.now, opts[:content_type], opts[:metadata] || {}) return io unless block begin yield(io) ensure io.close end end |
#info(path, _opts = {}) ⇒ Object
Info returns the file info
29 30 31 32 33 34 35 |
# File 'lib/bfs/bucket/in_mem.rb', line 29 def info(path, _opts={}) path = norm_path(path) raise BFS::FileNotFound, path unless @files.key?(path) entry = @files[path] BFS::FileInfo.new(path, entry.io.size, entry.mtime, entry.content_type, entry.) end |
#ls(pattern = '**/*', _opts = {}) ⇒ Object
Lists the contents of a bucket using a glob pattern
20 21 22 23 24 25 26 |
# File 'lib/bfs/bucket/in_mem.rb', line 20 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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/bfs/bucket/in_mem.rb', line 51 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.
67 68 69 |
# File 'lib/bfs/bucket/in_mem.rb', line 67 def rm(path, _opts={}) @files.delete(norm_path(path)) end |