Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add transaction

  • Loading branch information...
commit 269634ce6289f121607ed2c86c0495139e08a75f 1 parent ddac300
Francis Chong authored
20 README.md
Source Rendered
@@ -92,6 +92,26 @@ user = User.find(:name, NSFEqualTo, "Bob").first
92 92 user.delete
93 93 ````
94 94
  95 +## Using Transaction
  96 +
  97 +Use transaction is easy, just wrap your database code in a transaction block.
  98 +
  99 +```ruby
  100 +store = NanoStore.shared_store = NanoStore.store
  101 +
  102 +store.transaction do |the_store|
  103 + Animal.count.should == 0
  104 + obj1 = Animal.new
  105 + obj1.name = "Cat"
  106 + obj2 = Animal.new
  107 + obj2.name = "Dog"
  108 + store.changed? # => should be true at this point
  109 + raise "error" # => an error happened!
  110 +end
  111 +
  112 +store.changed? # => should be false
  113 +```
  114 +
95 115 ## Using Bags
96 116
97 117 A bag is a loose collection of objects stored in a document store.
4 lib/nano_store/store_extension.rb
@@ -127,14 +127,14 @@ def count(clazz)
127 127 end
128 128
129 129 # Create a transaction
130   - def transcation
  130 + def transaction
131 131 error_ptr = Pointer.new(:id)
132 132 beginTransactionAndReturnError(error_ptr)
133 133 raise NanoStoreError, error_ptr[0].description if error_ptr[0]
134 134
135 135 begin
136 136 yield self
137   - rescue e
  137 + rescue StandardError => e
138 138 rollbackTransactionAndReturnError(error_ptr)
139 139 raise e
140 140 end
27 spec/store_extension_spec.rb
@@ -69,21 +69,40 @@ class Animal < NanoStore::Model
69 69 Animal.count.should == 0
70 70 end
71 71
72   - it "should create a transaction and rollback when fail" do
73   - begin
74   - store = NanoStore.shared_store = NanoStore.store
  72 + it "should create a transaction and commit" do
  73 + store = NanoStore.shared_store = NanoStore.store
  74 + store.transaction do |the_store|
  75 + Animal.count.should == 0
  76 + obj1 = Animal.new
  77 + obj1.name = "Cat"
  78 + obj1.save
75 79
  80 + obj2 = Animal.new
  81 + obj2.name = "Dog"
  82 + obj2.save
  83 + end
  84 + store.save
  85 + Animal.count.should == 2
  86 + end
  87 +
  88 + it "should create a transaction and rollback when fail" do
  89 + store = NanoStore.shared_store = NanoStore.store
  90 + begin
76 91 store.transaction do |the_store|
77 92 Animal.count.should == 0
78 93 obj1 = Animal.new
79 94 obj1.name = "Cat"
  95 + obj1.save
  96 +
80 97 obj2 = Animal.new
81 98 obj2.name = "Dog"
82   - the_store.changed?.should.be.true
  99 + obj2.save
  100 +
83 101 raise "error"
84 102 end
85 103 rescue
86 104 end
  105 + store.save
87 106 Animal.count.should == 0
88 107 end
89 108

0 comments on commit 269634c

Please sign in to comment.
Something went wrong with that request. Please try again.