bfs 0.7.4 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/bfs/blob.rb +3 -0
- data/lib/bfs/bucket/abstract.rb +1 -1
- data/lib/bfs/bucket/fs.rb +3 -8
- data/lib/bfs/bucket/in_mem.rb +19 -12
- data/lib/bfs/helpers.rb +36 -12
- data/spec/bfs/blob_spec.rb +12 -7
- data/spec/bfs/bucket/abstract_spec.rb +1 -1
- data/spec/bfs/bucket/fs_spec.rb +7 -5
- data/spec/bfs/helpers_spec.rb +20 -13
- data/spec/bfs_spec.rb +4 -4
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 32e754ffdd81a72cc648eb786fc2ca34839c0debe67b80e2ee6256766c8194e4
|
4
|
+
data.tar.gz: 443d181595af99d3b00be21809965d68e2373439435bfefbca35df168bdc1962
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13cd555372996a1a2050229f57eae18c32215dc0303e83ccbc866ed4e6b3e5d1c8079a827520ed39871c39152cf96a01532bc254ad2f1a9e996cf6a32b5c24c9
|
7
|
+
data.tar.gz: 8ac28da9c0c664810e5665299f1cd97609dba0c789fc42244e9494a95a8587ef799ecee64c38eec40a301c2e1d131bcb98aad89d0ee4d43ab4f66a2db14203c7
|
data/lib/bfs/blob.rb
CHANGED
@@ -32,6 +32,9 @@ module BFS
|
|
32
32
|
end
|
33
33
|
|
34
34
|
# Creates the blob and opens it for writing.
|
35
|
+
# If a block is passed the writer is automatically committed in the end.
|
36
|
+
# If no block is passed, you must manually call #commit to persist the
|
37
|
+
# result.
|
35
38
|
def create(**opts, &block)
|
36
39
|
@bucket.create(path, **opts, &block)
|
37
40
|
end
|
data/lib/bfs/bucket/abstract.rb
CHANGED
@@ -107,7 +107,7 @@ module BFS
|
|
107
107
|
def norm_meta(meta)
|
108
108
|
norm = {}
|
109
109
|
meta.each do |key, value|
|
110
|
-
nkey = key.to_s.downcase.split(
|
110
|
+
nkey = key.to_s.downcase.split('-').map(&:capitalize).join('-')
|
111
111
|
norm[nkey] = value
|
112
112
|
end if meta.is_a?(Hash)
|
113
113
|
norm
|
data/lib/bfs/bucket/fs.rb
CHANGED
@@ -42,14 +42,9 @@ module BFS
|
|
42
42
|
full = @root.join(norm_path(path))
|
43
43
|
FileUtils.mkdir_p(full.dirname.to_s)
|
44
44
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
begin
|
49
|
-
yield temp
|
50
|
-
ensure
|
51
|
-
temp.close
|
52
|
-
end
|
45
|
+
BFS::Writer.new(full, encoding: encoding, perm: perm) do |temp|
|
46
|
+
FileUtils.mv temp, full.to_s
|
47
|
+
end.perform(&block)
|
53
48
|
end
|
54
49
|
|
55
50
|
# Opens an existing file for reading
|
data/lib/bfs/bucket/in_mem.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'bfs'
|
2
2
|
require 'stringio'
|
3
|
+
require 'delegate'
|
3
4
|
|
4
5
|
module BFS
|
5
6
|
module Bucket
|
@@ -7,6 +8,21 @@ module BFS
|
|
7
8
|
class InMem < Abstract
|
8
9
|
Entry = Struct.new(:io, :mtime, :content_type, :metadata)
|
9
10
|
|
11
|
+
class Writer < DelegateClass(::StringIO)
|
12
|
+
include BFS::Writer::Mixin
|
13
|
+
|
14
|
+
def initialize(encoding:, &on_commit)
|
15
|
+
@on_commit = on_commit
|
16
|
+
|
17
|
+
sio = StringIO.new
|
18
|
+
sio.set_encoding(encoding)
|
19
|
+
super sio
|
20
|
+
end
|
21
|
+
|
22
|
+
alias close! close
|
23
|
+
alias commit_ref __getobj__
|
24
|
+
end
|
25
|
+
|
10
26
|
def initialize(**opts)
|
11
27
|
super(**opts.dup)
|
12
28
|
@files = {}
|
@@ -43,18 +59,9 @@ module BFS
|
|
43
59
|
# @option opts [String] :content_type Custom content type.
|
44
60
|
# @option opts [Hash] :metadata Metadata key-value pairs.
|
45
61
|
def create(path, encoding: self.encoding, content_type: nil, metadata: nil, **_opts, &block)
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
entry = Entry.new(io, Time.now, content_type, norm_meta(metadata))
|
50
|
-
@files[norm_path(path)] = entry
|
51
|
-
return io unless block
|
52
|
-
|
53
|
-
begin
|
54
|
-
yield(io)
|
55
|
-
ensure
|
56
|
-
io.close
|
57
|
-
end
|
62
|
+
Writer.new(encoding: encoding) do |wio|
|
63
|
+
@files[norm_path(path)] = Entry.new(wio, Time.now, content_type, norm_meta(metadata))
|
64
|
+
end.perform(&block)
|
58
65
|
end
|
59
66
|
|
60
67
|
# Opens an existing file for reading
|
data/lib/bfs/helpers.rb
CHANGED
@@ -2,21 +2,45 @@ require 'tempfile'
|
|
2
2
|
require 'delegate'
|
3
3
|
|
4
4
|
module BFS
|
5
|
-
class
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
class Writer < DelegateClass(::Tempfile)
|
6
|
+
module Mixin
|
7
|
+
def perform
|
8
|
+
return self unless block_given?
|
9
|
+
|
10
|
+
begin
|
11
|
+
yield self
|
12
|
+
commit
|
13
|
+
ensure
|
14
|
+
discard
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def commit
|
19
|
+
close
|
20
|
+
return false if @on_commit.nil?
|
21
|
+
|
22
|
+
@on_commit.call(commit_ref)
|
23
|
+
true
|
24
|
+
ensure
|
25
|
+
discard
|
26
|
+
end
|
27
|
+
|
28
|
+
def discard
|
29
|
+
@on_commit = nil
|
30
|
+
close!
|
31
|
+
end
|
11
32
|
end
|
12
33
|
|
13
|
-
|
14
|
-
|
34
|
+
include Mixin
|
35
|
+
|
36
|
+
def initialize(name, tempdir: nil, perm: nil, **opts, &on_commit)
|
37
|
+
@on_commit = on_commit
|
15
38
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
@tempfile.unlink
|
39
|
+
tempfile = ::Tempfile.new(File.basename(name.to_s), tempdir, **opts)
|
40
|
+
tempfile.chmod(perm) if perm
|
41
|
+
super tempfile
|
20
42
|
end
|
43
|
+
|
44
|
+
alias commit_ref path
|
21
45
|
end
|
22
46
|
end
|
data/spec/bfs/blob_spec.rb
CHANGED
@@ -2,12 +2,15 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
RSpec.describe BFS::Blob, core: true do
|
4
4
|
describe 'default' do
|
5
|
+
subject { described_class.new('memtest://bucket/path/to/file.txt') }
|
6
|
+
|
5
7
|
let(:bucket) { BFS::Bucket::InMem.new }
|
8
|
+
|
6
9
|
before { allow(BFS).to receive(:resolve).and_return(bucket) }
|
7
|
-
|
10
|
+
|
8
11
|
after { subject.close }
|
9
12
|
|
10
|
-
it '
|
13
|
+
it 'moves' do
|
11
14
|
expect(subject.path).to eq('path/to/file.txt')
|
12
15
|
expect { subject.mv('/to/other/path.txt') }.to raise_error(BFS::FileNotFound)
|
13
16
|
|
@@ -16,7 +19,7 @@ RSpec.describe BFS::Blob, core: true do
|
|
16
19
|
expect(subject.path).to eq('to/other/path.txt')
|
17
20
|
end
|
18
21
|
|
19
|
-
it '
|
22
|
+
it 'write/reads' do
|
20
23
|
expect { subject.read }.to raise_error(BFS::FileNotFound)
|
21
24
|
subject.write('TESTDATA', content_type: 'text/plain', metadata: { 'x-key' => 'val' })
|
22
25
|
|
@@ -36,12 +39,14 @@ RSpec.describe BFS::Blob, core: true do
|
|
36
39
|
end
|
37
40
|
|
38
41
|
describe 'file system' do
|
42
|
+
subject { described_class.new("file:///#{path}") }
|
43
|
+
|
39
44
|
let(:tmpdir) { Dir.mktmpdir }
|
40
45
|
let(:path) { "#{tmpdir}/path/to/file.txt".sub('/', '') }
|
41
|
-
after { FileUtils.rm_rf tmpdir }
|
42
|
-
subject { described_class.new("file:///#{path}") }
|
43
46
|
|
44
|
-
|
47
|
+
after { FileUtils.rm_rf tmpdir }
|
48
|
+
|
49
|
+
it 'moves' do
|
45
50
|
expect(subject.path).to eq(path)
|
46
51
|
expect { subject.mv("#{tmpdir}/to/other/path.txt") }.to raise_error(BFS::FileNotFound)
|
47
52
|
|
@@ -54,7 +59,7 @@ RSpec.describe BFS::Blob, core: true do
|
|
54
59
|
]
|
55
60
|
end
|
56
61
|
|
57
|
-
it '
|
62
|
+
it 'write/reads' do
|
58
63
|
expect { subject.read }.to raise_error(BFS::FileNotFound)
|
59
64
|
|
60
65
|
subject.write('TESTDATA', content_type: 'text/plain', metadata: { 'x-key' => 'val' })
|
data/spec/bfs/bucket/fs_spec.rb
CHANGED
@@ -1,15 +1,17 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe BFS::Bucket::FS, core: true do
|
4
|
-
let(:tmpdir) { Dir.mktmpdir }
|
5
|
-
after { FileUtils.rm_rf tmpdir }
|
6
4
|
subject { described_class.new(tmpdir) }
|
7
5
|
|
6
|
+
let(:tmpdir) { Dir.mktmpdir }
|
7
|
+
|
8
|
+
after { FileUtils.rm_rf tmpdir }
|
9
|
+
|
8
10
|
it_behaves_like 'a bucket',
|
9
11
|
content_type: false,
|
10
12
|
metadata: false
|
11
13
|
|
12
|
-
it '
|
14
|
+
it 'resolves from URL' do
|
13
15
|
File.open(File.join(tmpdir, 'test.txt'), 'wb') {|f| f.write 'TESTDATA' }
|
14
16
|
|
15
17
|
bucket = BFS.resolve("file://#{tmpdir}")
|
@@ -18,14 +20,14 @@ RSpec.describe BFS::Bucket::FS, core: true do
|
|
18
20
|
bucket.close
|
19
21
|
end
|
20
22
|
|
21
|
-
it '
|
23
|
+
it 'supports custom perms on #initialize' do
|
22
24
|
blob = BFS::Blob.new("file://#{tmpdir}/test.txt?perm=0666")
|
23
25
|
blob.create {|w| w.write 'foo' }
|
24
26
|
expect(blob.info.mode).to eq(0o666)
|
25
27
|
blob.close
|
26
28
|
end
|
27
29
|
|
28
|
-
it '
|
30
|
+
it 'supports custom perms on #create' do
|
29
31
|
blob = BFS::Blob.new("file://#{tmpdir}/test.txt")
|
30
32
|
blob.create(perm: 0o666) {|w| w.write 'foo' }
|
31
33
|
expect(blob.info.mode).to eq(0o666)
|
data/spec/bfs/helpers_spec.rb
CHANGED
@@ -1,22 +1,29 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
RSpec.describe BFS::
|
4
|
-
|
5
|
-
subject { described_class.new 'test', &closer }
|
3
|
+
RSpec.describe BFS::Writer, core: true do
|
4
|
+
subject { described_class.new 'test', &on_commit }
|
6
5
|
|
7
|
-
|
8
|
-
missing = ::File.public_instance_methods - subject.public_methods
|
9
|
-
expect(missing).to be_empty
|
10
|
-
end
|
6
|
+
let(:on_commit) { proc { true } }
|
11
7
|
|
12
|
-
it '
|
13
|
-
subject = described_class.new 'test', perm: 0o640, &
|
8
|
+
it 'supports custom params' do
|
9
|
+
subject = described_class.new 'test', perm: 0o640, &on_commit
|
14
10
|
expect(subject.stat.mode).to eq(0o100640)
|
15
|
-
expect(subject.
|
11
|
+
expect(subject.commit).to be(true)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'executes a on_commit block' do
|
15
|
+
expect(on_commit).to receive(:call).with(subject.path).once
|
16
|
+
expect(subject.commit).to be(true)
|
17
|
+
expect(subject.commit).to be(false)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'may skip on_commit block' do
|
21
|
+
expect(on_commit).not_to receive(:call)
|
22
|
+
expect(subject.discard).to be(true)
|
16
23
|
end
|
17
24
|
|
18
|
-
it '
|
19
|
-
expect(
|
20
|
-
expect(subject.close).to
|
25
|
+
it 'does not auto-commit on close' do
|
26
|
+
expect(on_commit).not_to receive(:call)
|
27
|
+
expect(subject.close).to be_nil
|
21
28
|
end
|
22
29
|
end
|
data/spec/bfs_spec.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe BFS, core: true do
|
4
|
-
it '
|
5
|
-
bucket =
|
4
|
+
it 'resolves' do
|
5
|
+
bucket = described_class.resolve("file://#{Dir.tmpdir}")
|
6
6
|
expect(bucket).to be_instance_of(BFS::Bucket::FS)
|
7
7
|
bucket.close
|
8
8
|
end
|
9
9
|
|
10
|
-
it '
|
11
|
-
|
10
|
+
it 'resolves with block' do
|
11
|
+
described_class.resolve("file://#{Dir.tmpdir}") do |bucket|
|
12
12
|
expect(bucket).to be_instance_of(BFS::Bucket::FS)
|
13
13
|
expect(bucket).to receive(:close)
|
14
14
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bfs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dimitrij Denissenko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-05-04 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Minimalist abstraction for bucket storage
|
14
14
|
email: dimitrij@blacksquaremedia.com
|
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
51
|
- !ruby/object:Gem::Version
|
52
52
|
version: '0'
|
53
53
|
requirements: []
|
54
|
-
rubygems_version: 3.1.
|
54
|
+
rubygems_version: 3.1.4
|
55
55
|
signing_key:
|
56
56
|
specification_version: 4
|
57
57
|
summary: Multi-platform cloud bucket adapter
|