bfs 0.7.6 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd67ecfdb038d1809be4193caf014dfd9e8bcba4e26a4cd04da3c64e61522ddc
4
- data.tar.gz: 17586a9b02836e8bdcbf4da6bc822d9c169cca9823b5817357dba1d4a5f1a072
3
+ metadata.gz: 3de22f9cab6ba21d594854a2fcec49cc215b7c10c6f6b5b9cb1528ba6ce96b59
4
+ data.tar.gz: cc3b427ff41229556b7b39a9d0ad5d61afdd0439fa984aafadd0aa9635ac4664
5
5
  SHA512:
6
- metadata.gz: e1ab4426da76e5a0de997d9b1f355c549197e4bd199629a37f6f787a9dcd068020c04ed190f117461490bb75d86a4070b475d481cc679005de731dacabb8fd64
7
- data.tar.gz: 355cd6811da84829fad5f59927535b681c3f357968681d175c8a400f4e0b445920120daba944ea679dc965b582d65781063e2be199ffc372cda4e7df6739f89c
6
+ metadata.gz: 569ac3d4e157eebf08f948eab831f214a900d29a96028a703d541d3fa7de7fdcde6a2cb11413ff802c459ff163cff15feb00c3477abcae7b18d77c302255d529
7
+ data.tar.gz: 2804530bcc9aaf60f1b475a6948b357242dc96e15788835e34d6cc1989ec7bda8e0bd66372427ce4aa9d5b923eda5468a9c33087b716ac3ca379f391a6192f43
@@ -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
@@ -42,7 +42,7 @@ module BFS
42
42
  full = @root.join(norm_path(path))
43
43
  FileUtils.mkdir_p(full.dirname.to_s)
44
44
 
45
- BFS::TempWriter.new(full, encoding: encoding, perm: perm) do |temp|
45
+ BFS::Writer.new(full, encoding: encoding, perm: perm) do |temp|
46
46
  FileUtils.mv temp, full.to_s
47
47
  end.perform(&block)
48
48
  end
@@ -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
@@ -8,34 +9,18 @@ module BFS
8
9
  Entry = Struct.new(:io, :mtime, :content_type, :metadata)
9
10
 
10
11
  class Writer < DelegateClass(::StringIO)
11
- def initialize(encoding:, &closer)
12
- @closer = closer
12
+ include BFS::Writer::Mixin
13
+
14
+ def initialize(encoding:, &on_commit)
15
+ @on_commit = on_commit
13
16
 
14
17
  sio = StringIO.new
15
18
  sio.set_encoding(encoding)
16
19
  super sio
17
20
  end
18
21
 
19
- def close
20
- super.tap do
21
- @closer&.call(self)
22
- end
23
- end
24
-
25
- def close!
26
- __getobj__.close
27
- end
28
-
29
- def perform
30
- return self unless block_given?
31
-
32
- begin
33
- yield self
34
- close
35
- ensure
36
- close!
37
- end
38
- end
22
+ alias close! close
23
+ alias commit_ref __getobj__
39
24
  end
40
25
 
41
26
  def initialize(**opts)
@@ -2,33 +2,45 @@ require 'tempfile'
2
2
  require 'delegate'
3
3
 
4
4
  module BFS
5
- class TempWriter < DelegateClass(::Tempfile)
6
- def initialize(name, tempdir: nil, perm: nil, **opts, &closer)
7
- @closer = closer
5
+ class Writer < DelegateClass(::Tempfile)
6
+ module Mixin
7
+ def perform
8
+ return self unless block_given?
8
9
 
9
- tempfile = ::Tempfile.new(File.basename(name.to_s), tempdir, **opts)
10
- tempfile.chmod(perm) if perm
11
- super tempfile
12
- end
13
-
14
- def perform
15
- return self unless block_given?
10
+ begin
11
+ yield self
12
+ commit
13
+ ensure
14
+ discard
15
+ end
16
+ end
16
17
 
17
- begin
18
- yield self
18
+ def commit
19
19
  close
20
+ return false if @on_commit.nil?
21
+
22
+ @on_commit.call(commit_ref)
23
+ true
20
24
  ensure
25
+ discard
26
+ end
27
+
28
+ def discard
29
+ @on_commit = nil
21
30
  close!
22
31
  end
23
32
  end
24
33
 
25
- def close
26
- return if closed?
34
+ include Mixin
27
35
 
28
- super.tap do
29
- @closer&.call(path)
30
- end
31
- unlink
36
+ def initialize(name, tempdir: nil, perm: nil, **opts, &on_commit)
37
+ @on_commit = on_commit
38
+
39
+ tempfile = ::Tempfile.new(File.basename(name.to_s), tempdir, **opts)
40
+ tempfile.chmod(perm) if perm
41
+ super tempfile
32
42
  end
43
+
44
+ alias commit_ref path
33
45
  end
34
46
  end
@@ -1,28 +1,28 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe BFS::TempWriter, core: true do
4
- let(:closer) { proc {} }
5
- subject { described_class.new 'test', &closer }
6
-
7
- it 'should behave like a File' do
8
- missing = ::File.public_instance_methods - subject.public_methods
9
- expect(missing).to be_empty
10
- end
3
+ RSpec.describe BFS::Writer, core: true do
4
+ let(:on_commit) { proc { true } }
5
+ subject { described_class.new 'test', &on_commit }
11
6
 
12
7
  it 'should support custom params' do
13
- subject = described_class.new 'test', perm: 0o640, &closer
8
+ subject = described_class.new 'test', perm: 0o640, &on_commit
14
9
  expect(subject.stat.mode).to eq(0o100640)
15
- expect(subject.close).to be_truthy
10
+ expect(subject.commit).to be(true)
16
11
  end
17
12
 
18
- it 'should execute a closer block' do
19
- expect(closer).to receive(:call).with(subject.path).once
20
- expect(subject.close).to be_truthy
21
- expect(subject.close).to be_nil
13
+ it 'should execute a on_commit block' do
14
+ expect(on_commit).to receive(:call).with(subject.path).once
15
+ expect(subject.commit).to be(true)
16
+ expect(subject.commit).to be(false)
22
17
  end
23
18
 
24
- it 'may skip closer block' do
25
- expect(closer).not_to receive(:call)
26
- expect(subject.close!).to be_truthy
19
+ it 'may skip on_commit block' do
20
+ expect(on_commit).not_to receive(:call)
21
+ expect(subject.discard).to be(true)
22
+ end
23
+
24
+ it 'should not auto-commit on close' do
25
+ expect(on_commit).not_to receive(:call)
26
+ expect(subject.close).to be_nil
27
27
  end
28
28
  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.7.6
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimitrij Denissenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-10 00:00:00.000000000 Z
11
+ date: 2020-12-01 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.2
54
+ rubygems_version: 3.1.4
55
55
  signing_key:
56
56
  specification_version: 4
57
57
  summary: Multi-platform cloud bucket adapter