persist 0.1.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -1
- data/README.md +35 -17
- data/lib/persist.rb +190 -2
- data/lib/persist/version.rb +2 -2
- data/test/helper.rb +1 -1
- data/test/persist_test.rb +43 -47
- metadata +11 -12
- data/lib/persist/persist.rb +0 -211
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f1c70f2b195711a276e56d584a1f295b2f631a5
|
4
|
+
data.tar.gz: e2c7509fb56d7dcdd7f48e818f0d3736b2bf9523
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea2768e1355af8ac7dc0dd98deaa7d3900695bc52e4b02e56b45b389c21039e782fa03833172ce146a03bd2b150860edd9cebb7344face0ad4c187a82cd2cdb2
|
7
|
+
data.tar.gz: a899d08b24c0c0192e1227cf247937d241b62f357c73427906c9d8b83a82332cf000c3483c61e507113a79031bff6fe8304da2eb6e55cf5c90ac39c17cf92871
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -3,66 +3,84 @@
|
|
3
3
|
|
4
4
|
The Persist gem makes it really, really simple to persist Ruby objects to disk. Persist uses Ruby's PStore class to serialize Ruby objects with Marshal and transactionally save them for retrieval later.
|
5
5
|
|
6
|
-
## Installation
|
7
|
-
Install the gem from the command line:
|
8
|
-
`gem install persist`
|
9
|
-
|
10
6
|
## Usage
|
11
7
|
Example in irb or [Pry](http://pryrepl.org):
|
12
8
|
```ruby
|
13
9
|
require 'persist'
|
14
|
-
|
10
|
+
|
11
|
+
store = Persist.new
|
12
|
+
store[:pie] = ['Key Lime', 'Strawberry Rhubarb', 'Blackberry Cobbler']
|
15
13
|
# => ["Key Lime", "Strawberry Rhubarb", "Blackberry Cobbler"]
|
16
14
|
```
|
17
15
|
|
18
16
|
You can now exit irb or [Pry](http://pryrepl.org) and your Ruby objects are still there:
|
19
17
|
```ruby
|
20
18
|
require 'persist'
|
21
|
-
|
19
|
+
|
20
|
+
store = Persist.new
|
21
|
+
store[:pie]
|
22
22
|
#=> ["Key Lime", "Strawberry Rhubarb", "Blackberry Cobbler"]
|
23
23
|
```
|
24
24
|
|
25
25
|
## Transactions
|
26
26
|
Transactions succeed or fail together to ensure that data is not left in a transitory state:
|
27
27
|
```ruby
|
28
|
-
|
28
|
+
store.transaction do |db|
|
29
29
|
db[:ice_cream] = ['chocolate', 'vanilla']
|
30
30
|
db.delete :pie
|
31
31
|
end
|
32
32
|
```
|
33
33
|
|
34
34
|
## Helper Methods
|
35
|
-
|
35
|
+
Look up table keys:
|
36
36
|
```ruby
|
37
|
-
|
37
|
+
store.keys
|
38
38
|
#=> [:pie, :ice_cream]
|
39
39
|
|
40
|
-
|
40
|
+
store.key? :pie
|
41
41
|
#=> true
|
42
42
|
|
43
|
-
|
43
|
+
store.key? :cake
|
44
44
|
#=> false
|
45
45
|
```
|
46
46
|
|
47
|
-
|
47
|
+
Delete tables:
|
48
48
|
```ruby
|
49
|
-
|
49
|
+
store.delete :pie
|
50
50
|
#=> nil
|
51
51
|
```
|
52
52
|
|
53
|
-
|
53
|
+
## File Store Path
|
54
|
+
Set the path to the persistant store file upon initialization:
|
55
|
+
```ruby
|
56
|
+
store = Persist.new "../.db.pstore"
|
57
|
+
store.path
|
58
|
+
#=> "../.db.pstore"
|
59
|
+
```
|
60
|
+
|
61
|
+
The default path is ".db.pstore" if one is not otherwise specified:
|
54
62
|
```ruby
|
55
|
-
Persist.
|
63
|
+
store = Persist.new
|
64
|
+
store.path
|
56
65
|
#=> ".db.pstore"
|
57
66
|
```
|
58
67
|
|
59
|
-
|
68
|
+
## Installation
|
69
|
+
Install the gem from the command line:
|
70
|
+
```bash
|
71
|
+
gem install persist
|
72
|
+
```
|
73
|
+
|
74
|
+
Or add the gem to your app's Gemfile and `bundle install`:
|
75
|
+
```ruby
|
76
|
+
gem 'persist'
|
77
|
+
```
|
60
78
|
|
61
79
|
## Supported Platforms
|
62
80
|
|
63
81
|
Persist takes advantage of PStore's [ultra_safe attribute](http://ruby-doc.org/stdlib-2.0/libdoc/pstore/rdoc/PStore.html#ultra_safe-attribute-method), which requires:
|
64
82
|
|
65
|
-
1. Ruby 1.9
|
83
|
+
1. Ruby 1.9 or higher.
|
66
84
|
2. A POSIX compliant platform (such as OS X, GNU/Linux or a BSD).
|
67
85
|
|
68
86
|
## Contributing
|
data/lib/persist.rb
CHANGED
@@ -1,2 +1,190 @@
|
|
1
|
-
require '
|
2
|
-
|
1
|
+
require 'pstore'
|
2
|
+
|
3
|
+
# Public: Implements a DSL around Ruby Standard Library's PStore to facilitate
|
4
|
+
# simple file-persistant storage of Ruby objects in a transactional NoSQL
|
5
|
+
# database.
|
6
|
+
class Persist
|
7
|
+
# Public: Returns the persistant store Object if initialized.
|
8
|
+
attr_reader :db
|
9
|
+
|
10
|
+
# Public: Returns the path to the persistent store file.
|
11
|
+
attr_reader :path
|
12
|
+
|
13
|
+
# Initializes the PStore Object and sets thread_safe and ultra_safe to true.
|
14
|
+
# Creates the file store at the specified path if it does not exist.
|
15
|
+
#
|
16
|
+
# path - An optional String representing the relative path of the file.
|
17
|
+
#
|
18
|
+
# Examples
|
19
|
+
#
|
20
|
+
# store = Persist.new
|
21
|
+
# # => #<Persist:0x007f97b1b1b930
|
22
|
+
# @db=
|
23
|
+
# #<PStore:0x007f97b1b1b8e0
|
24
|
+
# @abort=false,
|
25
|
+
# @filename=".db.pstore",
|
26
|
+
# @lock=#<Mutex:0x007f97b1b1b7c8>,
|
27
|
+
# @rdonly=true,
|
28
|
+
# @table=
|
29
|
+
# {:trees=>["oak", "pine", "cedar"],
|
30
|
+
# :one=>"first",
|
31
|
+
# :two=>"second",
|
32
|
+
# :author=>{:first_name=>"Shannon", :last_name=>"Skipper"},
|
33
|
+
# :aim=>true,
|
34
|
+
# :pie=>["Key Lime", "Strawberry Rhubarb", "Blackberry Cobbler"]},
|
35
|
+
# @thread_safe=true,
|
36
|
+
# @ultra_safe=true>,
|
37
|
+
# @path=".db.pstore">
|
38
|
+
#
|
39
|
+
# Returns the entire persistent store Object.
|
40
|
+
def initialize path = '.db.pstore'
|
41
|
+
@path = path
|
42
|
+
@db = PStore.new path, true
|
43
|
+
@db.ultra_safe = true
|
44
|
+
@db.transaction(true) {}
|
45
|
+
@db
|
46
|
+
end
|
47
|
+
|
48
|
+
# Public: Process multiple transactions to set table values and commit if
|
49
|
+
# all transactions are successful.
|
50
|
+
#
|
51
|
+
# block - A required block that processes multiple transactions that
|
52
|
+
# succeed or fail together to ensure that data is not left in a
|
53
|
+
# transitory state.
|
54
|
+
#
|
55
|
+
# Examples
|
56
|
+
#
|
57
|
+
# store.transaction do |db|
|
58
|
+
# db[:weather] = 'sunny'
|
59
|
+
# db.delete[:author]
|
60
|
+
# end
|
61
|
+
# # => nil
|
62
|
+
#
|
63
|
+
# Returns nothing.
|
64
|
+
def transaction
|
65
|
+
@db.transaction do
|
66
|
+
yield @db
|
67
|
+
@db.commit
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Public: Fetch a list of persistent store root tables.
|
72
|
+
#
|
73
|
+
# Examples
|
74
|
+
#
|
75
|
+
# store.keys
|
76
|
+
# # => [:author]
|
77
|
+
#
|
78
|
+
# Returns an Array containing the persistent store root tables.
|
79
|
+
def keys
|
80
|
+
@db.transaction true do
|
81
|
+
@db.roots
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# Public: Determine whether a particular persistent store root table
|
86
|
+
# exists.
|
87
|
+
#
|
88
|
+
# table - A Symbol.
|
89
|
+
#
|
90
|
+
# Examples
|
91
|
+
#
|
92
|
+
# store.key? :author
|
93
|
+
# # => true
|
94
|
+
#
|
95
|
+
# store.key? :this_does_not_exist
|
96
|
+
# # => false
|
97
|
+
#
|
98
|
+
# Returns true or false.
|
99
|
+
def key? table
|
100
|
+
@db.transaction true do
|
101
|
+
@db.root? table
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# Public: Fetch a particular table from the persistent store.
|
106
|
+
#
|
107
|
+
# table - A Symbol corresponding to a root table key in the persistent
|
108
|
+
# store.
|
109
|
+
#
|
110
|
+
# Examples
|
111
|
+
#
|
112
|
+
# store[:author]
|
113
|
+
# # => {:first_name => "Shannon", :last_name => "Skipper"}
|
114
|
+
#
|
115
|
+
# store[:author][:first_name]
|
116
|
+
# # => "Shannon"
|
117
|
+
#
|
118
|
+
# Returns the value stored in the fetched table.
|
119
|
+
def [] table
|
120
|
+
@db.transaction true do
|
121
|
+
@db[table]
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
# Public: Fetch a particular table from the persistent store.
|
126
|
+
#
|
127
|
+
# table - A Symbol corresponding to a root table key in the persistent
|
128
|
+
# store.
|
129
|
+
#
|
130
|
+
# default - An optional value that is returned if the table is not found.
|
131
|
+
#
|
132
|
+
# Examples
|
133
|
+
#
|
134
|
+
# store.fetch :author
|
135
|
+
# # => {:first_name => "Shannon", :last_name => "Skipper"}
|
136
|
+
#
|
137
|
+
# store.fetch :snowman
|
138
|
+
# # => nil
|
139
|
+
#
|
140
|
+
# store.fetch :snowman, 'default value instead of nil'
|
141
|
+
# # => "default value instead of nil"
|
142
|
+
#
|
143
|
+
# Returns the value stored in the fetched table.
|
144
|
+
def fetch table, default = nil
|
145
|
+
@db.transaction true do
|
146
|
+
@db.fetch table, default
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
# Public: Use a single transaction to set a table value.
|
151
|
+
#
|
152
|
+
# table - A Symbol.
|
153
|
+
#
|
154
|
+
# value - Any Ruby Object that is marshallable.
|
155
|
+
#
|
156
|
+
# Examples
|
157
|
+
#
|
158
|
+
# store[:sky] = 'blue'
|
159
|
+
# # => "blue"
|
160
|
+
#
|
161
|
+
# Returns the value of the table.
|
162
|
+
def []= table, value
|
163
|
+
@db.transaction do
|
164
|
+
@db[table] = value
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
# Public: Delete one or more entire root tables from the persistent store.
|
169
|
+
#
|
170
|
+
# tables - One or more Symbols corresponding to root table keys in the
|
171
|
+
# persistent store.
|
172
|
+
#
|
173
|
+
# Examples
|
174
|
+
#
|
175
|
+
# store.delete :author
|
176
|
+
# # => nil
|
177
|
+
#
|
178
|
+
# store.delete :author, :clients, :rentals
|
179
|
+
# # => nil
|
180
|
+
#
|
181
|
+
# Returns nothing.
|
182
|
+
def delete *tables
|
183
|
+
@db.transaction do
|
184
|
+
tables.each do |table|
|
185
|
+
@db.delete table
|
186
|
+
end
|
187
|
+
@db.commit
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
data/lib/persist/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = '0.
|
1
|
+
class Persist
|
2
|
+
VERSION = '1.0.0'
|
3
3
|
end
|
data/test/helper.rb
CHANGED
data/test/persist_test.rb
CHANGED
@@ -2,109 +2,105 @@ require_relative 'helper'
|
|
2
2
|
|
3
3
|
describe Persist do
|
4
4
|
before do
|
5
|
-
Persist.
|
6
|
-
Persist[:author] = {first_name: 'Shannon', last_name: 'Skipper'}
|
5
|
+
@store = Persist.new
|
7
6
|
end
|
8
|
-
|
9
|
-
describe "initializing the persistent store with Persist.db" do
|
10
|
-
it "returns a PStore object" do
|
11
|
-
assert_equal PStore, Persist.db.class
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
7
|
+
|
15
8
|
describe "getting a list of root keys with Persist.keys" do
|
16
9
|
it "returns an Array" do
|
17
|
-
assert_kind_of Array,
|
10
|
+
assert_kind_of Array, @store.keys
|
18
11
|
end
|
19
12
|
end
|
20
|
-
|
13
|
+
|
21
14
|
describe "getting true or false if key exists with Persist.key?" do
|
22
15
|
it "returns true if key exists" do
|
23
|
-
|
16
|
+
@store[:author] = {first_name: 'Shannon', last_name: 'Skipper'}
|
17
|
+
assert @store.key?(:author)
|
24
18
|
end
|
25
|
-
|
19
|
+
|
26
20
|
it "returns false if the key doesn't exist" do
|
27
|
-
refute
|
21
|
+
refute @store.key?(:this_does_not_exist)
|
28
22
|
end
|
29
23
|
end
|
30
|
-
|
24
|
+
|
31
25
|
describe "getting a particular key's value with Persist.[]" do
|
32
26
|
it "returns a value if the key exists" do
|
33
|
-
|
27
|
+
@store[:author] = {first_name: 'Shannon', last_name: 'Skipper'}
|
28
|
+
assert_equal "Shannon", @store[:author][:first_name]
|
34
29
|
end
|
35
|
-
|
30
|
+
|
36
31
|
it "returns nil if the key doesn't exist" do
|
37
|
-
assert_nil
|
32
|
+
assert_nil @store[:this_does_not_exist]
|
38
33
|
end
|
39
34
|
end
|
40
|
-
|
35
|
+
|
41
36
|
describe "getting a particular key or default value with Persist.fetch" do
|
42
37
|
it "returns a value if the key exists" do
|
43
|
-
|
38
|
+
@store[:author] = {first_name: 'Shannon', last_name: 'Skipper'}
|
39
|
+
assert_equal "Shannon", @store.fetch(:author)[:first_name]
|
44
40
|
end
|
45
|
-
|
41
|
+
|
46
42
|
it "returns nil if the key doesn't exist and no default is given" do
|
47
|
-
assert_nil
|
43
|
+
assert_nil @store.fetch(:this_does_not_exist)
|
48
44
|
end
|
49
|
-
|
45
|
+
|
50
46
|
it "returns the default value if the key doesn't exist" do
|
51
|
-
default =
|
47
|
+
default = @store.fetch(:this_does_not_exist, "default value")
|
52
48
|
assert_equal "default value", default
|
53
49
|
end
|
54
50
|
end
|
55
|
-
|
51
|
+
|
56
52
|
describe "setting a particular key's value with Persist.[]=" do
|
57
53
|
before do
|
58
|
-
|
54
|
+
@store[:trees] = ['oak', 'pine', 'cedar']
|
59
55
|
end
|
60
|
-
|
61
|
-
it "should add the key to the persistent store" do
|
62
|
-
assert
|
56
|
+
|
57
|
+
it "should add the key to the persistent @store" do
|
58
|
+
assert @store.key?(:trees)
|
63
59
|
end
|
64
|
-
|
60
|
+
|
65
61
|
it "should be set to the expected value" do
|
66
|
-
assert_equal ["oak", "pine", "cedar"],
|
62
|
+
assert_equal ["oak", "pine", "cedar"], @store[:trees]
|
67
63
|
end
|
68
64
|
end
|
69
|
-
|
65
|
+
|
70
66
|
describe "a Persist.transaction do" do
|
71
67
|
it "sets multiple keys when commited" do
|
72
|
-
|
68
|
+
@store.transaction do |db|
|
73
69
|
db[:one] = 'first'
|
74
70
|
db[:two] = 'second'
|
75
71
|
end
|
76
|
-
assert_equal 'first',
|
77
|
-
assert_equal 'second',
|
72
|
+
assert_equal 'first', @store[:one]
|
73
|
+
assert_equal 'second', @store[:two]
|
78
74
|
end
|
79
|
-
|
75
|
+
|
80
76
|
it "sets no keys when aborted" do
|
81
|
-
|
77
|
+
@store.transaction do |db|
|
82
78
|
db[:pre] = 'before'
|
83
79
|
db.abort
|
84
80
|
db[:post] = 'after'
|
85
81
|
end
|
86
|
-
assert_nil
|
87
|
-
assert_nil
|
82
|
+
assert_nil @store[:pre]
|
83
|
+
assert_nil @store[:post]
|
88
84
|
end
|
89
85
|
end
|
90
|
-
|
86
|
+
|
91
87
|
describe "deleting a root key with Persist.delete" do
|
92
88
|
before do
|
93
|
-
|
89
|
+
@store.delete :author
|
94
90
|
end
|
95
91
|
|
96
92
|
it "returns nil because the key no longer exists" do
|
97
|
-
assert_nil
|
93
|
+
assert_nil @store[:author]
|
98
94
|
end
|
99
95
|
end
|
100
|
-
|
96
|
+
|
101
97
|
describe "check path with Persist.path" do
|
102
98
|
it "returns a String" do
|
103
|
-
assert_kind_of String,
|
99
|
+
assert_kind_of String, @store.path
|
104
100
|
end
|
105
|
-
|
106
|
-
it "includes the data store file name" do
|
107
|
-
assert_includes ".db.pstore",
|
101
|
+
|
102
|
+
it "includes the data @store file name" do
|
103
|
+
assert_includes ".db.pstore", @store.path
|
108
104
|
end
|
109
105
|
end
|
110
106
|
end
|
metadata
CHANGED
@@ -1,41 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: persist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shannon Skipper
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-09-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
description: The Persist gem makes it really, really simple to persist Ruby objects
|
@@ -46,14 +46,13 @@ executables: []
|
|
46
46
|
extensions: []
|
47
47
|
extra_rdoc_files: []
|
48
48
|
files:
|
49
|
-
- .gitignore
|
50
|
-
- .travis.yml
|
49
|
+
- ".gitignore"
|
50
|
+
- ".travis.yml"
|
51
51
|
- Gemfile
|
52
52
|
- LICENSE
|
53
53
|
- README.md
|
54
54
|
- Rakefile
|
55
55
|
- lib/persist.rb
|
56
|
-
- lib/persist/persist.rb
|
57
56
|
- lib/persist/version.rb
|
58
57
|
- persist.gemspec
|
59
58
|
- test/helper.rb
|
@@ -68,17 +67,17 @@ require_paths:
|
|
68
67
|
- lib
|
69
68
|
required_ruby_version: !ruby/object:Gem::Requirement
|
70
69
|
requirements:
|
71
|
-
- -
|
70
|
+
- - ">="
|
72
71
|
- !ruby/object:Gem::Version
|
73
72
|
version: '0'
|
74
73
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
74
|
requirements:
|
76
|
-
- -
|
75
|
+
- - ">="
|
77
76
|
- !ruby/object:Gem::Version
|
78
77
|
version: '0'
|
79
78
|
requirements: []
|
80
79
|
rubyforge_project:
|
81
|
-
rubygems_version: 2.
|
80
|
+
rubygems_version: 2.4.1
|
82
81
|
signing_key:
|
83
82
|
specification_version: 4
|
84
83
|
summary: Persist Ruby objects to a transactional file store using Ruby's Pstore.
|
data/lib/persist/persist.rb
DELETED
@@ -1,211 +0,0 @@
|
|
1
|
-
require 'pstore'
|
2
|
-
|
3
|
-
# Public: Implements a DSL around Ruby Standard Library's PStore to facilitate
|
4
|
-
# simple file-persistant storage of Ruby objects in a transactional NoSQL
|
5
|
-
# database. All methods are module methods and should be called on the Persist
|
6
|
-
# module.
|
7
|
-
module Persist
|
8
|
-
class << self
|
9
|
-
# Public: Returns the persistant store Object if initialized.
|
10
|
-
attr_reader :db
|
11
|
-
|
12
|
-
# Public: Initialize the PStore Object--deserializing the marshalled Hash
|
13
|
-
# stored in the '.db.pstore' file (creating the file if it does't exist)--
|
14
|
-
# and set thread_safe and ultra_safe to true.
|
15
|
-
#
|
16
|
-
# Examples
|
17
|
-
#
|
18
|
-
# Persist.pull
|
19
|
-
# # => #<PStore:0x007f8c199c9698
|
20
|
-
# @abort=false,
|
21
|
-
# @filename=".db.pstore",
|
22
|
-
# @lock=#<Mutex:0x007f8c199c9580>,
|
23
|
-
# @rdonly=true,
|
24
|
-
# @table={},
|
25
|
-
# @thread_safe=true,
|
26
|
-
# @ultra_safe=true>
|
27
|
-
#
|
28
|
-
# Returns the entire persistent store Object.
|
29
|
-
def pull
|
30
|
-
@db = PStore.new '.db.pstore', true
|
31
|
-
@db.ultra_safe = true
|
32
|
-
@db.transaction(true) {}
|
33
|
-
@db
|
34
|
-
end
|
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
|
-
|
65
|
-
# Public: Fetch a list of persistent store root tables.
|
66
|
-
#
|
67
|
-
# Examples
|
68
|
-
#
|
69
|
-
# Persist.keys
|
70
|
-
# # => [:author]
|
71
|
-
#
|
72
|
-
# Returns an Array containing the persistent store root tables.
|
73
|
-
def keys
|
74
|
-
initialize_db
|
75
|
-
|
76
|
-
@db.transaction true do
|
77
|
-
@db.roots
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
# Public: Determine whether a particular persistent store root table
|
82
|
-
# exists.
|
83
|
-
#
|
84
|
-
# table - A Symbol.
|
85
|
-
#
|
86
|
-
# Examples
|
87
|
-
#
|
88
|
-
# Persist.key? :author
|
89
|
-
# # => true
|
90
|
-
#
|
91
|
-
# Persist.key? :this_does_not_exist
|
92
|
-
# # => false
|
93
|
-
#
|
94
|
-
# Returns true or false.
|
95
|
-
def key? table
|
96
|
-
initialize_db
|
97
|
-
|
98
|
-
@db.transaction true do
|
99
|
-
@db.root? table
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
# Public: Fetch a particular table from the persistent store.
|
104
|
-
#
|
105
|
-
# table - A Symbol corresponding to a root table key in the persistent
|
106
|
-
# store.
|
107
|
-
#
|
108
|
-
# Examples
|
109
|
-
#
|
110
|
-
# Persist[:author]
|
111
|
-
# # => {:first_name => "Shannon", :last_name => "Skipper"}
|
112
|
-
#
|
113
|
-
# Persist[:author][:first_name]
|
114
|
-
# # => "Shannon"
|
115
|
-
#
|
116
|
-
# Returns the value stored in the fetched table.
|
117
|
-
def [] table
|
118
|
-
initialize_db
|
119
|
-
|
120
|
-
@db.transaction true do
|
121
|
-
@db[table]
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
# Public: Fetch a particular table from the persistent store.
|
126
|
-
#
|
127
|
-
# table - A Symbol corresponding to a root table key in the persistent
|
128
|
-
# store.
|
129
|
-
#
|
130
|
-
# default - An optional value that is returned if the table is not found.
|
131
|
-
#
|
132
|
-
# Examples
|
133
|
-
#
|
134
|
-
# Persist.fetch :author
|
135
|
-
# # => {:first_name => "Shannon", :last_name => "Skipper"}
|
136
|
-
#
|
137
|
-
# Persist.fetch :snowman
|
138
|
-
# # => nil
|
139
|
-
#
|
140
|
-
# Persist.fetch :snowman, 'default value instead of nil'
|
141
|
-
# # => "default value instead of nil"
|
142
|
-
#
|
143
|
-
# Returns the value stored in the fetched table.
|
144
|
-
def fetch table, default = nil
|
145
|
-
initialize_db
|
146
|
-
|
147
|
-
@db.transaction true do
|
148
|
-
@db.fetch table, default
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
# Public: Use a single transaction to set a table value.
|
153
|
-
#
|
154
|
-
# table - A Symbol.
|
155
|
-
#
|
156
|
-
# value - Any Ruby Object that is marshallable.
|
157
|
-
#
|
158
|
-
# Examples
|
159
|
-
#
|
160
|
-
# Persist[:sky] = 'blue'
|
161
|
-
# # => "blue"
|
162
|
-
#
|
163
|
-
# Returns the value of the table.
|
164
|
-
def []= table, value
|
165
|
-
initialize_db
|
166
|
-
|
167
|
-
@db.transaction do
|
168
|
-
@db[table] = value
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
# Public: Delete one or more entire root tables from the persistent store.
|
173
|
-
#
|
174
|
-
# tables - One or more Symbols corresponding to root table keys in the
|
175
|
-
# persistent store.
|
176
|
-
#
|
177
|
-
# Examples
|
178
|
-
#
|
179
|
-
# Persist.delete :author
|
180
|
-
# # => nil
|
181
|
-
#
|
182
|
-
# Persist.delete :author, :clients, :rentals
|
183
|
-
# # => nil
|
184
|
-
#
|
185
|
-
# Returns nothing.
|
186
|
-
def delete *tables
|
187
|
-
initialize_db
|
188
|
-
|
189
|
-
@db.transaction do
|
190
|
-
tables.each do |table|
|
191
|
-
@db.delete table
|
192
|
-
end
|
193
|
-
@db.commit
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
# Public: Determine location of the persistent store file.
|
198
|
-
#
|
199
|
-
# Examples
|
200
|
-
#
|
201
|
-
# Persist.path
|
202
|
-
# # => ".db.pstore"
|
203
|
-
#
|
204
|
-
# Returns the path to the data file as a String.
|
205
|
-
def path
|
206
|
-
initialize_db
|
207
|
-
|
208
|
-
@db.path
|
209
|
-
end
|
210
|
-
end
|
211
|
-
end
|