Class: RactorDNS::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/ractor_dns/transaction.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Transaction

Returns a new instance of Transaction.



4
5
6
7
8
# File 'lib/ractor_dns/transaction.rb', line 4

def initialize(&block)
  # exploiting a bug in ruby for fun and profit (https://bugs.ruby-lang.org/issues/19374)
  @block = Ractor.make_shareable(block.curry)
  @original_state = nil
end

Instance Method Details

#apply(obj) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/ractor_dns/transaction.rb', line 10

def apply(obj)
  @original_state = obj.dup
  @new_obj = obj.deep_transform_values { |value| value.dup }
  @block.call(@new_obj)
  obj.replace(@new_obj)
rescue => e
  puts e
  rollback(obj)
end

#rollback(obj) ⇒ Object



20
21
22
23
# File 'lib/ractor_dns/transaction.rb', line 20

def rollback(obj)
  puts "rolling back!"
  obj.replace(@original_state) if @original_state
end