Class: BFS::Bucket::Abstract
- Inherits:
-
Object
- Object
- BFS::Bucket::Abstract
- Defined in:
- lib/bfs/bucket/abstract.rb
Instance Attribute Summary collapse
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
-
#perm ⇒ Object
readonly
Returns the value of attribute perm.
Class Method Summary collapse
-
.open(*args, **opts) ⇒ Object
Behaves like new, but accepts an optional block.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the underlying connection.
-
#cp(src, dst, **opts) ⇒ Object
Copies src to dst.
-
#create(_path, **_opts) ⇒ Object
Creates a new file and opens it for writing.
-
#glob(_pattern = '**', **_opts) ⇒ Object
Iterates over the contents of a bucket using a glob pattern.
-
#info(_path, **_opts) ⇒ Object
Info returns the info for a single file.
-
#initialize(encoding: Encoding.default_external, perm: nil, **_opts) ⇒ Abstract
constructor
Initializes a new bucket.
-
#ls(_pattern = '**', **_opts) ⇒ Object
Lists the contents of a bucket using a glob pattern.
-
#mv(src, dst, **_opts) ⇒ Object
Moves src to dst.
-
#open(_path, **_opts) ⇒ Object
Opens an existing file for reading May raise BFS::FileNotFound.
-
#read(path, **opts) ⇒ Object
Shortcut method to read the contents of a file into memory.
-
#rm(_path, **_opts) ⇒ Object
Deletes a file.
-
#write(path, data, **opts) ⇒ Object
Shortcut method to write data to path.
Constructor Details
#initialize(encoding: Encoding.default_external, perm: nil, **_opts) ⇒ Abstract
Initializes a new bucket
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/bfs/bucket/abstract.rb', line 25 def initialize(encoding: Encoding.default_external, perm: nil, **_opts) @encoding = encoding case perm when Integer @perm = perm when String @perm = perm.to_i(8) end BFS.defer(self, :close) end |
Instance Attribute Details
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
6 7 8 |
# File 'lib/bfs/bucket/abstract.rb', line 6 def encoding @encoding end |
#perm ⇒ Object (readonly)
Returns the value of attribute perm.
6 7 8 |
# File 'lib/bfs/bucket/abstract.rb', line 6 def perm @perm end |
Class Method Details
.open(*args, **opts) ⇒ Object
Behaves like new, but accepts an optional block. If a block is given, buckets are automatically closed after the block is yielded.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/bfs/bucket/abstract.rb', line 10 def self.open(*args, **opts) bucket = new(*args, **opts) return bucket unless block_given? begin yield bucket ensure bucket.close end end |
Instance Method Details
#close ⇒ Object
Closes the underlying connection
108 |
# File 'lib/bfs/bucket/abstract.rb', line 108 def close; end |
#cp(src, dst, **opts) ⇒ Object
Copies src to dst
90 91 92 93 94 95 96 |
# File 'lib/bfs/bucket/abstract.rb', line 90 def cp(src, dst, **opts) self.open(src, **opts) do |r| create(dst, **opts) do |w| IO.copy_stream(r, w) end end end |
#create(_path, **_opts) ⇒ Object
Creates a new file and opens it for writing
54 55 56 |
# File 'lib/bfs/bucket/abstract.rb', line 54 def create(_path, **_opts) raise 'not implemented' end |
#glob(_pattern = '**', **_opts) ⇒ Object
Iterates over the contents of a bucket using a glob pattern
44 45 46 |
# File 'lib/bfs/bucket/abstract.rb', line 44 def glob(_pattern = '**', **_opts) raise 'not implemented' end |
#info(_path, **_opts) ⇒ Object
Info returns the info for a single file
49 50 51 |
# File 'lib/bfs/bucket/abstract.rb', line 49 def info(_path, **_opts) raise 'not implemented' end |
#ls(_pattern = '**', **_opts) ⇒ Object
Lists the contents of a bucket using a glob pattern
39 40 41 |
# File 'lib/bfs/bucket/abstract.rb', line 39 def ls(_pattern = '**', **_opts) raise 'not implemented' end |
#mv(src, dst, **_opts) ⇒ Object
Moves src to dst
102 103 104 105 |
# File 'lib/bfs/bucket/abstract.rb', line 102 def mv(src, dst, **_opts) cp(src, dst) rm(src) end |
#open(_path, **_opts) ⇒ Object
Opens an existing file for reading May raise BFS::FileNotFound
60 61 62 |
# File 'lib/bfs/bucket/abstract.rb', line 60 def open(_path, **_opts) raise 'not implemented' end |
#read(path, **opts) ⇒ Object
Shortcut method to read the contents of a file into memory
73 74 75 |
# File 'lib/bfs/bucket/abstract.rb', line 73 def read(path, **opts) self.open(path, **opts, &:read) end |
#rm(_path, **_opts) ⇒ Object
Deletes a file.
65 66 67 |
# File 'lib/bfs/bucket/abstract.rb', line 65 def rm(_path, **_opts) raise 'not implemented' end |
#write(path, data, **opts) ⇒ Object
Shortcut method to write data to path
82 83 84 |
# File 'lib/bfs/bucket/abstract.rb', line 82 def write(path, data, **opts) create(path, **opts) {|f| f.write data } end |