persist 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # Persist
1
+ # Persist.db
2
2
 
3
- Persist is a simple gem that allows you to painlessly save Ruby objects in a NoSQL persistent store.
3
+ Persist.db is a simple gem that allows you to painlessly save Ruby Objects in a NoSQL persistent file store.
4
4
 
5
5
  ## Installation
6
6
 
@@ -12,26 +12,30 @@ And then execute:
12
12
 
13
13
  $ bundle
14
14
 
15
- Or install it yourself as:
15
+ Or install it yourself:
16
16
 
17
17
  $ gem install persist
18
18
 
19
19
  ## Usage
20
20
 
21
- Example in Pry (or IRB):
21
+ Example in Pry (or IRB if you must):
22
22
 
23
23
  ```ruby
24
24
  require 'persist'
25
25
  #=> true
26
26
 
27
- Persist.pull
28
- #=> {}
27
+ Persist.db
28
+ # => #<PStore:0x007f8c199c9698
29
+ @abort=false,
30
+ @filename=".db.pstore",
31
+ @lock=#<Mutex:0x007f8c199c9580>,
32
+ @rdonly=true,
33
+ @table={},
34
+ @thread_safe=true,
35
+ @ultra_safe=true>
29
36
 
30
- Persist.store[:siblings] = ["Katie", "Molly", "Shannnon"]
31
- #=> ["Katie", "Molly", "Shannnon"]
32
-
33
- Persist.push # This saves your Stash to the persistent store.
34
- #=> <File:.persistent_store (closed)>
37
+ Persist[:pie] = ['Key Lime', 'Strawberry Rhubarb', 'Blackberry Cobbler']
38
+ # => ["Key Lime", "Strawberry Rhubarb", "Blackberry Cobbler"]
35
39
  ```
36
40
 
37
41
  You can now quit IRB and your store will persist!
@@ -40,15 +44,14 @@ You can now quit IRB and your store will persist!
40
44
  require 'persist'
41
45
  #=> true
42
46
 
