persist 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/persist/persist.rb +44 -24
- data/lib/persist/version.rb +1 -1
- data/test/test_persist.rb +7 -7
- metadata +2 -2
data/lib/persist/persist.rb
CHANGED
@@ -33,6 +33,35 @@ module Persist
|
|
33
33
|
@db
|
34
34
|
end
|
35
35
|
|
36
|
+
def initialize_db
|
37
|
+
@db || pull
|
38
|
+
end
|
39
|
+
|
40
|
+
# Public: Process multiple transactions to set table values and commit if
|
41
|
+
# all transactions are successful.
|
42
|
+
#
|
43
|
+
# block - A required block that processes multiple transactions that
|
44
|
+
# succeed or fail together to ensure that data is not left in a
|
45
|
+
# transitory state.
|
46
|
+
#
|
47
|
+
# Examples
|
48
|
+
#
|
49
|
+
# Persist.transaction do |db|
|
50
|
+
# db[:weather] = 'sunny'
|
51
|
+
# db.delete[:author]
|
52
|
+
# end
|
53
|
+
# # => nil
|
54
|
+
#
|
55
|
+
# Returns nothing.
|
56
|
+
def transaction
|
57
|
+
initialize_db
|
58
|
+
|
59
|
+
@db.transaction do
|
60
|
+
yield @db
|
61
|
+
@db.commit
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
36
65
|
# Public: Fetch a list of persistent store root tables.
|
37
66
|
#
|
38
67
|
# Examples
|
@@ -42,6 +71,8 @@ module Persist
|
|
42
71
|
#
|
43
72
|
# Returns an Array containing the persistent store root tables.
|
44
73
|
def keys
|
74
|
+
initialize_db
|
75
|
+
|
45
76
|
@db.transaction true do
|
46
77
|
@db.roots
|
47
78
|
end
|
@@ -62,6 +93,8 @@ module Persist
|
|
62
93
|
#
|
63
94
|
# Returns true or false.
|
64
95
|
def key? table
|
96
|
+
initialize_db
|
97
|
+
|
65
98
|
@db.transaction true do
|
66
99
|
@db.root? table
|
67
100
|
end
|
@@ -82,6 +115,8 @@ module Persist
|
|
82
115
|
#
|
83
116
|
# Returns the value stored in the fetched table.
|
84
117
|
def [] table
|
118
|
+
initialize_db
|
119
|
+
|
85
120
|
@db.transaction true do
|
86
121
|
@db[table]
|
87
122
|
end
|
@@ -107,6 +142,8 @@ module Persist
|
|
107
142
|
#
|
108
143
|
# Returns the value stored in the fetched table.
|
109
144
|
def fetch table, default = nil
|
145
|
+
initialize_db
|
146
|
+
|
110
147
|
@db.transaction true do
|
111
148
|
@db.fetch table, default
|
112
149
|
end
|
@@ -125,34 +162,13 @@ module Persist
|
|
125
162
|
#
|
126
163
|
# Returns the value of the table.
|
127
164
|
def []= table, value
|
165
|
+
initialize_db
|
166
|
+
|
128
167
|
@db.transaction do
|
129
168
|
@db[table] = value
|
130
169
|
end
|
131
170
|
end
|
132
|
-
|
133
|
-
# Public: Process multiple transactions to set table values and commit if
|
134
|
-
# all transactions are successful.
|
135
|
-
#
|
136
|
-
# block - A required block that processes multiple transactions that
|
137
|
-
# succeed or fail together to ensure that data is not left in a
|
138
|
-
# transitory state.
|
139
|
-
#
|
140
|
-
# Examples
|
141
|
-
#
|
142
|
-
# Persist.transaction do
|
143
|
-
# Persist.db[:weather] = 'sunny'
|
144
|
-
# Persist.db.delete[:author]
|
145
|
-
# end
|
146
|
-
# # => nil
|
147
|
-
#
|
148
|
-
# Returns nothing.
|
149
|
-
def transaction &block
|
150
|
-
@db.transaction do
|
151
|
-
yield
|
152
|
-
@db.commit
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
171
|
+
|
156
172
|
# Public: Delete one or more entire root tables from the persistent store.
|
157
173
|
#
|
158
174
|
# tables - One or more Symbols corresponding to root table keys in the
|
@@ -168,6 +184,8 @@ module Persist
|
|
168
184
|
#
|
169
185
|
# Returns nothing.
|
170
186
|
def delete *tables
|
187
|
+
initialize_db
|
188
|
+
|
171
189
|
@db.transaction do
|
172
190
|
tables.each do |table|
|
173
191
|
@db.delete table
|
@@ -185,6 +203,8 @@ module Persist
|
|
185
203
|
#
|
186
204
|
# Returns the path to the data file as a String.
|
187
205
|
def path
|
206
|
+
initialize_db
|
207
|
+
|
188
208
|
@db.path
|
189
209
|
end
|
190
210
|
end
|
data/lib/persist/version.rb
CHANGED
data/test/test_persist.rb
CHANGED
@@ -69,19 +69,19 @@ describe Persist do
|
|
69
69
|
|
70
70
|
describe "a Persist.transaction do" do
|
71
71
|
it "sets multiple keys when commited" do
|
72
|
-
Persist.transaction do
|
73
|
-
|
74
|
-
|
72
|
+
Persist.transaction do |db|
|
73
|
+
db[:one] = 'first'
|
74
|
+
db[:two] = 'second'
|
75
75
|
end
|
76
76
|
assert_equal 'first', Persist[:one]
|
77
77
|
assert_equal 'second', Persist[:two]
|
78
78
|
end
|
79
79
|
|
80
80
|
it "sets no keys when aborted" do
|
81
|
-
Persist.transaction do
|
82
|
-
|
83
|
-
|
84
|
-
|
81
|
+
Persist.transaction do |db|
|
82
|
+
db[:pre] = 'before'
|
83
|
+
db.abort
|
84
|
+
db[:post] = 'after'
|
85
85
|
end
|
86
86
|
assert_nil Persist[:pre]
|
87
87
|
assert_nil Persist[:post]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: persist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minitest
|