Class: Kybus::Storage::Datasource::Sequel

Inherits:
Repository
  • Object
show all
Defined in:
lib/kybus/storage/datasource/sequel.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sequel_object, id, id_generator) ⇒ Sequel

Returns a new instance of Sequel.



25
26
27
28
# File 'lib/kybus/storage/datasource/sequel.rb', line 25

def initialize(sequel_object, id, id_generator)
  @sequel = sequel_object
  super(id, id_generator)
end

Class Method Details

.from_config(conf) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kybus/storage/datasource/sequel.rb', line 11

def self.from_config(conf)
  require 'sequel'
  conf['table'] ||= conf['schema_name']
  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 kybus-nanoservice
    new(conn, conf['schema']::PRIMARY_KEY, IDGenerators[:id])
  end
end

Instance Method Details

#connectionObject



49
50
51
# File 'lib/kybus/storage/datasource/sequel.rb', line 49

def connection
  @sequel.db
end

#create_(data) ⇒ Object



37
38
39
40
41
# File 'lib/kybus/storage/datasource/sequel.rb', line 37

def create_(data)
  id = @sequel.insert(data)
  data[@id] ||= id
  data
end

#get(id) ⇒ Object

Raises:

  • (ObjectNotFound)


30
31
32
33
34
35
# File 'lib/kybus/storage/datasource/sequel.rb', line 30

def get(id)
  result = @sequel.where(@id.to_sym => id).first
  raise(ObjectNotFound, id) if result.nil?

  result
end

#store(data) ⇒ Object



43
44
45
46
47
# File 'lib/kybus/storage/datasource/sequel.rb', line 43

def store(data)
  data2 = data.dup
  data2.delete(@id)
  @sequel.where(@id => data[@id]).update(data2)
end