file_transactions 0.1.1 → 0.1.2

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: 4c00351141cff6a4cbbe25c582306665eb384d2f8491345c25e13683cb8d464c
4
- data.tar.gz: 175b7c985bff046e4410f344b2fcd8c7c809af194b47c59c8602a203b08b9d26
3
+ metadata.gz: 877914f1551238a6f03b03e24f4670b247a3df7339303d2b174db0ba2a60b2ff
4
+ data.tar.gz: 06b545de3377911e4502f35788fe10e2034daf51a319c9d80830aa11c28588d7
5
5
  SHA512:
6
- metadata.gz: c5b8cd9c28f375321294ae2a90a7afade3343d3366e4a86af2be3d78cc9719962012b2a1a6c39a2d19d6f1f565f7d8df91230ba4f477f38d2e95a5b57c4d9816
7
- data.tar.gz: '097de30c6a7554dfe73264399e138f99812e317232090a47c7d979234dfbb9b1f545b877e5024be7c90111e997a4c62cb2dae45993bbcf62bd262a62e27a98bf'
6
+ metadata.gz: 96b18e2d349cbcde39e6c2b870d88a156ff7de0a9a0f7dc458418ef68a0610487090a8611d9068ef2e85abfc89a5f3dd1de2fd3311ccf2eba461a593b41f54f1
7
+ data.tar.gz: 41ab3cf26cfbc1f908ed1064f3503fd9e6a0d83b87b76d9a6a26c475d8a3991529c0297fe1940f4972b19e0057af2bf8f14bb30af8f1e5e6a1a86f59c2eeba69
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -11,6 +11,37 @@ require 'file_transactions/move_file_command'
11
11
  require 'file_transactions/transaction'
12
12
 
13
13
  module FileTransactions
14
+ # This method runs the block inside a transaction
15
+ #
16
+ # ==== Examples
17
+ #
18
+ # FileTransactions.transaction do
19
+ # CreateFileCommand.execute('new_file') { 'hello' }
20
+ # ChangeFileCommand.execute('new_file') { 'world' }
21
+ # end
22
+ #
23
+ # FileTransactions.transaction do
24
+ # CreateFileCommand.execute('new_file') { 'hello' }
25
+ # DeleteFileCommand.execute('some_file')
26
+ #
27
+ # # An exception will make the transaction be rolled back. E.g
28
+ # # 'new_file' will be removed again and 'some_file' will be restored
29
+ # raise "foobar"
30
+ # end
31
+ #
32
+ # # Create an alias for FileTransactions
33
+ # FT = FileTransactions
34
+ #
35
+ # FT.transaction do
36
+ # CreateFileCommand.execute('new_file') { 'hello' }
37
+ #
38
+ # FT.transaction do
39
+ # ChangeFileCommand.execute('new_file') { 'world' }
40
+ #
41
+ # # This rolls back the current transaction but not the outer transaction
42
+ # raise FT::Rollback
43
+ # end
44
+ # end
14
45
  def self.transaction(&block)
15
46
  Transaction.run(&block)
16
47
  end
@@ -4,9 +4,35 @@ require 'fileutils'
4
4
  require 'tmpdir'
5
5
 
6
6
  module FileTransactions
7
+ # This command supports making changes to files.
8
+ # The block passed to +::new+ must either return a +String+, in which case
9
+ # the file content will be replace with that string.
10
+ # Or the block must itself modify the file with desired changes (and return anything but a String).
11
+ #
12
+ # When this command has been executed, the file can be restored to the
13
+ # previous state again by calling #undo.
14
+ #
15
+ # ==== Examples
16
+ #
17
+ # # Pass in the filename name to ::new
18
+ # cmd1 = ChangeFileCommand.new('some_existing_file') do
19
+ # <<~EOF
20
+ # Some content to that should
21
+ # replace the current file content.
22
+ # EOF
23
+ # end
24
+ #
25
+ # # Files can also be modified manually.
26
+ # # Note: the block gets name as argument.
27
+ # cmd2 = ChangeFileCommand.new('another_existing_file') do |name|
28
+ # File.open(name, 'a') do |f|
29
+ # f.write("Add some more stuff at the end\n")
30
+ # end
31
+ # end
7
32
  class ChangeFileCommand < BaseCommand
8
33
  attr_reader :name, :block
9
34
 
35
+ # @param name [String] The name of the file to be changed. May be just a name or an absolut or relative path
10
36
  def initialize(name, &block)
11
37
  @name = name
12
38
  @block = block
@@ -3,24 +3,23 @@
3
3
  require 'fileutils'
4
4
 
5
5
  module FileTransactions
6
+ # This command creates a new directory. It will also create parent
7
+ # directories if the given path contains directories that doesn't exist.
8
+ #
9
+ # When this command has been executed, the created directories can be removed
10
+ # again by calling #undo.
11
+ #
12
+ # ==== Examples
13
+ #
14
+ # # Pass in the new directory name to ::new
15
+ # cmd1 = CreateDirectoryCommand.new('directory_name')
16
+ #
17
+ # # The new directory name may be a path of multiple non exsting directories (like `mkdir -p`)
18
+ # cmd2 = CreateDirectoryCommand.new('non_existing_dir1/non_existing_dir2/new_dir')
6
19
  class CreateDirectoryCommand < BaseCommand
7
20
  attr_reader :name
8
21
 
