class Switches::Backends::Postgres

Constants

CHANNEL
TABLE

Public Class Methods

new(uri, instance) click to toggle source
# File lib/switches/backends/postgres.rb, line 11
def initialize(uri, instance)
  @uri = uri
  @instance = instance
end

Public Instance Methods

clear() click to toggle source
# File lib/switches/backends/postgres.rb, line 34
def clear
  table.clear
end
get(item) click to toggle source
# File lib/switches/backends/postgres.rb, line 20
def get(item)
  if result = table.find(item.key)
    JSONSerializer.deserialize(result)
  end
end
listen() click to toggle source
# File lib/switches/backends/postgres.rb, line 26
def listen
  @thread ||= Thread.new { subscribe }
end
notify(update) click to toggle source
# File lib/switches/backends/postgres.rb, line 30
def notify(update)
  connection.notify(CHANNEL, update.to_json)
end
set(item) click to toggle source
# File lib/switches/backends/postgres.rb, line 16
def set(item)
  table.upsert(item.key, item.to_json)
end
stop() click to toggle source
# File lib/switches/backends/postgres.rb, line 38
def stop
  listen.kill
  listener.close
  connection.close
end

Private Instance Methods

connection() click to toggle source
# File lib/switches/backends/postgres.rb, line 50
def connection
  @connection ||= Connection.new(@uri)
end
listener() click to toggle source
# File lib/switches/backends/postgres.rb, line 46
def listener
  @listener ||= Connection.new(@uri)
end
subscribe() click to toggle source
# File lib/switches/backends/postgres.rb, line 58
def subscribe
  listener.listen(CHANNEL) do |message|
    update = Update.load(message)
    @instance.notified(update)
  end
end
table() click to toggle source
# File lib/switches/backends/postgres.rb, line 54
def table
  @table ||= Table.new(TABLE, connection)
end