persist 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +6 -1
- data/lib/persist/persist.rb +163 -0
- data/lib/persist/version.rb +1 -1
- data/lib/persist.rb +2 -157
- data/persist.gemspec +1 -1
- data/test/helper.rb +1 -1
- data/test/test_persist.rb +11 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -15,6 +15,8 @@ And then execute:
|
|
15
15
|
Or install it yourself:
|
16
16
|
|
17
17
|
$ gem install persist
|
18
|
+
|
19
|
+
*Requires Ruby 1.9 or newer.
|
18
20
|
|
19
21
|
## Usage
|
20
22
|
|
@@ -48,7 +50,10 @@ Persist[:pie]
|
|
48
50
|
#=> ["Key Lime", "Strawberry Rhubarb", "Blackberry Cobbler"]
|
49
51
|
```
|
50
52
|
|
51
|
-
TODO: Document Persist.db's other public methods such as #transaction, #delete, #key? and #keys. The best documentation in the meanwhile is in the code itself: https://github.com/Havenwood/persist/blob/master/lib/persist.rb
|
53
|
+
TODO: Document Persist.db's other public methods such as #transaction, #delete, #path, #key? and #keys. The best documentation in the meanwhile is in the code itself: https://github.com/Havenwood/persist/blob/master/lib/persist/persist.rb
|
54
|
+
|
55
|
+
## Is It Production Ready™?
|
56
|
+
No. Persist.db is early Alpha, but please try it out and let me know if you find any bugs or real-world performance problems.
|
52
57
|
|
53
58
|
## Contributing
|
54
59
|
|
@@ -0,0 +1,163 @@
|
|
1
|
+
# Public: Implements a DSL around Ruby Standard Library's PStore to facilitate
|
2
|
+
# simple file-persistant storage of Ruby objects in a transactional NoSQL
|
3
|
+
# database.
|
4
|
+
module Persist
|
5
|
+
class << self
|
6
|
+
# Public: Returns the persistant store Object if initialized.
|
7
|
+
attr_reader :store
|
8
|
+
|
9
|
+
# Public: Initialize the PStore Object--deserializing the marshalled Hash
|
10
|
+
# stored in the '.db.pstore' file (creating the file if it does't exist)--
|
11
|
+
# and set thread_safe and ultra_safe to true.
|
12
|
+
#
|
13
|
+
# Examples
|
14
|
+
#
|
15
|
+
# Persist.db
|
16
|
+
# # => #<PStore:0x007f8c199c9698
|
17
|
+
# @abort=false,
|
18
|
+
# @filename=".db.pstore",
|
19
|
+
# @lock=#<Mutex:0x007f8c199c9580>,
|
20
|
+
# @rdonly=true,
|
21
|
+
# @table={},
|
22
|
+
# @thread_safe=true,
|
23
|
+
# @ultra_safe=true>
|
24
|
+
#
|
25
|
+
# Returns the entire persistent store Object.
|
26
|
+
def db
|
27
|
+
@store = PStore.new '.db.pstore', true
|
28
|
+
@store.ultra_safe = true
|
29
|
+
@store.transaction(true) {}
|
30
|
+
@store
|
31
|
+
end
|
32
|
+
|
33
|
+
# Public: Fetch a list of persistent store root tables.
|
34
|
+
#
|
35
|
+
# Examples
|
36
|
+
#
|
37
|
+
# Persist.keys
|
38
|
+
# # => [:author]
|
39
|
+
#
|
40
|
+
# Returns an Array containing the persistent store root tables.
|
41
|
+
def keys
|
42
|
+
@store.transaction true do
|
43
|
+
@store.roots
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Public: Determine whether a particular persistent store root table
|
48
|
+
# exists.
|
49
|
+
#
|
50
|
+
# table - A Symbol.
|
51
|
+
#
|
52
|
+
# Examples
|
53
|
+
#
|
54
|
+
# Persist.key? :author
|
55
|
+
# # => true
|
56
|
+
#
|
57
|
+
# Persist.key? :this_does_not_exist
|
58
|
+
# # => false
|
59
|
+
#
|
60
|
+
# Returns true or false.
|
61
|
+
def key? table
|
62
|
+
@store.transaction true do
|
63
|
+
@store.root? table
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Public: Fetch a particular table from the persistent store.
|
68
|
+
#
|
69
|
+
# table - A Symbol corresponding to a root table key in the persistent
|
70
|
+
# store.
|
71
|
+
#
|
72
|
+
# Examples
|
73
|
+
#
|
74
|
+
# Persist[:author]
|
75
|
+
# # => {:first_name => "Shannon", :last_name => "Skipper"}
|
76
|
+
#
|
77
|
+
# Persist[:author][:first_name]
|
78
|
+
# # => "Shannon"
|
79
|
+
#
|
80
|
+
# Returns the value stored in the fetched table.
|
81
|
+
def [] table
|
82
|
+
@store.transaction true do
|
83
|
+
@store[table]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
# Public: Use a single transaction to set a table value.
|
88
|
+
#
|
89
|
+
# table - A Symbol.
|
90
|
+
#
|
91
|
+
# value - Any Ruby Object that is marshallable.
|
92
|
+
#
|
93
|
+
# Examples
|
94
|
+
#
|
95
|
+
# Persist[:sky] = 'blue'
|
96
|
+
# # => "blue"
|
97
|
+
#
|
98
|
+
# Returns the value of the table.
|
99
|
+
def []= table, value
|
100
|
+
@store.transaction do
|
101
|
+
@store[table] = value
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# Public: Process multiple transactions to set table values and commit if
|
106
|
+
# all transactions are successful.
|
107
|
+
#
|
108
|
+
# block - A required block that processes multiple transactions that
|
109
|
+
# succeed or fail together to ensure that data is not left in a
|
110
|
+
# transitory state.
|
111
|
+
#
|
112
|
+
# Examples
|
113
|
+
#
|
114
|
+
# Persist.transaction do
|
115
|
+
# Persist.store[:weather] = 'sunny'
|
116
|
+
# Persist.store[:hour] = 'midday'
|
117
|
+
# end
|
118
|
+
# # => nil
|
119
|
+
#
|
120
|
+
# Returns nothing.
|
121
|
+
def transaction &block
|
122
|
+
@store.transaction do
|
123
|
+
yield
|
124
|
+
@store.commit
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
# Public: Delete one or more entire root tables from the persistent store.
|
129
|
+
#
|
130
|
+
# tables - One or more Symbols corresponding to root table keys in the
|
131
|
+
# persistent store.
|
132
|
+
#
|
133
|
+
# Examples
|
134
|
+
#
|
135
|
+
# Persist.delete :author
|
136
|
+
# # => nil
|
137
|
+
#
|
138
|
+
# Persist.delete :author, :clients, :rentals
|
139
|
+
# # => nil
|
140
|
+
#
|
141
|
+
# Returns nothing.
|
142
|
+
def delete *tables
|
143
|
+
@store.transaction do
|
144
|
+
tables.each do |table|
|
145
|
+
@store.delete table
|
146
|
+
end
|
147
|
+
@store.commit
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
# Public: Determine location of the persistent store file.
|
152
|
+
#
|
153
|
+
# Examples
|
154
|
+
#
|
155
|
+
# Persist.path
|
156
|
+
# # => ".db.pstore"
|
157
|
+
#
|
158
|
+
# Returns the path to the data file as a String.
|
159
|
+
def path
|
160
|
+
@store.path
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
data/lib/persist/version.rb
CHANGED
data/lib/persist.rb
CHANGED
@@ -1,158 +1,3 @@
|
|
1
1
|
require 'pstore'
|
2
|
-
require 'persist/
|
3
|
-
|
4
|
-
# Public: Implements a DSL around Ruby Standard Library's PStore to facilitate
|
5
|
-
# simple file-persistant storage of Ruby objects in a transactional NoSQL
|
6
|
-
# database.
|
7
|
-
module Persist
|
8
|
-
class << self
|
9
|
-
# Returns the persistant store Object if one has been initialized.
|
10
|
-
attr_reader :store
|
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
|
-
#
|
15
|
-
# Examples
|
16
|
-
#
|
17
|
-
# Persist.db
|
18
|
-
# # => #<PStore:0x007f8c199c9698
|
19
|
-
# @abort=false,
|
20
|
-
# @filename=".db.pstore",
|
21
|
-
# @lock=#<Mutex:0x007f8c199c9580>,
|
22
|
-
# @rdonly=true,
|
23
|
-
# @table={},
|
24
|
-
# @thread_safe=true,
|
25
|
-
# @ultra_safe=true>
|
26
|
-
#
|
27
|
-
# Returns the entire persistent store Object.
|
28
|
-
def db
|
29
|
-
# Initialize a persistent store in the file '.db.pstore' and set
|
30
|
-
# thread_safe to true.
|
31
|
-
@store = PStore.new('.db.pstore', true)
|
32
|
-
|
33
|
-
# Set ultru_safe to true for extra safety checks regarding full disk, etc.
|
34
|
-
@store.ultra_safe = true
|
35
|
-
|
36
|
-
# Open a read-only transaction to fetch latest store.
|
37
|
-
@store.transaction(true) {}
|
38
|
-
|
39
|
-
# Return the entire fetched store Object.
|
40
|
-
@store
|
41
|
-
end
|
42
|
-
|
43
|
-
# Public: Fetch a list of persistent store root tables.
|
44
|
-
#
|
45
|
-
# Examples
|
46
|
-
#
|
47
|
-
# Persist.keys
|
48
|
-
# # => [:author]
|
49
|
-
#
|
50
|
-
# Returns an Array containing the persistent store root tables.
|
51
|
-
def keys
|
52
|
-
# Open a read-only transaction.
|
53
|
-
@store.transaction(true) do
|
54
|
-
#Return a list of all tables in the persistent store.
|
55
|
-
@store.roots
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
# Public: Determine whether a particular persistent store root table exists.
|
60
|
-
#
|
61
|
-
# Examples
|
62
|
-
#
|
63
|
-
# Persist.key? :author
|
64
|
-
# # => true
|
65
|
-
#
|
66
|
-
# Persist.key? :this_does_not_exist
|
67
|
-
# # => false
|
68
|
-
#
|
69
|
-
# Returns true or false.
|
70
|
-
def key? table
|
71
|
-
# Open a read-only transaction.
|
72
|
-
@store.transaction(true) do
|
73
|
-
#Return a list of all tables in the persistent store.
|
74
|
-
@store.root? table
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
# Public: Fetch a particular table from the persistent store.
|
79
|
-
#
|
80
|
-
# table - A Symbol.
|
81
|
-
#
|
82
|
-
# Examples
|
83
|
-
#
|
84
|
-
# Persist[:author]
|
85
|
-
# # => {:first_name => "Shannon", :last_name => "Skipper"}
|
86
|
-
#
|
87
|
-
# Persist[:author][:first_name]
|
88
|
-
# # => "Shannon"
|
89
|
-
#
|
90
|
-
# Returns the value stored in the fetched table.
|
91
|
-
def [] table
|
92
|
-
# Open a read-only transaction.
|
93
|
-
@store.transaction(true) do
|
94
|
-
# Return a particular table from the persistent store.
|
95
|
-
@store[table]
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
# Public: Use a single transaction to set a table value.
|
100
|
-
#
|
101
|
-
# table - A Symbol.
|
102
|
-
#
|
103
|
-
# value - Any Ruby Object that is marshallable.
|
104
|
-
#
|
105
|
-
# Examples
|
106
|
-
#
|
107
|
-
# Persist[:sky] = 'blue'
|
108
|
-
# # => "blue"
|
109
|
-
#
|
110
|
-
# Returns the value of the table.
|
111
|
-
def []= table, value
|
112
|
-
# Open a writable transaction.
|
113
|
-
@store.transaction do
|
114
|
-
# Process the single transaction.
|
115
|
-
@store[table] = value
|
116
|
-
end # Commit transaction.
|
117
|
-
end
|
118
|
-
|
119
|
-
# Public: Process multiple transactions to set table values and commit if
|
120
|
-
# all are transactions are successful.
|
121
|
-
#
|
122
|
-
# block - A required block that processes multiple transactions that
|
123
|
-
# succeed or fail together to ensure that data is not left in a
|
124
|
-
# transitory state.
|
125
|
-
#
|
126
|
-
# Examples
|
127
|
-
#
|
128
|
-
# Persist.transaction do
|
129
|
-
# Persist.store[:weather] = 'sunny'
|
130
|
-
# Persist.store[:hour] = 'midday'
|
131
|
-
# end
|
132
|
-
# # => nil
|
133
|
-
#
|
134
|
-
# Returns nothing.
|
135
|
-
def transaction &block
|
136
|
-
@store.transaction do
|
137
|
-
yield
|
138
|
-
@store.commit
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
# Public: Delete an entire root table from the persistent store.
|
143
|
-
#
|
144
|
-
# Examples
|
145
|
-
#
|
146
|
-
# Persist.delete :author
|
147
|
-
# # => nil
|
148
|
-
#
|
149
|
-
# Returns nothing.
|
150
|
-
def delete table
|
151
|
-
# Open a writable transaction.
|
152
|
-
@store.transaction do
|
153
|
-
# Delete a particular table from the persistent store.
|
154
|
-
@store.delete table
|
155
|
-
end
|
156
|
-
end
|
157
|
-
end
|
158
|
-
end
|
2
|
+
require 'persist/persist'
|
3
|
+
require 'persist/version'
|
data/persist.gemspec
CHANGED
@@ -12,6 +12,6 @@ Gem::Specification.new do |gem|
|
|
12
12
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
13
|
gem.test_files = gem.files.grep(%r{^test/})
|
14
14
|
gem.name = 'persist'
|
15
|
-
gem.require_paths = ['lib'
|
15
|
+
gem.require_paths = ['lib']
|
16
16
|
gem.version = Persist::VERSION
|
17
17
|
end
|
data/test/helper.rb
CHANGED
data/test/test_persist.rb
CHANGED
@@ -58,7 +58,7 @@ describe Persist do
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
|
-
describe "deleting a root key" do
|
61
|
+
describe "deleting a root key with Persist.delete(:key)" do
|
62
62
|
before do
|
63
63
|
Persist.delete :author
|
64
64
|
end
|
@@ -67,4 +67,14 @@ describe Persist do
|
|
67
67
|
assert_nil Persist[:author]
|
68
68
|
end
|
69
69
|
end
|
70
|
+
|
71
|
+
describe "check path with Persist.path" do
|
72
|
+
it "returns a String" do
|
73
|
+
assert_kind_of String, Persist.path
|
74
|
+
end
|
75
|
+
|
76
|
+
it "includes the data store file name" do
|
77
|
+
assert_includes ".db.pstore", Persist.path
|
78
|
+
end
|
79
|
+
end
|
70
80
|
end
|
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.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -24,6 +24,7 @@ files:
|
|
24
24
|
- LICENSE
|
25
25
|
- README.md
|
26
26
|
- lib/persist.rb
|
27
|
+
- lib/persist/persist.rb
|
27
28
|
- lib/persist/version.rb
|
28
29
|
- persist.gemspec
|
29
30
|
- test/helper.rb
|
@@ -34,7 +35,6 @@ post_install_message:
|
|
34
35
|
rdoc_options: []
|
35
36
|
require_paths:
|
36
37
|
- lib
|
37
|
-
- test
|
38
38
|
required_ruby_version: !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|