9
- # Create new command for creating directories. N
10
- #
11
- # @param name [String]
12
- #
13
- # ==== Attributes
14
- #
15
- # * +name+ - The name of the directory to be created
16
- #
17
- # ==== Examples
18
- #
19
- # # Pass in the new directory name to ::new
20
- # cmd1 = CreateDirectoryCommand.new('directory_name')
21
- #
22
- # # The new directory may be a path of multiple non exsting directories (like `mkdir -p`)
23
- # cmd2 = CreateDirectoryCommand.new('non_existing_dir1/non_existing_dir2/new_dir')
22
+ # @param name [String] The name of the new directory. May be just a name or an absolut or relative path
24
23
  def initialize(name)
25
24
  @name = name
26
25
  end
@@ -1,9 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FileTransactions
4
+ # This command creates a new file. It will also create parent
5
+ # directories if the given path contains directories that doesn't exist.
6
+ # The block passed to +::new+ must either return a +String+ with the content
7
+ # that will be written to the new file. Or the block must itself create the file
8
+ # with the filname +name+ (and return anything but a String).
9
+ #
10
+ # When this command has been executed, the created file (and any directories)
11
+ # can be removed again by calling #undo.
12
+ #
13
+ # ==== Examples
14
+ #
15
+ # # Pass in the new directory name to ::new
16
+ # cmd1 = CreateFileCommand.new('new_name') do
17
+ # <<~EOF
18
+ # Some content to be
19
+ # written to the file.
20
+ # EOF
21
+ # end
22
+ #
23
+ # # Files can also be created manually.
24
+ # # Note: the block gets name as argument.
25
+ # cmd2 = CreateFileCommand.new('another_file') do |name|
26
+ # GenerateAwesomeReport.call(filename: name)
27
+ # true
28
+ # end
4
29
  class CreateFileCommand < BaseCommand
5
30
  attr_reader :name, :block
6
31
 
32
+ # @param name [String] The name of the new directory. May be just a name or an absolut or relative path
7
33
  def initialize(name, &block)
8
34
  @name = name
9
35
  @block = block
@@ -4,9 +4,19 @@ require 'fileutils'
4
4
  require 'tmpdir'
5
5
 
6
6
  module FileTransactions
7
+ # This command supports deleting a file.
8
+ #
9
+ # When this command has been executed, the file can be restored by calling
10
+ # #undo
11
+ #
12
+ # ==== Examples
13
+ #
14
+ # # Pass in the filename name to ::new
15
+ # cmd1 = DeleteFileCommand.new('some_existing_file')
7
16
  class DeleteFileCommand < BaseCommand
8
17
  attr_reader :name, :block
9
18
 
19
+ # @param name [String] The name of the file to be deleted. May be just a name or an absolut or relative path
10
20
  def initialize(name)
11
21
  @name = name
12
22
  end
@@ -1,9 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FileTransactions
4
+ # This command supports moveing (renaming) a file. It will also create parent
5
+ # directories if the given path contains directories that doesn't exist.
6
+ #
7
+ # When this command has been executed, the file can be moved back to the
8
+ # original location again by calling #undo. Any directories created during
9
+ # #execute will be deleted.
10
+ #
11
+ # ==== Examples
12
+ #
13
+ # cmd1 = MoveFileCommand.new('some_existing_file', 'some_new_dir/a_new_name')
4
14
  class MoveFileCommand < BaseCommand
5
15
  attr_reader :from, :to
6
16
 
17
+ # @param from [String] The name of the source file to be renamed. May be just a name or an absolut or relative path
18
+ # @param to [String] The target name of the file. May be just a name or an absolut or relative path
7
19
  def initialize(from:, to:)
8
20
  @from = from
9
21
  @to = to
@@ -1,6 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FileTransactions
4
+ # A class that keeps track of commands and nested transactions. If an
5
+ # exception is raised inside the transaction, then all commands and nested
6
+ # transactions get undone.
4
7
  class Transaction
5
8
  class << self
6
9
  def run(&block)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FileTransactions
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_transactions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sammy Henningsson
@@ -30,12 +30,16 @@ cert_chain:
30
30
  ZMhjYR7sRczGJx+GxGU2EaR0bjRsPVlC4ywtFxoOfRG3WaJcpWGEoAoMJX6Z0bRv
31
31
  M40=
32
32
  -----END CERTIFICATE-----
33
- date: 2020-06-18 00:00:00.000000000 Z
33
+ date: 2020-06-20 00:00:00.000000000 Z
34
34
  dependencies: []
35
35
  description: |
36
- A set of file operation that can be undone or wrapped in a
37
- transaction. If the transaction is rolled back then all file
38
- operations will be undone.
36
+ A library for creating commands can be done and
37
+ undone. Multiple commands can be grouped together
38
+ in a transaction making sure all commands succeed
39
+ or gets rolled back. This gem includes a few
40
+ commands for file operations but it makes it easy
41
+ to create custom commands and use them for more
42
+ than just file operations.
39
43
  email:
40
44
  - sammy.henningsson@gmail.com
41
45
  executables: []
metadata.gz.sig CHANGED
@@ -1 +1 @@
1
- p7#�<�!y�|̭�]�� J���W��YVy2U�:b����qTE�;����8����˱۩ߵ�-�.dǛ�1�R䚠ď��Xp��s׹�J�+�� ���Nr���Lha��ڕq!��k���E�Mj;�i"hI����0'i~��p��/�፜�?���M��q���`�|� ��1��!�Q����X���c���b�
1
+ 5���0���t�.a1R5!� y4���ǧ�J ��u���ҷ��y31�#��Pݒ��T���A��ŮsjO�\-�T���1��C9 ��C���w�$�Yu��������WɁb'A�b�Yn�$Q���ƈc%��ӶE SH*6q�}�]���~gR��� ��{Z ���O����pWS]�~�CL��5�kS��O �~W�K{��jJ&h#����+�����?Y����SX�4I�8: