Module: Squongo

Defined in:
lib/squongo.rb,
lib/squongo/version.rb,
lib/squongo/namespace.rb

Defined Under Namespace

Classes: Connection, Document, Writer

Constant Summary collapse

TABLES_QUERY =
'SELECT name FROM sqlite_master WHERE type="table";'
TABLE_SCHEMA =
'(id INTEGER PRIMARY KEY, data JSON, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP NULL);'
VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.connect(path) ⇒ Object



14
15
16
17
18
# File 'lib/squongo.rb', line 14

def self.connect(path)
  @@connection = Connection.connect(path)
  migrate_database
  start_writer
end

.connectionObject



49
50
51
# File 'lib/squongo.rb', line 49

def self.connection
  @@connection
end

.create_table(name) ⇒ Object



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

def self.create_table(name)
  create_table_query = "CREATE TABLE #{name} #{TABLE_SCHEMA}"
  connection.db.execute create_table_query
end

.document_typesObject



34
35
36
37
38
# File 'lib/squongo.rb', line 34

def self.document_types
  ObjectSpace.each_object(Class)
    .select { |klass| klass < Squongo::Document }
    .map { |type| type.table }
end

.ipc_decode(packet) ⇒ Object



61
62
63
# File 'lib/squongo.rb', line 61

def self.ipc_decode(packet)
  JSON.parse Base64.decode64(packet)
end

.ipc_encode(data) ⇒ Object



57
58
59
# File 'lib/squongo.rb', line 57

def self.ipc_encode(data)
  Base64.strict_encode64 data.to_json.to_s
end

.migrate_databaseObject



24
25
26
27
28
# File 'lib/squongo.rb', line 24

def self.migrate_database
  missing_tables.each do |table|
    create_table table
  end
end

.missing_tablesObject



30
31
32
# File 'lib/squongo.rb', line 30

def self.missing_tables
  document_types - tables
end

.reconnectObject



20
21
22
# File 'lib/squongo.rb', line 20

def self.reconnect
  @@connection.reconnect
end

.save(model_information) ⇒ Object



53
54
55
# File 'lib/squongo.rb', line 53

def self.save(model_information)
  @@writer.puts ipc_encode(model_information)
end

.start_writerObject



65
66
67
68
69
70
71
72
# File 'lib/squongo.rb', line 65

def self.start_writer
  @@reader, @@writer = IO.pipe
  @@squongo_writer = Squongo::Writer.new(@@reader, @@writer, Process.pid)

  @@pid = fork do
    @@squongo_writer.start
  end
end

.tablesObject



40
41
42
# File 'lib/squongo.rb', line 40

def self.tables
  connection.db.execute(TABLES_QUERY).flatten.map(&:to_sym)
end

.timestampObject



74
75
76
# File 'lib/squongo.rb', line 74

def self.timestamp
  DateTime.now.strftime('%Y-%m-%d %H:%M:%S')
end