43
- Persist.pull
44
- #=> {:siblings=>["Katie", "Molly", "Shannnon"]}
45
-
46
- Persist.pull[:siblings]
47
- #=> ["Katie", "Molly", "Shannnon"]
47
+ Persist[:pie]
48
+ #=> ["Key Lime", "Strawberry Rhubarb", "Blackberry Cobbler"]
48
49
  ```
49
50
 
51
+ TODO: Document Persist.db's other public methods such as #transaction, #delete, and #keys. The best documentation in the meanwhile is in the code itself: https://github.com/Havenwood/persist/blob/master/lib/persist.rb
52
+
50
53
  ## Contributing
51
54
 
52
55
  1. Fork it
53
- 2. Commit your changes (`git commit -am 'Added some feature'`)
56
+ 2. Commit your changes (`git commit -am 'Did something'`)
54
57
  3. Create new Pull Request
@@ -1,3 +1,3 @@
1
1
  module Persist
2
- VERSION = "0.0.2"
2
+ VERSION = '0.0.3'
3
3
  end
data/lib/persist.rb CHANGED
@@ -1,27 +1,140 @@
1
- require "persist/version"
1
+ require 'pstore'
2
+ require 'persist/version'
2
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.
3
7
  module Persist
4
8
  class << self
5
- STORE = '.persistent_store'
6
-
7
- attr_accessor :store
8
-
9
- def pull
10
- unless File.exists? STORE
11
- self.reset!
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
+ alias tables keys
60
+
61
+ # Public: Fetch a particular table from the persistent store.
62
+ #
63
+ # table - A Symbol.
64
+ #
65
+ # Examples
66
+ #
67
+ # Persist[:author]
68
+ # # => {:first_name => "Shannon", :last_name => "Skipper"}
69
+ #
70
+ # Persist[:author][:first_name]
71
+ # # => "Shannon"
72
+ #
73
+ # Returns the value stored in the fetched table.
74
+ def [] table
75
+ # Open a read-only transaction.
76
+ @store.transaction(true) do
77
+ # Return a particular table from the persistent store.
78
+ @store[table]
12
79
  end
13
- @store = Marshal.load File.read(STORE)
14
80
  end
15
-
16
- def push
17
- File.open STORE, 'w' do |store|
18
- store << Marshal.dump(@store)
81
+
82
+ # Public: Use a single transaction to set a table value.
83
+ #
84
+ # table - A Symbol.
85
+ #
86
+ # value - Any Ruby Object that is marshallable.
87
+ #
88
+ # Examples
89
+ #
90
+ # Persist[:sky] = 'blue'
91
+ # # => "blue"
92
+ #
93
+ # Returns the value of the table.
94
+ def []= table, value
95
+ # Open a writable transaction.
96
+ @store.transaction do
97
+ # Process the single transaction.
98
+ @store[table] = value
99
+ end # Commit transaction.
100
+ end
101
+
102
+ # Public: Process multiple transactions to set table values and commit if
103
+ # all are transactions are successful.
104
+ #
105
+ # block - A required block that processes multiple transactions that
106
+ # succeed or fail together to ensure that data is not left in a
107
+ # transitory state.
108
+ #
109
+ # Examples
110
+ #
111
+ # Persist.transaction do
112
+ # Persist.store[:weather] = 'sunny'
113
+ # Persist.store[:hour] = 'midday'
114
+ # end
115
+ # # => nil
116
+ #
117
+ # Returns nothing.
118
+ def transaction &block
119
+ @store.transaction do
120
+ yield
121
+ @store.commit
19
122
  end
20
123
  end
21
-
22
- def reset!
23
- File.open STORE, 'w' do |store|
24
- store << Marshal.dump({})
124
+
125
+ # Public: Delete an entire root table from the persistent store.
126
+ #
127
+ # Examples
128
+ #
129
+ # Persist.delete :author
130
+ # # => nil
131
+ #
132
+ # Returns nothing.
133
+ def delete table
134
+ # Open a writable transaction.
135
+ @store.transaction do
136
+ # Delete a particular table from the persistent store.
137
+ @store.delete table
25
138
  end
26
139
  end
27
140
  end
data/persist.gemspec CHANGED
@@ -4,13 +4,13 @@ require File.expand_path('../lib/persist/version', __FILE__)
4
4
  Gem::Specification.new do |gem|
5
5
  gem.authors = ["Shannon"]
6
6
  gem.email = ["shannonskipper@gmail.com"]
7
- gem.description = %q{Simple NoSQL Persistent Object Store for Ruby}
8
- gem.summary = %q{This gem allows you to save Ruby Objects to a persistent store and recover them later.}
7
+ gem.description = %q{A DSL for storing Ruby Objects transactionally in a persistent NoSQL database}
8
+ gem.summary = %q{Persist.db is a DSL implemented around Ruby Standard Library's PStore to facilitate simple file-persistant storage of Ruby objects in a transactional NoSQL database.}
9
9
  gem.homepage = "https://github.com/Havenwood/persist"
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
12
12
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.test_files = gem.files.grep(%r{^test/})
14
14
  gem.name = "persist"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Persist::VERSION
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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-08 00:00:00.000000000 Z
12
+ date: 2012-05-21 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Simple NoSQL Persistent Object Store for Ruby
14
+ description: A DSL for storing Ruby Objects transactionally in a persistent NoSQL
15
+ database
15
16
  email:
16
17
  - shannonskipper@gmail.com
17
18
  executables: []
@@ -49,7 +50,8 @@ rubyforge_project:
49
50
  rubygems_version: 1.8.24
50
51
  signing_key:
51
52
  specification_version: 3
52
- summary: This gem allows you to save Ruby Objects to a persistent store and recover
53
- them later.
53
+ summary: Persist.db is a DSL implemented around Ruby Standard Library's PStore to
54
+ facilitate simple file-persistant storage of Ruby objects in a transactional NoSQL
55
+ database.
54
56
  test_files: []
55
57
  has_rdoc: