Class: BFS::Blob

Inherits:
Object
  • Object
show all
Defined in:
lib/bfs/blob.rb

Overview

Blobs are references to single blob objects within a bucket.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Blob

Returns a new instance of Blob.



19
20
21
22
23
24
25
26
27
# File 'lib/bfs/blob.rb', line 19

def initialize(url)
  url = url.is_a?(::URI) ? url.dup : URI.parse(url)
  @path = BFS.norm_path(url.path)

  url.path = '/'
  @bucket = BFS.resolve(url)

  BFS.defer(self, :close)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/bfs/blob.rb', line 4

def path
  @path
end

Class Method Details

.open(url) ⇒ Object

Behaves like new, but accepts an optional block. If a block is given, blobs are automatically closed after the block is yielded.



8
9
10
11
12
13
14
15
16
17
# File 'lib/bfs/blob.rb', line 8

def self.open(url)
  blob = new(url)
  return blob unless block_given?

  begin
    yield blob
  ensure
    blob.close
  end
end

Instance Method Details

#closeObject

Closes the underlying bucket connection.



71
72
73
# File 'lib/bfs/blob.rb', line 71

def close
  @bucket.close
end

#create(**opts, &block) ⇒ Object

Creates the blob and opens it for writing. If a block is passed the writer is automatically committed in the end. If no block is passed, you must manually call #commit to persist the result.



38
39
40
# File 'lib/bfs/blob.rb', line 38

def create(**opts, &block)
  @bucket.create(path, **opts, &block)
end

#info(**opts) ⇒ Object

Info returns the blob info.



30
31
32
# File 'lib/bfs/blob.rb', line 30

def info(**opts)
  @bucket.info(path, **opts)
end

#mv(dst, **opts) ⇒ Object

Moves blob to dst.



64
65
66
67
68
# File 'lib/bfs/blob.rb', line 64

def mv(dst, **opts)
  dst = BFS.norm_path(dst)
  @bucket.mv(path, dst, **opts)
  @path = dst
end

#open(**opts, &block) ⇒ Object

Opens the blob for reading. May raise BFS::FileNotFound.



44
45
46
# File 'lib/bfs/blob.rb', line 44

def open(**opts, &block)
  @bucket.open(path, **opts, &block)
end

#read(**opts) ⇒ Object

Shortcut method to read the contents of the blob.



54
55
56
# File 'lib/bfs/blob.rb', line 54

def read(**opts)
  self.open(**opts, &:read)
end

#rm(**opts) ⇒ Object

Deletes the blob.



49
50
51
# File 'lib/bfs/blob.rb', line 49

def rm(**opts)
  @bucket.rm(path, **opts)
end

#write(data, **opts) ⇒ Object

Shortcut method to write data to blob.



59
60
61
# File 'lib/bfs/blob.rb', line 59

def write(data, **opts)
  create(**opts) {|f| f.write data }
end