Class: Persist

Inherits:
Object
  • Object
show all
Defined in:
lib/persist.rb,
lib/persist/version.rb

Overview

Public: Implements a DSL around Ruby Standard Library’s PStore to facilitate simple file-persistant storage of Ruby objects in a transactional NoSQL database.

Constant Summary collapse

VERSION =
'1.0.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = '.db.pstore') ⇒ Persist

Initializes the PStore Object and sets thread_safe and ultra_safe to true. Creates the file store at the specified path if it does not exist.

path - An optional String representing the relative path of the file.

Examples

store = Persist.new
# => #<Persist:0x007f97b1b1b930
  @db=
    #<PStore:0x007f97b1b1b8e0
     @abort=false,
     @filename=".db.pstore",
     @lock=#<Mutex:0x007f97b1b1b7c8>,
     @rdonly=true,
     @table=
      {:trees=>["oak", "pine", "cedar"],
       :one=>"first",
       :two=>"second",
       :author=>{:first_name=>"Shannon", :last_name=>"Skipper"},
       :aim=>true,
       :pie=>["Key Lime", "Strawberry Rhubarb", "Blackberry Cobbler"]},
     @thread_safe=true,
     @ultra_safe=true>,
   @path=".db.pstore">

Returns the entire persistent store Object.



40
41
42
43
44
45
46
# File 'lib/persist.rb', line 40

def initialize path = '.db.pstore'
  @path = path
  @db = PStore.new path, true
  @db.ultra_safe = true
  @db.transaction(true) {}
  @db
end

Instance Attribute Details

#dbObject (readonly)

Public: Returns the persistant store Object if initialized.



8
9
10
# File 'lib/persist.rb', line 8

def db
  @db
end

#pathObject (readonly)

Public: Returns the path to the persistent store file.



11
12
13
# File 'lib/persist.rb', line 11

def path
  @path
end

Instance Method Details

#[](table) ⇒ Object

Public: Fetch a particular table from the persistent store.

table - A Symbol corresponding to a root table key in the persistent

store.

Examples

store[:author]
# => {:first_name => "Shannon", :last_name => "Skipper"}

store[:author][:first_name]
# => "Shannon"

Returns the value stored in the fetched table.



119
120
121
122
123
# File 'lib/persist.rb', line 119

def [] table
  @db.transaction true do
    @db[table]
  end
end

#[]=(table, value) ⇒ Object

Public: Use a single transaction to set a table value.

table - A Symbol.

value - Any Ruby Object that is marshallable.

Examples

store[:sky] = 'blue'
# => "blue"

Returns the value of the table.



162
163
164
165
166
# File 'lib/persist.rb', line 162

def []= table, value
  @db.transaction do
    @db[table] = value
  end
end

#delete(*tables) ⇒ Object

Public: Delete one or more entire root tables from the persistent store.

tables - One or more Symbols corresponding to root table keys in the

persistent store.

Examples

store.delete :author
# => nil

store.delete :author, :clients, :rentals
# => nil

Returns nothing.



182
183
184
185
186
187
188
189
# File 'lib/persist.rb', line 182

def delete *tables
  @db.transaction do
    tables.each do |table|
      @db.delete table
    end
    @db.commit
  end
end

#fetch(table, default = nil) ⇒ Object

Public: Fetch a particular table from the persistent store.

table - A Symbol corresponding to a root table key in the persistent

store.

default - An optional value that is returned if the table is not found.

Examples

store.fetch :author
# => {:first_name => "Shannon", :last_name => "Skipper"}

store.fetch :snowman
# => nil

store.fetch :snowman, 'default value instead of nil'
# => "default value instead of nil"

Returns the value stored in the fetched table.



144
145
146
147
148
# File 'lib/persist.rb', line 144

def fetch table, default = nil
  @db.transaction true do
    @db.fetch table, default
  end
end

#key?(table) ⇒ Boolean

Public: Determine whether a particular persistent store root table exists.

table - A Symbol.

Examples

store.key? :author
# => true

store.key? :this_does_not_exist
# => false

Returns true or false.

Returns:

  • (Boolean)


99
100
101
102
103
# File 'lib/persist.rb', line 99

def key? table
  @db.transaction true do
    @db.root? table
  end
end

#keysObject

Public: Fetch a list of persistent store root tables.

Examples

store.keys
# => [:author]

Returns an Array containing the persistent store root tables.



79
80
81
82
83
# File 'lib/persist.rb', line 79

def keys
  @db.transaction true do
    @db.roots
  end
end

#transactionObject

Public: Process multiple transactions to set table values and commit if all transactions are successful.

block - A required block that processes multiple transactions that

succeed or fail together to ensure that data is not left in a 
transitory state.

Examples

store.transaction do |db|
  db[:weather] = 'sunny'
  db.delete[:author]
end
# => nil

Returns nothing.



64
65
66
67
68
69
# File 'lib/persist.rb', line 64

def transaction
  @db.transaction do
    yield @db
    @db.commit
  end
end