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

Instance Method Summary collapse

Methods inherited from Abstract

#cp, #mv, #read, #write

Constructor Details

#initializeInMem

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

#create(path, _opts = {}, &block) ⇒ Object

Creates a new file and opens it for writing



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bfs/bucket/in_mem.rb', line 33

def create(path, _opts={}, &block)
  io = StringIO.new
  @files[norm_path(path)] = Entry.new(io, Time.now)
  return io unless block

  begin
    yield(io)
  ensure
    io.close
  end
end

#info(path, _opts = {}) ⇒ Object

Info returns the file info

Raises:



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

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)
end

#ls(pattern = '**/*', _opts = {}) ⇒ Object

Lists the contents of a bucket using a glob pattern



15
16
17
18
19
20
21
# File 'lib/bfs/bucket/in_mem.rb', line 15

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

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bfs/bucket/in_mem.rb', line 46

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.



62
63
64
# File 'lib/bfs/bucket/in_mem.rb', line 62

def rm(path, _opts={})
  @files.delete(norm_path(path))
end