Class: Palsy

Inherits:
SQLite3::Database
  • Object
show all
Includes:
Singleton
Defined in:
lib/palsy.rb,
lib/palsy/basic/map.rb,
lib/palsy/basic/set.rb,
lib/palsy/basic/list.rb,
lib/palsy/basic/object.rb,
lib/palsy/basic/generic.rb,
lib/palsy/basic/collection.rb

Overview

Present ruby core data structures in a manner similar to perl’s tie backed by a SQLite database. Intended to be as simple as possible, sacrificing performance and flexibility to do so.

It is not a 1:1 emulation of tie, as ruby cannot support this. What it does do is provide a convincing enough facsimile by emulating the interface, and making it easy to convert to native ruby types when that’s not enough.

This library is completely unapologetic with regards to how little it does or how slow it does things.

All writes are fully consistent, which is something SQLite gives us. Reads always hit the database. This allows us to reason more clearly about how our data persists, even in concurrent models where shared state can get very complicated to use.

Defined Under Namespace

Classes: Collection, Generic, List, Map, Object, Set

Constant Summary collapse

VERSION =

Palsy’s version, as a string.

PalsyVersion

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePalsy

Initializer. Since Palsy is a proper Ruby singleton, calling Palsy.instance will run this the first time, but calling this directly is not advised. Use the Palsy class methods like Palsy.change_db, or set Palsy.database= before working with the rest of the library.



69
70
71
# File 'lib/palsy.rb', line 69

def initialize
  super(connect)
end

Class Attribute Details

.databaseObject

The name of the database Palsy will manipulate. Set this before using Palsy. Look at Palsy.change_db for a better way to swap DB’s during runtime.



34
35
36
# File 'lib/palsy.rb', line 34

def database
  @database
end

Class Method Details

.change_db(db_name) ⇒ Object

Given a db name, assigns that to Palsy and initiates a reconnection.

Note that existing Palsy objects will immediately start working against this new database, and expect everything needed to read the data to exist, namely the tables they’re keyed against.



44
45
46
47
# File 'lib/palsy.rb', line 44

def self.change_db(db_name)
  self.database = db_name
  self.reconnect
end

.closeObject

Closes the SQLite database.



59
60
61
# File 'lib/palsy.rb', line 59

def self.close
  self.instance.close
end

.reconnectObject

Initiates a reopening of the SQLite database.



52
53
54
# File 'lib/palsy.rb', line 52

def self.reconnect
  self.instance.reconnect
end

Instance Method Details

#connectObject

Opens the database. Note that this is a no-op unless Palsy.database= is set.



85
86
87
88
89
# File 'lib/palsy.rb', line 85

def connect
  if self.class.database
    SQLite3::Database.new(self.class.database)
  end
end

#execute_t(query, args = [], &block) ⇒ Object

Execute this query in an exclusive transaction.



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

def execute_t(query, args=[], &block)
  result = nil

  transaction(:exclusive) do
    result = execute(query, args, &block)
  end

  return result
end

#reconnectObject

Initiates a reopening of the SQLite database. Instance method that Palsy.reconnect uses.



77
78
79
80
# File 'lib/palsy.rb', line 77

def reconnect
  close rescue nil
  __setobj__(connect)
end