-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowcase.rb
54 lines (43 loc) · 1.42 KB
/
showcase.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'spec', 'support', 'classes')
require 'petra'
require 'simple_user'
require 'simple_user_with_auto_save'
Petra.configure do
log_level :warn
end
def log(message, identifier = 'External')
puts [identifier, message].join(': ')
end
user = Classes::SimpleUserWithAutoSave.petra.new('John', 'Doe')
# Start a new transaction and start changing attributes
Petra.transaction(identifier: 'tr1') do
user.first_name = 'Foo'
end
# No changes outside the transaction yet...
log user.name #=> 'John Doe'
# Continue the same transaction
Petra.transaction(identifier: 'tr1') do
log(user.name, 'tr1') #=> 'Foo Doe'
user.last_name = 'Bar'
end
# Another transaction changes a value already changed in 'tr1'
Petra.transaction(identifier: 'tr2') do
log(user.name, 'tr2') #=> John Doe
user.first_name = 'Moo'
Petra.commit!
end
log user.name #=> 'Moo Doe'
# Try to commit our first transaction
Petra.transaction(identifier: 'tr1') do
log(user.name, 'tr1')
Petra.commit!
rescue Petra::WriteClashError => e
# => "The attribute `first_name` has been changed externally and in the transaction. (Petra::WriteClashError)"
# Let's use our value and go on with committing the transaction
e.use_ours!
e.continue!
end
# The actual object is updated with the values from tr1
log user.name #=> 'Foo Bar'