Skip to content

Commit

Permalink
renamed methods in hopes of clearer DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
havenwood committed May 22, 2012
1 parent 67928e0 commit fe6b9ab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
46 changes: 23 additions & 23 deletions lib/persist/persist.rb
Expand Up @@ -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",
Expand All @@ -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

This comment has been minimized.

Copy link
@markburns

markburns Dec 2, 2013

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'

This comment has been minimized.

Copy link
@havenwood

havenwood Dec 2, 2013

Author Owner

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

This comment has been minimized.

Copy link
@havenwood

havenwood Dec 2, 2013

Author 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.

This comment has been minimized.

Copy link
@markburns

markburns Dec 3, 2013

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?

@db.ultra_safe = true
@db.transaction(true) {}
@db
end

# Public: Fetch a list of persistent store root tables.
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
4 changes: 2 additions & 2 deletions test/test_persist.rb
Expand Up @@ -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

Expand Down

0 comments on commit fe6b9ab

Please sign in to comment.