class Ant::Storage::Datasource::Sequel

Repository that fetch and store objects using a sequel connection as resource.

Public Class Methods

from_config(conf) click to toggle source
# File lib/ant/storage/datasource/sequel.rb, line 11
def self.from_config(conf)
  conn = ::Sequel.connect(conf['endpoint'], conf)[conf['table'].to_sym]
  if conf['schema'].nil?
    # TODO: decouple use of classes
    new(conn, conf['primary_key'].to_sym,
        IDGenerators[:id])
  else
    # TODO: This line is very high coupled to ant-nanoservice
    new(conn, conf['schema']::PRIMARY_KEY, IDGenerators[:id])
  end
end
new(sequel_object, id, id_generator) click to toggle source
# File lib/ant/storage/datasource/sequel.rb, line 23
def initialize(sequel_object, id, id_generator)
  @sequel = sequel_object
  super(id, id_generator)
end

Public Instance Methods

connection() click to toggle source
# File lib/ant/storage/datasource/sequel.rb, line 47
def connection
  @sequel.db
end
create_(data) click to toggle source
# File lib/ant/storage/datasource/sequel.rb, line 35
def create_(data)
  id = @sequel.insert(data)
  data[@id] ||= id
  data
end
get(id) click to toggle source
# File lib/ant/storage/datasource/sequel.rb, line 28
def get(id)
  result = @sequel.where(@id.to_sym => id).first
  raise(ObjectNotFound, id) if result.nil?

  result
end
store(data) click to toggle source
# File lib/ant/storage/datasource/sequel.rb, line 41
def store(data)
  data2 = data.dup
  data2.delete(@id)
  @sequel.where(@id => data[@id]).update(data2)
end