Class: Geode::SequelStore

Inherits:
Store
  • Object
show all
Defined in:
lib/geode/sequel.rb

Overview

A store that uses a relational database supported by Sequel instead of Redis. This is mainly useful for Heroku, where persistent Postgres is free, but the same is not true of Redis. Unless you have a similarly good reason to use this class, use ‘RedisStore` instead.

Instance Method Summary collapse

Methods inherited from Store

#[], #peek

Constructor Details

#initialize(name, connection = nil) ⇒ SequelStore

Connect to a store held in a relational database supported by Sequel. A table named ‘geode` will be created and used to store the data.

Parameters:

  • name (Symbol, String)

    The name of the store

  • connection (Hash, String) (defaults to: nil)

    Connection parameters passed to ‘Sequel.connect`. Defaults to `{ adapter: ’postgres’ }‘



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/geode/sequel.rb', line 16

def initialize(name, connection = nil)
  super

  connection ||= { adapter: 'postgres' }
  db = Sequel.connect(connection)
  db.create_table? :geode do
    String :name, primary_key: true
    File :value
  end

  @db = db[:geode]
end

Instance Method Details

#destroyObject



43
44
45
46
# File 'lib/geode/sequel.rb', line 43

def destroy
  @db.where(name: @name).delete
  nil
end

#openObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/geode/sequel.rb', line 29

def open
  store = @db.where(name: @name)
  table = if store.empty?
            store.insert(name: @name, value: '')
            {}
          else
            Marshal.load(store.first[:value])
          end

  (yield table).tap do
    store.update(value: Sequel.blob(Marshal.dump(table)))
  end
end