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

#close, #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

#clearObject

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.

Parameters:

  • path (String)

    The creation path.

  • opts (Hash) (defaults to: {})

    Additional options.

Options Hash (opts):

  • :encoding (String)

    Custom encoding.

  • :content_type (String)

    Custom content type.

  • :metadata (Hash)

    Metadata key-value pairs.



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

def create(path, opts={}, &block)
  io = StringIO.new
  opts[:encoding] ? io.set_encoding(opts[:encoding]) : io.binmode

  @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

Raises:



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

Raises:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bfs/bucket/in_mem.rb', line 59

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.



75
76
77
# File 'lib/bfs/bucket/in_mem.rb', line 75

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