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

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Blob

Returns a new instance of Blob.



6
7
8
9
10
11
12
# File 'lib/bfs/blob.rb', line 6

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

Instance Method Details

#closeObject

Closes the underlying bucket connection.



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

def close
  @bucket.close
end

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

Creates the blob and opens it for writing.



20
21
22
# File 'lib/bfs/blob.rb', line 20

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

#info(**opts) ⇒ Object

Info returns the blob info.



15
16
17
# File 'lib/bfs/blob.rb', line 15

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

#mv(dst, **opts) ⇒ Object

Moves blob to dst.



46
47
48
49
50
# File 'lib/bfs/blob.rb', line 46

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.



26
27
28
# File 'lib/bfs/blob.rb', line 26

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

#read(**opts) ⇒ Object

Shortcut method to read the contents of the blob.



36
37
38
# File 'lib/bfs/blob.rb', line 36

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

#rm(**opts) ⇒ Object

Deletes the blob.



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

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

#write(data, **opts) ⇒ Object

Shortcut method to write data to blob.



41
42
43
# File 'lib/bfs/blob.rb', line 41

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