Module: Persist

Defined in:
lib/persist/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. All methods are module methods and should be called on the Persist module.

Constant Summary collapse

VERSION =
'0.1.1'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dbObject (readonly)

Public: Returns the persistant store Object if initialized.



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

def db
  @db
end

Class 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

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

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

Returns the value stored in the fetched table.



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

def [] table
  initialize_db
  
  @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

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

Returns the value of the table.



164
165
166
167
168
169
170
# File 'lib/persist/persist.rb', line 164

def []= table, value
  initialize_db
  
  @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

Persist.delete :author
# => nil

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

Returns nothing.



186
187
188
189
190
191
192
193
194
195
# File 'lib/persist/persist.rb', line 186

def delete *tables
  initialize_db
  
  @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

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

Persist.fetch :snowman
# => nil

Persist.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
149
150
# File 'lib/persist/persist.rb', line 144

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

.initialize_dbObject



36
37
38
# File 'lib/persist/persist.rb', line 36

def initialize_db
  @db || pull
end

.key?(table) ⇒ Boolean

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

table - A Symbol.

Examples

Persist.key? :author
# => true

Persist.key? :this_does_not_exist
# => false

Returns true or false.

Returns:

  • (Boolean)


95
96
97
98
99
100
101
# File 'lib/persist/persist.rb', line 95

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

.keysObject

Public: Fetch a list of persistent store root tables.

Examples

Persist.keys
# => [:author]

Returns an Array containing the persistent store root tables.



73
74
75
76
77
78
79
# File 'lib/persist/persist.rb', line 73

def keys
  initialize_db

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

.pathObject

Public: Determine location of the persistent store file.

Examples

Persist.path
# => ".db.pstore"

Returns the path to the data file as a String.



205
206
207
208
209
# File 'lib/persist/persist.rb', line 205

def path
  initialize_db
  
  @db.path
end

.pullObject

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.pull
# => #<PStore:0x007f8c199c9698
  @abort=false,
  @filename=".db.pstore",
  @lock=#<Mutex:0x007f8c199c9580>,
  @rdonly=true,
  @table={},
  @thread_safe=true,
  @ultra_safe=true>

Returns the entire persistent store Object.



29
30
31
32
33
34
# File 'lib/persist/persist.rb', line 29

def pull
  @db = PStore.new '.db.pstore', true
  @db.ultra_safe = true
  @db.transaction(true) {}
  @db
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

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

Returns nothing.



56
57
58
59
60
61
62
63
# File 'lib/persist/persist.rb', line 56

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