Browse files

renamed methods in hopes of clearer DSL

  • Loading branch information...
1 parent 67928e0 commit fe6b9ab3ee0f00b181b9fb6d9fe9fdfee1aba69d @havenwood committed May 22, 2012
Showing with 25 additions and 25 deletions.
  1. +23 −23 lib/persist/persist.rb
  2. +2 −2 test/test_persist.rb
View
46 lib/persist/persist.rb
@@ -4,15 +4,15 @@
module Persist
class << self
# Public: Returns the persistant store Object if initialized.
- attr_reader :store
+ attr_reader :db
# Public: Initialize the PStore Object--deserializing the marshalled Hash
# stored in the '.db.pstore' file (creating the file if it does't exist)--
# and set thread_safe and ultra_safe to true.
#
# Examples
#
- # Persist.db
+ # Persist.pull
# # => #<PStore:0x007f8c199c9698
# @abort=false,
# @filename=".db.pstore",
@@ -23,11 +23,11 @@ class << self
# @ultra_safe=true>
#
# Returns the entire persistent store Object.
- def db
- @store = PStore.new '.db.pstore', true
- @store.ultra_safe = true
- @store.transaction(true) {}
- @store
+ def pull
+ @db = PStore.new '.db.pstore', true

I like the idea of this gem, and found myself starting to write something similar.
Only thing I'd like is for this file to be configurable.

E.g.

Persist.storage_file = 'some_name.pstore'
@havenwood
Owner

@markburns Agreed, you ought to be able to specify the db filename.

@havenwood
Owner

@markburns I went ahead and added the feature you suggested by adding a #path= in addition to the existing #path with the cfb86f2 commit. I kinda like your #storage_file nomenclature though and have a feeling I should go ahead and refactor this whole thing from a Module to a Class, since there is enough state to deserve initializing now.

That's great, thanks.

I don't know. Path also makes sense really, it's not actually a file, it is really a path.

Wouldn't you lose a bunch of the ease of use/simplicity of the API by making this instantiatable?
Don't get me wrong, I'm all for instances over class methods in general, but here it kind of makes sense.

Maybe it can become instantiatable for those that want more control/more sane code when using different
data files in different part of the apps, but can fall back to the kind of singleton pattern for the default case.
Maybe a block syntax would be nice too, if you don't want to necessarily change the default data store.

E.g.

Persist.in ".cat.pstore" do |p|
  p['tail'] = true
end

#and for readability
Persist.from ".cat.pstore" do |p|
  p['tail']
end

#=> true

Or maybe this is all just adding too much to what should be a simple library?

BTW, what's the meaning of pull here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ @db.ultra_safe = true
+ @db.transaction(true) {}
+ @db
end
# Public: Fetch a list of persistent store root tables.
@@ -39,8 +39,8 @@ def db
#
# Returns an Array containing the persistent store root tables.
def keys
- @store.transaction true do
- @store.roots
+ @db.transaction true do
+ @db.roots
end
end
@@ -59,8 +59,8 @@ def keys
#
# Returns true or false.
def key? table
- @store.transaction true do
- @store.root? table
+ @db.transaction true do
+ @db.root? table
end
end
@@ -79,8 +79,8 @@ def key? table
#
# Returns the value stored in the fetched table.
def [] table
- @store.transaction true do
- @store[table]
+ @db.transaction true do
+ @db[table]
end
end
@@ -97,8 +97,8 @@ def [] table
#
# Returns the value of the table.
def []= table, value
- @store.transaction do
- @store[table] = value
+ @db.transaction do
+ @db[table] = value
end
end
@@ -112,16 +112,16 @@ def []= table, value
# Examples
#
# Persist.transaction do
- # Persist.store[:weather] = 'sunny'
- # Persist.store[:hour] = 'midday'
+ # Persist.db[:weather] = 'sunny'
+ # Persist.db.delete[:author]
# end
# # => nil
#
# Returns nothing.
def transaction &block
- @store.transaction do
+ @db.transaction do
yield
- @store.commit
+ @db.commit
end
end
@@ -140,11 +140,11 @@ def transaction &block
#
# Returns nothing.
def delete *tables
- @store.transaction do
+ @db.transaction do
tables.each do |table|
- @store.delete table
+ @db.delete table
end
- @store.commit
+ @db.commit
end
end
@@ -157,7 +157,7 @@ def delete *tables
#
# Returns the path to the data file as a String.
def path
- @store.path
+ @db.path
end
end
end
View
4 test/test_persist.rb
@@ -2,13 +2,13 @@
describe Persist do
before do
- @db = Persist.db
+ Persist.pull
Persist[:author] = {first_name: 'Shannon', last_name: 'Skipper'}
end
describe "initializing the persistent store with Persist.db" do
it "returns a PStore object" do
- assert_equal PStore, @db.class
+ assert_equal PStore, Persist.db.class
end
end

0 comments on commit fe6b9ab

Please sign in to comment.