Class: Mxrb::Scaffold::Transaction
- Inherits:
-
Object
- Object
- Mxrb::Scaffold::Transaction
- Defined in:
- lib/mxrb/scaffold/transaction.rb
Overview
Applies a set of file creations and replacements with rollback on failure.
Defined Under Namespace
Classes: Change
Instance Attribute Summary collapse
-
#created ⇒ Object
readonly
Returns the value of attribute created.
-
#updated ⇒ Object
readonly
Returns the value of attribute updated.
Instance Method Summary collapse
- #commit ⇒ Object
- #content(path) ⇒ Object
- #create(path, content) ⇒ Object
-
#initialize ⇒ Transaction
constructor
A new instance of Transaction.
- #write(path, new_content) ⇒ Object
Constructor Details
#initialize ⇒ Transaction
Returns a new instance of Transaction.
11 12 13 14 15 |
# File 'lib/mxrb/scaffold/transaction.rb', line 11 def initialize @changes = {} @created = [] @updated = [] end |
Instance Attribute Details
#created ⇒ Object (readonly)
Returns the value of attribute created.
9 10 11 |
# File 'lib/mxrb/scaffold/transaction.rb', line 9 def created @created end |
#updated ⇒ Object (readonly)
Returns the value of attribute updated.
9 10 11 |
# File 'lib/mxrb/scaffold/transaction.rb', line 9 def updated @updated end |
Instance Method Details
#commit ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mxrb/scaffold/transaction.rb', line 41 def commit applied = [] @changes.each_value do |change| apply(change) applied << change end rescue StandardError applied.reverse_each { rollback(_1) } raise ensure cleanup_staging end |
#content(path) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/mxrb/scaffold/transaction.rb', line 17 def content(path) return @changes.fetch(path).content if @changes.key?(path) return File.binread(path) if File.file?(path) nil end |
#create(path, content) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/mxrb/scaffold/transaction.rb', line 24 def create(path, content) abort "#{path}: file already exists" if File.exist?(path) || @changes.key?(path) @created << path @changes[path] = Change.new(path, content, nil, 0o644) end |
#write(path, new_content) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/mxrb/scaffold/transaction.rb', line 31 def write(path, new_content) original = content(path) return create(path, new_content) unless original return if original == new_content @updated << path unless @updated.include?(path) mode = File.stat(path).mode @changes[path] = Change.new(path, new_content, File.binread(path), mode) end |